0
votes

It gives error when I make object of rectangle type and says undefined reference. Kindly resolve. I might be doing some mistake as my concept in virtual functions is quite vague

#include <iostream>
#include <cmath>
#include <conio.h>
using namespace std;


class shape{
protected:
string type;
float width;
float height;
public:
shape(){
    type = "shape";
    width = 0;
    height = 0;
}

string getType(){
    return type;
}

float getWidth(){
    return width;
}

float getHeight(){
    return height;
}

void setType(string t){
    type = t;
}

void setWidth(float w){
    width = w;
}

void setHeight(float h){
    height = h;
}

virtual float area() = 0;
virtual void display(){
    cout<<"Type : "<<type;
    cout<<"Width :"<<width;
    cout<<"Height :"<<height;
}
};


class rectangle:public shape{
public:
rectangle();
void display();
float area(){
    int A;
    A = width*height;
return A;
}
};



int main(){

rectangle rec;
rec.setHeight(4);
rec.setWidth(5);
rec.display();

}

Error it gives:

04:36:04 **** Incremental Build of configuration Debug for project Q1 ****  

make all 'Building file: ../src/Q1.cpp' 'Invoking: Cross G++ Compiler' g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/Q1.d" -MT"src/Q1.o" -o "src/Q1.o"
"../src/Q1.cpp" 'Finished building: ../src/Q1.cpp' ' ' 'Building target: Q1' 'Invoking: Cross G++ Linker' g++ -o "Q1" ./src/Q1.o
./src/Q1.o: In function main': F:\Semester 2\OOP\Theory\Assignment5- Composition and Inheritance\19I-2167
D\Q1\Debug/../src/Q1.cpp:120: undefined reference to
rectangle::rectangle()'
F:\Semester 2\OOP\Theory\Assignment5- Composition and Inheritance\19I-2167
D\Q1\Debug/../src/Q1.cpp:123:
undefined reference to rectangle::display()' ./src/Q1.o:Q1.cpp:(.rdata$.refptr._ZTV9rectangle[.refptr._ZTV9rectangle]+0x0): undefined reference to vtable for rectangle' collect2.exe: error: ld returned 1 exit status make: *** [makefile:47: Q1] Error 1 "make all" terminated with exit code 2. Build might be incomplete.

04:36:06 Build Failed. 3 errors, 0 warnings. (took 2s.243ms)
1
Your rectangle class has no implementations for the constructor rectangle() nor the display() method - Cory Kramer
Hard to understand what you think the error is. The linker tells you that you haven't defined rectangle constructor and rectangle::display, and you haven't. This doesn't have anything to do with virtual functions, if you declare a function (or constructor) and you use it, then you must define it. - john
Since you don't appear to need them, the simple solution would be to delete the declarations for rectangle::rectangle() and rectangle::display(). - john

1 Answers

3
votes

Both inheritance and the presence of pure virtual functions are not the problem. You'd get similar error for

class rectangle {
public:
    rectangle();
    void display();
    float area(){
        int A = 42;
        return A;
    }
};


int main(){

    rectangle rec;
    rec.display();

}

You declare a constructor and you declare a method display and the compiler takes that as a promise that you will provide a definition of them somewhere. The linker cannot find them, hence the error. You have to define those methods.

However, in your code actually both methods are not needed. You can use the compiler generated constructor and you can reuse shape::display, hence you can change rectangle to:

class rectangle : public shape{
public:
    float area(){
        int A;
        A = width*height;
        return A;
    }
};