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-2167rectangle::rectangle()'
D\Q1\Debug/../src/Q1.cpp:120: undefined reference to
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)
rectangleclass has no implementations for the constructorrectangle()nor thedisplay()method - Cory Kramerrectangleconstructor andrectangle::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