class peson err.15|undefined reference to `Person::setlastName(char*)' help!!!
Guys, I really really need help with the following code. It works when I #include "person.cpp", but without it it fusses at me with errors: 15|undefined reference to `Person::setlastName(char*)' and warns: |24|warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]|
If some guru would take time and help me to understand and correct this - I really need help!
Code:
#include <iostream>
using namespace std;
#include "person.h"
#include "person.cpp"
int main(int argc, char *argv[])
{
Person p1;
p1.setlastName("Smith");
p1.setfirstName("John");
p1.setmiddleInitial("M");
p1.setdateOfBirth("06.25.1989");
p1.setstreetAddress("205 Nicholas St.");
p1.setcity("Dallas");
p1.setstate("TX");
p1.setzipCode("52080");
p1.sethomePhone("755-258-5595");
p1.setworkPhone("755-358-1125");
cout << p1.getlastName() << endl
<< p1.getfirstName() << endl
<< p1.getmiddleInitial() << endl
<< p1.getdateOfBirth() << endl
<< p1.getstreetAddress() << endl
<< p1.getcity() << endl
<< p1.getstate() << endl
<< p1.getzipCode() << endl
<< p1.gethomePhone() << endl
<< p1.getworkPhone() << endl;
return 0;
}
Code:
// Person.h
#ifndef PERSON_H
#define PERSON_H
class Person{
char lastName [21];
char firstName [15];
char middleInitial[3];
char dateOfBirth[11];
char streetAddress [25];
char city [20];
char state [3];
char zipCode [11];
char homePhone [13];
char workPhone [13];
public:
void setlastName (char *L);
char* getlastName();
void setfirstName (char *F);
char* getfirstName ();
void setmiddleInitial (char *M);
char* getmiddleInitial ();
void setdateOfBirth (char *D);
char* getdateOfBirth ();
void setstreetAddress (char *A);
char* getstreetAddress ();
void setcity (char *C);
char* getcity ();
void setstate (char *S);
char* getstate ();
void setzipCode (char *Z);
char* getzipCode ();
void sethomePhone (char *H);
char* gethomePhone ();
void setworkPhone (char *W);
char* getworkPhone ();
};
#endif
Code:
//person.cpp:
#include <string.h>
#include "person.h"
void Person::setlastName(char *L)
{
strcpy (lastName, L);
}
char* Person::getlastName()
{
static char temp[21];
strcpy (temp, lastName);
return temp;
}
void Person::setfirstName(char *F)
{
strcpy (firstName, F);
}
char* Person::getfirstName()
{
static char temp[15];
strcpy (temp, firstName);
return temp;
}
void Person::setmiddleInitial(char *M)
{
strcpy (middleInitial, M);
}
char* Person::getmiddleInitial()
{
static char temp[3];
strcpy (temp, middleInitial);
return temp;
}
void Person::setdateOfBirth(char *D)
{
strcpy (dateOfBirth, D);
}
char* Person::getdateOfBirth()
{
static char temp[11];
strcpy (temp, dateOfBirth);
return temp;
}
void Person::setstreetAddress(char *A)
{
strcpy (streetAddress, A);
}
char* Person::getstreetAddress()
{
static char temp[25];
strcpy (temp, streetAddress);
return temp;
}
void Person::setcity(char *C)
{
strcpy (city, C);
}
char* Person::getcity()
{
static char temp[20];
strcpy (temp, city);
return temp;
}
void Person::setstate(char *S)
{
strcpy (state, S);
}
char* Person::getstate()
{
static char temp[3];
strcpy (temp, state);
return temp;
}
void Person::setzipCode(char *Z)
{
strcpy (zipCode, Z);
}
char* Person::getzipCode()
{
static char temp[11];
strcpy (temp, zipCode);
return temp;
}
void Person::sethomePhone(char *H)
{
strcpy (homePhone, H);
}
char* Person::gethomePhone()
{
static char temp[13];
strcpy (temp, homePhone);
return temp;
}
void Person::setworkPhone(char *W)
{
strcpy (workPhone, W);
}
char* Person::getworkPhone()
{
static char temp[13];
strcpy (temp, workPhone);
return temp;
}
Re: class peson err.15|undefined reference to `Person::setlastName(char*)' help!!!
Compiled for me using Visual Studio 2012 without #including person.cpp
Re: class peson err.15|undefined reference to `Person::setlastName(char*)' help!!!
Quote:
Originally Posted by
elenaq13
Guys, I really really need help with the following code. It works when I #include "person.cpp",
Why do you #include "person.cpp"? You should not #include .cpp files,
instead you should include them to the project!
1 Attachment(s)
Re: class peson err.15|undefined reference to `Person::setlastName(char*)' help!!!
Quote:
Originally Posted by
VictorN
Why do you #include "person.cpp"? You should not #include .cpp files,
instead you should include them to the project!
I know! But with include person.cpp in main - it compiles, and without it - it does not compile and yells at me with errors!
Here how it compiles
Attachment 33101
Re: class peson err.15|undefined reference to `Person::setlastName(char*)' help!!!
I would but I can't find C++ , only Visual C++. Can I do it with Visual C++? I am not familiar with it yet.
Re: class peson err.15|undefined reference to `Person::setlastName(char*)' help!!!
Quote:
Originally Posted by
elenaq13
I would but I can't find C++ , only Visual C++. Can I do it with Visual C++? I am not familiar with it yet.
As I said, it compiled for me with Visual C++. You should never #include a .cpp file, but as VictorN said, it needs to be added as part of the project so that the compiler builds it. There are two stages to building an executable file, compiling and linking. Can you tell which stage the error occurred in?
Re: class peson err.15|undefined reference to `Person::setlastName(char*)' help!!!
It's in build messages. I am using CodeBlocks
Re: class peson err.15|undefined reference to `Person::setlastName(char*)' help!!!
I thought that it has something in the .cpp file that should be in .h file - can't figure out what.
Re: class peson err.15|undefined reference to `Person::setlastName(char*)' help!!!
Quote:
Originally Posted by
elenaq13
I thought that it has something in the .cpp file that should be in .h file - can't figure out what.
I don't see anything wrong with the code. It's likely some kind of configuration thing. I don't use Codeblocks, so I can't tell you what to look for. Hopefully somebody will be along that can.
Re: class peson err.15|undefined reference to `Person::setlastName(char*)' help!!!
What compiler are you using?
If using VC++ under solution, source files you need to have the file that contains your main() and also person.cpp. Right click source file and then add, add existing item to add the person.cpp file to the solution.
Code:
char* Person::getlastName()
{
static char temp[21];
strcpy (temp, lastName);
return temp;
}
Note that it is not a good idea to return a pointer to a function static variable. What happens if you have more than one instance of the same class?
Re: class peson err.15|undefined reference to `Person::setlastName(char*)' help!!!
This link
http://wiki.codeblocks.org/index.php...-existing_file
should help with adding a file to Codeblocks to be included in the compile. You need to add person.cpp.
Re: class peson err.15|undefined reference to `Person::setlastName(char*)' help!!!
I have person.cpp in the project - I posted 3 files at the beginning - main.cpp, person.h and person.cpp. They do compile and work well only if I include #include "person.cpp" in the main (which I should not do). I checked some similar programs - and can't find my mistake to save my life.
Re: class peson err.15|undefined reference to `Person::setlastName(char*)' help!!!
Quote:
Originally Posted by
elenaq13
I have person.cpp in the project - I posted 3 files at the beginning - main.cpp, person.h and person.cpp. They do compile and work well only if I include #include "person.cpp" in the main (which I should not do). I checked some similar programs - and can't find my mistake to save my life.
The issue is not with your code - its how you are using Codeblocks. As I use Visual Studio I can only point you in the direction of the Codeblocks website for info about how to add files to a project/solution.
Good luck! :)
Re: class peson err.15|undefined reference to `Person::setlastName(char*)' help!!!
Quote:
Originally Posted by
2kaud
The issue is not with your code - its how you are using Codeblocks.
With all my respect I disagree. Other projects work all right. Saying this, I would like to thank you for your time - and when I find the solution I will be happy to share it here.
Re: class peson err.15|undefined reference to `Person::setlastName(char*)' help!!!
Yeah.... Have a look at main.cpp
You have an extra } at the end of your code