Help please!

Hi guys,

When i create classes they work perfectly when the class definition is in main.cpp. But when i try make classes with the header and source files separately the compiler cannot seem to find the source file and comes up with "undefined reference to className::classMemberFunction()" .

Heres an example :

The header file:

#ifndef EXAMPLE_H
#define EXAMPLE_H

#include <iostream>


class Example
{
public:
Example();

};

#endif // EXAMPLE_H


The source file:

#include "Example.h"

Example::Example()
{
std::cout << "This isn't working ):";
}


The main file:

#include <iostream>
#include "Example.h"

int main()
{
Example eg;

return 0;
}

The error that comes up :

undefined reference to 'Example::Example()'
=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===


I can compile the program if I go settings-->compiler-->linker setings, and then under link library click add and add the pathway to the source file.
The only problem is that i have to then delete the added link library path to be able to compile other programs and then i have to reset it to compile the class program. I have made sure that all the files are in the same directory.

Please can someone tell me if there is a permanent setting that i can change to fix this, its really annoying me.

Apparently this does not happen with the older 12.11 version, but that gives me another problem - something about the GNU GCC compiler.

Also im quite a noob in programming, thought it should be mentioned.

Any help will be greatly appreciated(:
Does file "example.cpp" appear in project files list?
use command:
g++ main.cpp example.cpp -o test_example
replace here main.cpp example.cpp by names of your files.
There are many a beautiful papers for newbies about gcc and it's options. Google helps!
Topic archived. No new replies allowed.