0

Well I'm facing this problem when my subclass doesn't use a virtual function from superclass.. How to solve it? Thanks in advance for help!

this is my superclass Q header file

#ifndef Q_HPP
#define Q_HPP

#include <iostream>

using namespace std;

class Q
{
protected:
    int w;
public :
    virtual void setW ( int w );
    virtual void display() = 0;
};

#endif

this is my superclass Q source file

#include "Q.hpp"

void Q::setW ( int w )
{
    this-> w = w;
}

this is my subclass R header file

#ifndef R_HPP
#define R_HPP

#include "Q.hpp"

class R : public Q
{
public:
    void display() override;
};

#endif 

this is my subclass R source file

#include "Q.hpp"
#include "R.hpp"

void R::display()
{
    cout << w << endl;
}

error during R.cpp compilation: undefined reference to `Q::setW(int)'

Jess
  • 71
  • 3
  • 10
  • 2
    Please try to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) and show us. And also please show or tell us how you build your program. – Some programmer dude Dec 25 '16 at 08:05
  • 2
    Are you linking all stuff together? – YiFei Dec 25 '16 at 08:10
  • say how do you compile this stuff, provide commands you use, your makefile? – PiotrNycz Dec 25 '16 at 08:15
  • `#include "Q.hpp"` `#include "R.hpp"` `int main()` `{` `Q *qq = new R;` `qq->display();` `}` – Jess Dec 25 '16 at 08:15
  • There's almost certainly something wrong with your compiler and/or linker invocation. Please show us what commands they are fed so we can help. – starturtle Dec 25 '16 at 08:18
  • the comment above is my main.cpp after adding constructor to both classes and the following error arrised during compilation of main.cpp: `undefined reference to vtable for 'Q'` & `undefined reference to vtable for 'R'` – Jess Dec 25 '16 at 08:21
  • That has *nothing* to do with what has been asked for. The command line(s) used to **build** your program is... ? – WhozCraig Dec 25 '16 at 08:24
  • 2
    Not really; that's an IDE that wraps the real toolset, usually gcc or clang. Somewhere in your build logs (not familiar with your IDE anymore; only used it a little years ago) are the commands used to perform the build. Eventually they end with the error you're describing. I'd check there. – WhozCraig Dec 25 '16 at 08:31
  • Getting warmer. There should be a sequence of those; one for each source file compiled. Chances are the collection of them will probably divulge the problem. The one you posted is the *error*. The "what was trying to be done" is going to be interesting. Just collect the build commands and add them to your question, preferably formatted. Thanks. – WhozCraig Dec 25 '16 at 08:39
  • @WhozCraig thanks for your guidance, I just realised that I didn't link all those file in a project and my problem is solved. – Jess Dec 25 '16 at 09:31
  • 1
    That's usually the case when a [undefined reference or unresolved external](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) error crops up. Glad you found it, and more importantly, now you know how to do it again if this ever crops up. Best of luck. – WhozCraig Dec 25 '16 at 09:33

0 Answers0