Thread: ABC question

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    127

    ABC question

    If I have an ABC with a non-default, non-virtual constructor that has an implementation and 2 virtual functions, 1 pure virtual, and 1 not pure virtual with no implementation and I have code like:

    Code:
    MyABC *foo[10];
    when I compile and link, during linking there is a vtable symbol error.

    If I comment out the non-default, non-virtual constructor's implementation, it links. Why would it link? Shouldn't it still complain about the non-pure virtual function with no implementation?

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Post the smallest example program that demonstrates the issue.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    127
    Quote Originally Posted by oogabooga View Post
    Post the smallest example program that demonstrates the issue.
    It's so simple, didn't think I needed one.

    Code:
    class hello
    {
    public:
    	hello(int x);
    	
    	virtual void hello1() = 0;
    	
    	virtual void hello2();
    	
    	
    private:
    	int val;
    };
    
    hello::hello(int x) : val(x)
    {
    	
    }
    
    int main(void)
    {
    	hello *myhello[10];
    	
    	return 0;
    }
    If you comment out the constructor implementation. It links. Or if you assign 0 to hello2, it links.

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Strange... the error does not appear if the constructor is defined inline.

    I think gcc gets confused about something but whether it is a bug... or a well defined feature.. I have no idea.

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    It's so simple, didn't think I needed one.
    Indeed. But code is more trustworthy than an explanation in words. And why should I have to code up your example myself, especially since you may have left something out of your explanation? ALWAYS post a code example.

    For instance, it also links if you define the ctor inline, which you did not mention.

    Still, I don't know what's going on here....
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    127
    hello2 should be set to 0, though, correct? Or does it not actually matter?

  7. #7
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by homer_3 View Post
    Or does it not actually matter?
    Somewhat correct... imo.
    The only difference is that different errors will be shown if any of the derived classes do not define it.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, I notice that you never actually create any objects of the class, so all you needed was a forward declaration of the class.
    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
    Jun 2005
    Posts
    6,815
    The output of some compilers, when given an ABC with a non-pure virtual function declared but not defined, does confuse some linkers.

    In your case, define (i.e. implement) hello::hello2(). It doesn't matter if its definition is inlined within the class definition or not.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-23-2011, 09:00 AM
  2. *szString = things question/memory question
    By Jang in forum C Programming
    Replies: 3
    Last Post: 01-20-2011, 04:59 AM
  3. Newbish Question file reading question....
    By kas2002 in forum C Programming
    Replies: 23
    Last Post: 05-17-2007, 12:06 PM
  4. Self regiserting DLLs question and libraries question.
    By ApocalypticTime in forum Windows Programming
    Replies: 2
    Last Post: 03-22-2003, 02:02 PM
  5. A question of color...(not a racial question)
    By Sebastiani in forum Windows Programming
    Replies: 7
    Last Post: 01-15-2003, 08:05 PM