建筑x86_64的未定义符号:"Shape::get_area()",引用自:Shape.o 中的形状可投票。

undefined symbols for architecture x86_64: "Shape::get_area()", referenced from: votable for shape in shape.o

本文关键字:Shape 引用 未定义 x86 符号 get 建筑 area      更新时间:2023-10-16

嗨,我很确定我的问题很愚蠢,但我无法弄清楚它对我的生活有什么影响。我有这个家庭作业,基本上是为了加强我们在课堂上学到的关于多态性的知识(顺便说一下,这是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可能未编译。