Classes with Headers not Working

Trying to learn to use simple classes outside of main.cpp, and I can't get anything to work no matter what I do. Here's my attempt.

Test.h
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef TEST_H
#define TEST_H

class Test
{

public: 

	void Print();

}

#endif 


Test.cpp
1
2
3
4
5
6
7
8
#include <iostream>
#include "Test.h"

void Test::Print(){

	cout << "It Works" << endl;

}


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include "Test.h"

using namespace std;

int main(){

	Test one;
	one.Print;

	return 0;
}


As I'm sure you can guess, this spams errors. Please help.
You need a semicolon after your class-definition :)

EDIT: Also, in your main, one.Print; has to be one.Print();
Last edited on
Wow, can't believe I didn't see that...

But it still says cout and endl are undefined..... help?
They are in the std namespace so put std:: before them in your class cpp file.
Put "using namespace std;" in Test.cpp also
Last edited on
Don't. "using namespace foo;" is the using directive. It has the same effect as the library having no namespace at all. Standard library has not been put into a namespace without a good reason.

There is using declaration too. "using std::cout; using std::endl;" That brings only those two names into scope.

However, in the example code it is simpler to just prefix the names with std:: where they are used.


http://www.gotw.ca/gotw/053.htm
Topic archived. No new replies allowed.