Hi, I got a bug in the following C++ code: ||=== Build: Debug in lab9 (compiler:
ID: 666439 • Letter: H
Question
Hi, I got a bug in the following C++ code:
||=== Build: Debug in lab9 (compiler: GNU GCC Compiler) ===|
objDebugShape.o||In function `ZN5ShapeC2Ev':|
C:Documents and SettingsAdministratordesktoplab9shape.h|11|undefined reference to `vtable for Shape'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
Please debug it for me, thank you very much!
shape.h
#ifndef SHAPE_H_INCLUDED
#define SHAPE_H_INCLUDED
#include
#include
#include
#include
using namespace std;
class Shape
{
protected:
float base;
float height;
float width;
float radius;
float area;
public:
virtual void getlength();
virtual void printarea();
};
class Triangle : public Shape{
public:
void getlength() {
cout << "Input Your Triangle's base length ";
cin >> base;
cout << "Input Your Triangle's height length ";
cin >> height;
}
void printarea() {
area = 0.5 * base * height;
cout << "Your triangle's area is "<< area <<" ";
}
};
class Rectangle : public Shape{
public:
void getlength() {
cout << "Input Your Rectangle's width length ";
cin >> width;
cout << "Input Your Rectangle's height length ";
cin >> height;
}
void printarea() {
area = width * height;
cout << "Your Rectangle's area is "<< area <<" ";
}
};
class Circle : public Shape{
public:
void getlength() {
cout << "Input Your Cirlce's radius length ";
cin >> radius;
}
void printarea() {
area = 3.14 *radius*radius;
cout << "Your Circle's area is "<< area <<" ";
}
};
#endif // SHAPE_H_INCLUDED
shape.cpp
#include "shape.h"
int main()
{
vector vect;
vector myvec;
int choice;
while (true){
cout<<" Area of Different Shapes:";
cout<<" 1. Area of a Triangle";
cout<<" 2. Area of a Rectangle";
cout<<" 3. Area of a Circle";
cout<<" 4. Exit";
cout<<" Enter your choice: ";
cin>>choice;
if (choice == 1){
Triangle *tri = new Triangle();
vect.push_back(tri);
myvec.push_back(1);
}
else if (choice == 2){
Rectangle *rect = new Rectangle();
vect.push_back(rect);
myvec.push_back(2);
}
else if (choice == 3){
Circle *cir = new Circle();
vect.push_back(cir);
myvec.push_back(3);
}
else if (choice == 4)
break;
else{
cout << "Enter the number between (1 and 4) both included";
}
}
for (int i = 0; i < vect.size(); i++){
vect[i]->getlength();
vect[i]->printarea();
cout << endl << endl << endl;
}
}
Explanation / Answer
now changed.
vectors are not declared correctly.
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class Shape
{
protected:
float base;
float height;
float width;
float radius;
float area;
public:
virtual void getlength();
virtual void printarea();
};
class Triangle : public Shape{
public:
void getlength() {
cout << "Input Your Triangle's base length ";
cin >> base;
cout << "Input Your Triangle's height length ";
cin >> height;
}
void printarea() {
area = 0.5 * base * height;
cout << "Your triangle's area is "<< area <<" ";
}
};
class Rectangle : public Shape{
public:
void getlength() {
cout << "Input Your Rectangle's width length ";
cin >> width;
cout << "Input Your Rectangle's height length ";
cin >> height;
}
void printarea() {
area = width * height;
cout << "Your Rectangle's area is "<< area <<" ";
}
};
class Circle : public Shape{
public:
void getlength() {
cout << "Input Your Cirlce's radius length ";
cin >> radius;
}
void printarea() {
area = 3.14 *radius*radius;
cout << "Your Circle's area is "<< area <<" ";
}
};
int main()
{
vector<int> vect;
vector<int> myvec;
int choice;
while (true){
cout<<" Area of Different Shapes:";
cout<<" 1. Area of a Triangle";
cout<<" 2. Area of a Rectangle";
cout<<" 3. Area of a Circle";
cout<<" 4. Exit";
cout<<" Enter your choice: ";
cin>>choice;
if (choice == 1){
Triangle *tri = new Triangle();
vect.push_back(tri);
myvec.push_back(1);
}
else if (choice == 2){
Rectangle *rect = new Rectangle();
vect.push_back(rect);
myvec.push_back(2);
}
else if (choice == 3){
Circle *cir = new Circle();
vect.push_back(cir);
myvec.push_back(3);
}
else if (choice == 4)
break;
else{
cout << "Enter the number between (1 and 4) both included";
}
}
for (int i = 0; i < vect.size(); i++){
vect[i]->getlength();
vect[i]->printarea();
cout << endl << endl << endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.