gpt4 book ai didi

c++ - 未定义对 `typeinfo and ' vtable 的引用

转载 作者:行者123 更新时间:2023-11-30 02:00:39 26 4
gpt4 key购买 nike

我目前正在开发一个使用虚函数用户的程序。我只使用了一个虚拟函数,并且遇到了一个似乎是常见问题的常见解决方案,我已经尝试过但不幸的是没有成功。

我最初有 virtual void calcArea();在 BasicShape.h 中没有任何定义或指定为纯虚函数。我更改了它并在末尾添加了 {}(正如另一个有类似问题的线程所建议的那样),但我仍然收到以下错误:

我输入:

g++ BasicShape.h BasicShape.cpp circle.h circle.cpp Rectangle.h Rectangle.cpp driver.cpp -o Lab4

然后我得到:

/tmp/ccf1Y4Br.o: In function `BasicShape::BasicShape()': circle.cpp:(.text._ZN10BasicShapeC2Ev[_ZN10BasicShapeC5Ev]+0xf): undefined reference to `vtable for BasicShape'
/tmp/ccf1Y4Br.o:(.rodata._ZTI6circle[_ZTI6circle]+0x10): undefined reference to `typeinfo for BasicShape'
/tmp/ccc7gjtH.o:(.rodata._ZTI9Rectangle[_ZTI9Rectangle]+0x10): undefined reference to `typeinfo for BasicShape'
collect2: error: ld returned 1 exit status

有什么想法吗?

这是实现文件BasicShape.h:

#ifndef BASICSHAPE_H
#define BASICSHAPE_H

class BasicShape
{
protected:
double area;

public:
double getArea() const;
virtual void calcArea();
};

#endif

随附的 BasicShape.cpp 文件:

#include "BasicShape.h"

double BasicShape::getArea() const
{
return area;
}

void BasicShape::calcArea()
{
}

圆.h:

#include "BasicShape.h"

#ifndef CIRCLE_H
#define CIRCLE_H

class circle : public BasicShape
{
private:
long centerX;
long centerY;
double radius;

public:
circle(long, long, double);
long getCenterX() const;
long getCenterY() const;
virtual void calcArea();
};

#endif

圆.cpp:

#include "circle.h"

// constructor
circle::circle(long userIn, long userIn2, double userIn3)
{
centerX = userIn;
centerY = userIn2;
radius = userIn3;
calcArea();
}

// accesors
long circle::getCenterX() const
{
return centerX;
}

long circle::getCenterY() const
{
return centerY;
}

// virtual function
void circle::calcArea()
{
area = (3.14159 * radius * radius);
}

矩形.h

#include "BasicShape.h"

#ifndef RECTANGLE_H
#define RECTANGLE_H

class Rectangle : public BasicShape
{
private:
long width;
long length;

public:
Rectangle(long, long);
long getWidth() const;
long getLength() const;
virtual void calcArea();
};

#endif

矩形.cpp:

#include "Rectangle.h"

// constructor
Rectangle::Rectangle(long userIn, long userIn2)
{
width = userIn;
length = userIn2;
calcArea();
}

// accessors
long Rectangle::getWidth() const
{
return width;
}

long Rectangle::getLength() const
{
return length;
}

void Rectangle::calcArea()
{
area = (length * width);
}

驱动程序不完整,但无论如何与我的问题无关(至少我是这么认为的)。

#include <cassert>
#include <iostream>
#include "BasicShape.h"
#include "Rectangle.h"
#include "circle.h"
using namespace std;

int main()
{

cout << "Testing the functionality and efficiency of the circle class...\n";
// declare circle object and test accessors and area computation
circle objCircle(8,8,4);
assert(objCircle.getCenterX() == 8);
assert(objCircle.getCenterY() == 8);
assert(objCircle.getArea() == 50.26544);
cout << "Circle object testing completed successfully\n";

cout << "Testing the functionality and efficiency of the Rectangle class...\n";
// declare rectangle object and test accessors and area computation
//Rectangle objRec();

return 0;
}

最佳答案

实际上,正如所指出的,您不必编译 header 。 (虽然你可以,但在这里无关紧要——gcc 将生成预编译头文件)。

更有趣的是:您的示例在这里完美运行,GCC 4.6.3。

此外,旁注:calcArea 不应公开

关于c++ - 未定义对 `typeinfo and ' vtable 的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14927717/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com