Reputation: 93
I'm doing something wrong, I know. I can't quite figure out how to
link two .cpp
files together through a header file. The calling
method can't see the other source.
I'm using Code::Blocks as an IDE with MinGW.
Any help would be greatly appreciated. It would be even more appreciated if you could show the fixed source, link in the reply to a pastebin page with it.
/***********************************main.cpp***********************************/
#include <iostream>
using namespace std;
#include "test.h"
int main()
{
printTest(); //can't see printTest, defined in test.cpp
return 0;
};
/***********************************test.h***********************************/
#ifndef TEST_H_INCLUDED
#define TEST_H_INCLUDED
void printTest();
#endif // TEST_H_INCLUDED
/***********************************test.cpp***********************************/
#include "test.h"
void printTest()
{
cout << "Hello world!" << endl;
};
Upvotes: 1
Views: 10024
Reputation: 8242
You might find this code blocks wiki helpful. It looks like Code blocks uses a managed build system so if you add the file to the project properly then it should know to compile it and link in the object file that results.
And just to be more explicit about some other comments, when you use "using namespace std;" the namespace is only brought into scope for the file where the using statement is located. That is why others are telling you to explicitly specify the std:: namespace. You could also bring all of the std namespace into scope in the test.cpp file. Many people consider this a bad habit to get into. It's generally better to bring into scope just what you need via
using std::cout;
using std::endl;
Finally, remember that std::endl adds a new line AND flushes the buffer, it's not a good replacement for a new line character in all cases.
Upvotes: 4
Reputation: 12963
sanket answer’s seems incomplete to me.
You need to add #include <iostream>
in your test.cpp
so that the compiler knows what "cout" is.
As sanket stated, you should use std::cout
and std::endl
in test.cpp
.
Upvotes: 0
Reputation: 1398
In test.cpp replace cout << "Hello world!" << endl;
by std::cout << "Hello world!" << std::endl;
Upvotes: 1