Thread: Linker Error

  1. #1
    C++ Beginner
    Join Date
    Jun 2005
    Posts
    39

    Linker Error

    I have never had this problem before, and now i'm adding classes to my RPG, and all of the sethp,mp,etc says [Linker error] undefined reference to `User::sethp(int)' or something like that. What's wrong?
    I'm a beginner C++ programmer, but I have studied HTML and Java. So if you need to help me I should catch on fast =)

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You're calling User::sethp(int), and it isn't declared.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You may have declared a method for your class but you lack the implementation for it, as an example:

    Code:
    class myclass
    {
    public:
        void bar(void);
    };
    
    int main()
    {
        myclass foo;
        foo.bar();
        return 0;
    }
    The class has a method called bar declared but there is no implementation for it anywhere. You would need to add the implementation:

    Code:
    void myclass::bar(void)
    {
        ...
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    C++ Beginner
    Join Date
    Jun 2005
    Posts
    39
    yea just went back in some tuts and found that out. I learned inhertiance and some poly, and i didn't know something like that, dumb.
    I'm a beginner C++ programmer, but I have studied HTML and Java. So if you need to help me I should catch on fast =)

  5. #5
    C++ Beginner
    Join Date
    Jun 2005
    Posts
    39
    oh I get it now... I thought u didn't have to do that

    I thought u could just sethp();

    Code:
    class User 
    public:
    User();
    ~User();
    int gethp() { return hp }
    void sethp(int hp);
    private:
    int hp
    }
    doesn't work as an example?
    I'm a beginner C++ programmer, but I have studied HTML and Java. So if you need to help me I should catch on fast =)

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    class User
    {
    public:
    User();
    ~User();
    int gethp() { return hp; }
    void sethp(int hp);
    private:
    int hp;
    };
    Well, aside from the missing semicolons and bracket, that would need the implementation for the sethp method for it to work. You could either include the implementation as you've done with the gethp method (as a part of the class declaration) or you would need to define it elsewhere.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM