-
November 4th, 2014, 03:39 PM
#1
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;
}
Last edited by elenaq13; November 4th, 2014 at 07:46 PM.
-
November 4th, 2014, 03:48 PM
#2
Re: class peson err.15|undefined reference to `Person::setlastName(char*)' help!!!
Compiled for me using Visual Studio 2012 without #including person.cpp
Last edited by GCDEF; November 4th, 2014 at 03:56 PM.
-
November 4th, 2014, 04:32 PM
#3
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.
-
November 4th, 2014, 04:37 PM
#4
Re: class peson err.15|undefined reference to `Person::setlastName(char*)' help!!!
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?
-
November 4th, 2014, 04:42 PM
#5
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.
-
November 4th, 2014, 04:47 PM
#6
Re: class peson err.15|undefined reference to `Person::setlastName(char*)' help!!!
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.
-
November 4th, 2014, 03:55 PM
#7
Re: class peson err.15|undefined reference to `Person::setlastName(char*)' help!!!
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!
Victor Nijegorodov
-
November 4th, 2014, 04:30 PM
#8
Re: class peson err.15|undefined reference to `Person::setlastName(char*)' help!!!
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
-
November 4th, 2014, 04:40 PM
#9
Re: class peson err.15|undefined reference to `Person::setlastName(char*)' help!!!
It's in build messages. I am using CodeBlocks
-
November 4th, 2014, 04:51 PM
#10
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?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
November 4th, 2014, 05:00 PM
#11
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
November 4th, 2014, 05:01 PM
#12
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.
-
November 4th, 2014, 05:06 PM
#13
Re: class peson err.15|undefined reference to `Person::setlastName(char*)' help!!!
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!
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
November 4th, 2014, 05:23 PM
#14
Re: class peson err.15|undefined reference to `Person::setlastName(char*)' help!!!
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.
-
November 5th, 2014, 05:04 AM
#15
Re: class peson err.15|undefined reference to `Person::setlastName(char*)' help!!!
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.
For what it's worth, you should note that your code *does* compile. Rather, it does not *link*. This means there is nothing wrong with your code, and the compiler itself is perfectly happy with it.
It's the linker that is having trouble finding your compiled objects (in this case Person::setlastName). Linker errors are rarer and more obscure, so harder to deal with at first, but you'll get the hang of it:
90% of the time, it's because you declared a function, but either:
* Simply forgot to implement it.
* Implemented a function with a different name/type.
* Forgot to add "ClassName::" in implementation, which creates a different function.
The rest of the time, it's more complicated, and usually a configuration issue:
* Either the cpp is not in the project, and not compiled
* The libs you want to link against are not in your project
Honestly, that's just about it. Solving them is usually more trial & error though...
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|