建筑x86_64的未定义符号:"Shape::get_area()",引用自:Shape.o 中的形状可投票。
undefined symbols for architecture x86_64: "Shape::get_area()", referenced from: votable for shape in shape.o
嗨,我很确定我的问题很愚蠢,但我无法弄清楚它对我的生活有什么影响。我有这个家庭作业,基本上是为了加强我们在课堂上学到的关于多态性的知识(顺便说一下,这是C++(。该程序的基础是一个名为 shape 的类,它是圆形、三角形和矩形的父级。
我收到纯虚拟方法 get_area(( 的链接器错误,该方法应在子类中定义。我一生都无法弄清楚为什么它不会编译,我什至还没有制作使用它的主要方法,它本质上是一个原型方法,它不应该链接到任何东西对吗?
无论如何,这是shape.h文件的代码:
#ifndef Assign8_Shape_h
#define Assign8_Shape_h
#include <iostream>
#include <stdio.h>
#include <string>
using namespace::std;
class Shape{
private:
string color;
public:
Shape(const string& n_color);
virtual ~Shape ();
void print();
virtual double get_area() = 0;
};
#endif
然后这里是 Shape.cpp 文件:
#include <stdio.h>
#include <string>
#include "Shape.h"
using namespace::std;
Shape::Shape(const string& n_color) {
color = n_color;
}
Shape::~Shape (){
}
void Shape::print() {
cout << color;
}
下面是如何在 Circle.h 中覆盖有问题的方法的示例:
#ifndef __Assign8__Circle__
#define __Assign8__Circle__
#include <stdio.h>
#include <string>
#include "Shape.h"
using namespace::std;
class Circle : public Shape
{
public:
Circle(const string& n_color, int n_radius);
void print();
double get_area();
private:
int radius;
};
然后在圆圈中.cpp:
#include "Circle.h"
using namespace::std;
Circle::Circle(const string& n_color, int n_radius) : Shape(n_color) {
radius = n_radius;
}
void Circle::print(){
Shape::print();
cout << ", radius " << radius << " area " << get_area();
}
double Circle::get_area() {
double pi = 3.14159265359;
return pi * (radius * radius);
}
然后这是错误的完整正文:
Ld /Users/username/Library/Developer/Xcode/DerivedData/CSCI241-dhkihmradodwdkerbhwjkshakexl/Build/Products/Debug/Assign8 normal x86_64
cd /Users/username/Documents/NIU/CSCI241/Assign8
export MACOSX_DEPLOYMENT_TARGET=10.9
/Applications/Utilities/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -arch x86_64 -isysroot /Applications/Utilities/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk -L/Users/username/Library/Developer/Xcode/DerivedData/CSCI241-dhkihmradodwdkerbhwjkshakexl/Build/Products/Debug -F/Users/username/Library/Developer/Xcode/DerivedData/CSCI241-dhkihmradodwdkerbhwjkshakexl/Build/Products/Debug -filelist /Users/username/Library/Developer/Xcode/DerivedData/CSCI241-dhkihmradodwdkerbhwjkshakexl/Build/Intermediates/Assign8.build/Debug/Assign8.build/Objects-normal/x86_64/Assign8.LinkFileList -mmacosx-version-min=10.9 -stdlib=libc++ -Xlinker -dependency_info -Xlinker /Users/username/Library/Developer/Xcode/DerivedData/CSCI241-dhkihmradodwdkerbhwjkshakexl/Build/Intermediates/Assign8.build/Debug/Assign8.build/Objects-normal/x86_64/Assign8_dependency_info.dat -o /Users/username/Library/Developer/Xcode/DerivedData/CSCI241-dhkihmradodwdkerbhwjkshakexl/Build/Products/Debug/Assign8
Undefined symbols for architecture x86_64:
"Shape::get_area()", referenced from:
vtable for Shape in Shape.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
您的代码在 Ubuntu 上使用 g++ 版本 4.8.2 编译时没有问题:
$ g++ -o c Circle.cpp Shape.cpp
$ ./c
area=28.274334
int main()
{
Circle c ("white", 3);
printf("area=%lfn", c.get_area());
return 0;
}
您在 Circle.h 末尾省略了一个 #endif,shape.h 应该命名为 Shape.h。但是您如何编译和链接代码?难道你没有拥有一切程序的片段在过程中?
消息中缺少 vtable 位表明Shape.cpp
未链接。由于您正在使用在编译源代码时代表您配置此项的 IDE,因此Shape.cpp
可能未编译。
相关文章:
- 将对象数组的引用传递给函数
- 什么时候在C++中返回常量引用是个好主意
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 何时在引用或唯一指针上使用移动语义
- 如何在c++中使用引用实现类似python的行为
- 编译C++时未定义的引用
- Ctypes wstring通过引用传递
- c++r值引用应用于函数指针
- 理解c++中的引用
- C++取消引用指针.为什么会发生变化
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- 强制转换为引用类型
- 引用一个已擦除类型(void*)的指针
- 向量元素的引用地址与它所指向的向量元素的地址不同.为什么
- 具有默认值的引用获取函数
- 如何使用基类指针引用派生类成员
- 使用取消引用的指针的多态性会产生意外的结果.为什么?
- 建筑x86_64的未定义符号:"Shape::get_area()",引用自:Shape.o 中的形状可投票。