hi, here I am again. I'm in the pace where you will code a header or header of your own in a different file for program reusability. I have tried the code many times in different ways. I decided to make it small as shown below so that I could easily check the error quickly, unfortunately, I cannot see any problem in my code. I have no idea what's happening, but I've searched over the net and they have the same structure as mine. I don't know if it's with the compiler, but if I created Name.h without seperating the implementation of that class it works. I hope you could help me with this and give some advice. Hope to hear from you soon.
file: Name.h
Code:
#include <string>
using std::string;
class Name
{
public:
Name( string );
void setName( string );
string getName();
void displayName();
private:
string name;
};
file: Name.cpp
Code:
#include <iostream>
using std::cout;
using std::endl;
#include "Name.h"
Name::Name( string n )
{
setName( n );
}
void Name::setName( string n )
{
name = n;
}
string Name::getName()
{
return name;
}
void Name::displayName()
{
cout << "Name: " << getName() << endl;
}
file: Person.cpp
Code:
#include <iostream>
#include <string>
using namespace::std;
#include "Name.h"
int main( void )
{
Name emp1( "John Travolta" );
Name emp2( "Nicolas Cage" );
cin.get();
return 0;
}
Error:
C:\DOCUME~1\INFORM~1\LOCALS~1\Temp\ccYbcaaa.o(.tex t+0x1ac) In function `main':
[Linker error] undefined reference to `Name::Name(std::string)'
[Linker error] undefined reference to `Name::Name(std::string)'
C:\DOCUME~1\INFORM~1\LOCALS~1\Temp\ccYbcaaa.o(.tex t+0x1ac) ld returned 1 exit status
Compiler:
DevC++ 4.9.9.2