Thread: simple inheritance problem please help:P

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    4

    simple inheritance problem please help:P

    Hi, I´m new to c++ and inheritance therefore I have a problem with this code. Please help :P

    I want to create two classes ("leaf" and "internal") that inherit from "node". I removed everything from the code to reproduced the error.
    When I call a function that uses "node" in "leaf.cpp" a link error occurs :S

    The error is:
    Linking...
    leaf.obj : error LNK2019: unresolved external symbol "public: void __thiscall node::test(class node *)" (?test@node@@QAEXPAV1@@Z) referenced in function "public: void __thiscall leaf::test(class node *)" (?test@leaf@@QAEXPAVnode@@@Z)
    C:\Documents and Settings\MiguelDeVezEmQuando\My Documents\Visual Studio 2005\Projects\tree\Debug\tree.exe : fatal error LNK1120: 1 unresolved externals
    Build log was saved at "file://c:\Documents and Settings\MiguelDeVezEmQuando\My Documents\Visual Studio 2005\Projects\tree\tree\Debug\BuildLog.htm"
    tree - 2 error(s), 0 warning(s)



    I got three ".h"s
    Node.h
    Code:
     
    #ifndef nodeH
    #define nodeH
    
    class node {
    	public:
    	node* k;
    	void test(node* in);
    };
    
    #endif
    Leaf.h
    Code:
     
    #include "node.h"
    
    #ifndef leafH
    #define leafH
    
    class leaf : public node {
    public:
    void test(node* in);
    };
    
    #endif
    Internal.h
    Code:
     
    #include "node.h"
    
    #ifndef internalH
    #define internalH
    
    class internal : public node{
    	public:
    	node* j;
    	void test(node* in);
    };
    
    #endif
    And one CPP
    Leaf.h
    Code:
     
    #include "leaf.h"
    
    #ifndef leafCPP
    #define leafCPP
    
    
    void leaf::test(node* in)
    {
    	in->test(in);
    }
    
    int main()
    {
    	return 0;
    }
    
    #endif

  2. #2
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    where's your implementation of node::test?

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It looks like you never actually defined the node's test member function. To begin with, the test member function should be declared as virtual in the node class, and possibly be a pure virtual if you are not going to instantiate node objects, but are only going to instantiate objects of derived classes. Speaking of virtual functions, node should have a virtual destructor.

    By the way, there is no need to use inclusion guards for source files since source files are generally never included.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    4
    Thanks for the tips laserlight, the code looks cleaner using virtual.

    The solution for this lnk2019 error is solved (Hurray) with a virtual destructor in class Node (though I dont understand why :S).

    Another problem immediatly follows in my code. When I declare a object leaf and try to instanciate it.

    the error:
    leaf.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall node::test(class node *)" (?test@node@@UAEXPAV1@@Z)
    C:\Documents and Settings\MiguelDeVezEmQuando\My Documents\Visual Studio 2005\Projects\tree\Debug\tree.exe : fatal error LNK1120: 1 unresolved externals

    In LEAF.CPP
    Code:
    #include "leaf.h"
    
    void leaf::test(node* in)
    {
    	leaf* nova = new leaf();
    	in->test(in);
    }
    
    int main()
    {
    	return 0;
    }
    Now Leaf.H has a constructor and destructor
    Code:
    #include "node.h"
    
    #ifndef leafH
    #define leafH
    
    class leaf : public node {
    public:
    	int k;
    	leaf(){};
    	~leaf(){};
    	void test(node* in);
    };
    
    #endif
    help?
    Last edited by miguel811; 02-18-2009 at 11:22 AM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by miguel811
    with a virtual destructor in class Node (though I dont understand why :S)
    You could read this answer to the FAQ: When should my destructor be virtual?

    Quote Originally Posted by miguel811
    When I declare a object leaf and try to instanciate it.
    Did you link the object files together? If you are not using an IDE or build script, or using an IDE but without putting the files in a project, you may end up compiling each source file, but when you build the executable program, you might neglect to link the object files so the linker complains that it cannot build the executable program, even though all the object files have been individually built.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Did you implement node::test?
    Implement all the member functions in your classes to avoid linking errors. Even if they are just stubs.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    4
    I thing the files are well linked because I am using a win32 console aplication in visual studio 2005 and all files are in the same project so I think there is no problem finding the files.

    Implementing node::test solved the problem for this code! Thanks

    But for my real code something is missing because the problem continues (I implemented all virtual functions in node). When I use the constructor for "leaf" the linker still fails.

    Now I am considering use malloc to assign space to a leaf :S that would suck.

    Anyone has a idea?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by miguel811
    But for my real code something is missing because the problem continues (I implemented all virtual functions in node). When I use the constructor for "leaf" the linker still fails.
    You should implement all functions, not just the ones declared virtual (though implementing a pure virtual function is optional, and you can get away with not implementing a function that is never used).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    4
    It´s working It was a function that wasnt implemented (in node) and I did not notice. Now I can finish my Btree.

    Thank you laserlight, and thank you Elysia!

    Hurray for classes inheritance!

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    For future reference, let's look at the error again. It's very informative, actually.
    leaf.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall node::test(class node *)"
    "unresolved external symbol" basically means the linker could not find a variable or function.
    And...
    virtual void __thiscall node::test(class node *)
    Tells us that the function in question is the member function test inside class node that takes a node* as argument.

    Possible reasons as to why it cannot find it is that...
    1) You have a function which you forgot to implement (ie, you simply forgot or you misspelled the name).
    2) Misspelled variable.
    3) All source files weren't compiled properly (can sometimes happen; a rebuild will likely solve that in VS).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple File I/O problem
    By Ignited in forum C++ Programming
    Replies: 3
    Last Post: 01-07-2006, 10:49 AM
  2. Fairly simple problem
    By fatdunky in forum C Programming
    Replies: 1
    Last Post: 11-14-2005, 11:34 PM
  3. Simple Variable Problem
    By Cthulhu in forum C++ Programming
    Replies: 2
    Last Post: 11-11-2005, 04:07 PM
  4. Problem with overloaded operators, templates and inheritance
    By bleakcabal in forum C++ Programming
    Replies: 1
    Last Post: 03-19-2004, 05:07 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM