CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 24

Thread: class peson err.15|undefined reference to `Person::setlastName(char*)' help!!!

  1. #1
    Join Date
    Nov 2014
    Posts
    13

    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.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,620

    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.

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,980

    Re: class peson err.15|undefined reference to `Person::setlastName(char*)' help!!!

    Quote Originally Posted by elenaq13 View Post
    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

  4. #4
    Join Date
    Nov 2014
    Posts
    13

    Re: class peson err.15|undefined reference to `Person::setlastName(char*)' help!!!

    Quote Originally Posted by VictorN View Post
    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
    Name:  PROG04.exe_.jpg
Views: 666
Size:  13.8 KB

  5. #5
    Join Date
    Nov 2014
    Posts
    13

    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.

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,620

    Re: class peson err.15|undefined reference to `Person::setlastName(char*)' help!!!

    Quote Originally Posted by elenaq13 View Post
    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?

  7. #7
    Join Date
    Nov 2014
    Posts
    13

    Re: class peson err.15|undefined reference to `Person::setlastName(char*)' help!!!

    It's in build messages. I am using CodeBlocks

  8. #8
    Join Date
    Nov 2014
    Posts
    13

    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.

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,620

    Re: class peson err.15|undefined reference to `Person::setlastName(char*)' help!!!

    Quote Originally Posted by elenaq13 View Post
    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.

  10. #10
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,486

    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++20 Compiler: Microsoft VS2019 (16.10.0)

  11. #11
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,486

    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++20 Compiler: Microsoft VS2019 (16.10.0)

  12. #12
    Join Date
    Nov 2014
    Posts
    13

    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.

  13. #13
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,486

    Re: class peson err.15|undefined reference to `Person::setlastName(char*)' help!!!

    Quote Originally Posted by elenaq13 View Post
    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++20 Compiler: Microsoft VS2019 (16.10.0)

  14. #14
    Join Date
    Nov 2014
    Posts
    13

    Re: class peson err.15|undefined reference to `Person::setlastName(char*)' help!!!

    Quote Originally Posted by 2kaud View Post
    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.

  15. #15
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,486

    Re: class peson err.15|undefined reference to `Person::setlastName(char*)' help!!!

    Yeah.... Have a look at main.cpp
    Code:
    }
    }
    You have an extra } at the end of your code
    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++20 Compiler: Microsoft VS2019 (16.10.0)

Page 1 of 2 12 LastLast

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)