体系结构的未定义符号x86_64:" Shape :: get_area()",引用自:shapeable for shape in shape.o

时间:2014-12-02 02:39:47

标签: c++ xcode macos polymorphism linker-errors

嗨,我很确定我的问题是愚蠢的,但我无法弄清楚它对我的生活是什么。我有这个家庭作业,这主要是为了强化我们在课堂上学到的关于多态的知识(顺便说一下,这就是C ++)。该程序的基础是一个称为形状的类,它是圆形,三角形和矩形的父类。

我在纯子方法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;

};

然后在Circle.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)

2 个答案:

答案 0 :(得分:0)

您的代码在使用g ++版本4.8.2的Ubuntu上编译没有问题:

$ g++ -o c Circle.cpp Shape.cpp
$ ./c
area=28.274334

int main()
{ 
  Circle c ("white", 3);
  printf("area=%lf\n", c.get_area());
  return 0;
}

你在Circle.h的末尾省略了#endif,而shape.h应该命名为Shape.h。 但是你如何编译和链接代码?可能是你没有全部 这个程序中的各个部分?

答案 1 :(得分:0)

消息中缺少的vtable位表示Shape.cpp未被链接。由于您正在使用IDE,在编译源代码时代表您进行配置,因此可能无法编译Shape.cpp