1

The following code give me linker error:

#include <iostream>

using namespace std;

class CBase
{
public:
    virtual void myfunc();

    CBase()
    {
        // constructor
    }

};

class CMyclass: public CBase
{
public:

    CMyclass(): CBase()
    {
        // constructor
    }

    void myfunc()
    {
    }

};

auto create_class()-> CMyclass
{
    return CMyclass();
}

int main()
{
    // auto sys_conrollable = create_class();
    CMyclass a();
    return 0;
}

Running compiler is fine:

g++ -g  -Wfatal-errors  -std=c++11 main.cpp -c -o main.o

The linker:

g++ -g  -Wfatal-errors  -std=c++11 main.o -o run

gives this error:

main.o:(.rodata._ZTI8CMyclass[_ZTI8CMyclass]+0x10): undefined reference to `typeinfo for CBase' collect2: error: ld returned 1 exit status

Removing create_class function fixes the error. In original code, create_class will be a template calling a template class. So please do not tell me to remove auto!

barej
  • 1,330
  • 3
  • 25
  • 56
  • @πάνταῥεῖ Irrelevant link – barej Jan 20 '15 at 20:54
  • 2
    @πάνταῥεῖ I assume that you even did not understand the question at all. Its a specific question and you link to a general question. It is like liking to a page talking about what is programming! Having 20K reputation does not make you right. – barej Jan 20 '15 at 21:03
  • How does [this answer](http://stackoverflow.com/a/12574403/1413395) from the linked duplicate not explain your problem? [Elaborate in your question please](http://stackoverflow.com/posts/28054426/edit) how your case isn't answered there. Did you want to declare an abstract function like `virtual void myfunc() = 0;` in your `CBase` class? – πάντα ῥεῖ Jan 20 '15 at 21:27
  • @barej As you can see, by now, πάντα ῥεῖ was spot on (kudos for being fast). Sadly the duplicate banner text doesn't link to the particular answer (I'm assuming this is to prevent people from directing traffic to their own answers in preference of other answers). This makes the linked duplicate a bit daunting. I'll keep it in mind for my own "moderation" tasks, that I'll leave a comment when I fear the duplicate might not be "obvious". Cheers – sehe Jan 20 '15 at 21:37
  • 1
    People _are_ linking (read: marking as so-called "duplicate" ... LOL) far too much to huge "catch-all" questions just because one of the answers might answer the same question when parsed and analysed a certain way. THIS IS NOT WHAT SO IS FOR!!! Might as well just shut it down and start a new DeweyDecimal.Com website that sends people to the right place in their library. – Lightness Races in Orbit Jan 20 '15 at 22:59

1 Answers1

2

You haven't defined (provided a body for)

virtual void myfunc();

It is true that you don't have to define functions you don't use, but that is not true for non-pure virtual functions. Either make the function pure virtual ( = 0) or provide a definiton.

Incidentally,

CMyclass a();

doesn't do what you think it does. This does not define an object of type CMyClass. This declares a function which returns CMyclass. Get rid of the parentheses.

Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434