【问题标题】:C++ "Missing vtable" error despite virtual functions defined尽管定义了虚函数,但 C++“缺少 vtable”错误
【发布时间】:2021-12-13 13:12:33
【问题描述】:

我有两个名为SpherePlane 的类都继承自抽象类Shape。 Sphere 文件正在工作,所以我基本上复制粘贴它们并用 Plane 替换 Sphere,但现在它抛出了这个错误:

Undefined symbols for architecture x86_64:
   "vtable for rt::Plane", referenced from:
      rt::Plane::Plane() in Scene.cpp.o
    NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [raytracer] Error 1
make[1]: *** [CMakeFiles/raytracer.dir/all] Error 2

这是一种幼稚的做事方式吗?我对 C++ 很陌生,所以请多多包涵。这是我的文件:

形状.h

#ifndef SHAPE_H_
#define SHAPE_H_

#include "core/RayHitStructs.h"
#include "core/Material.h"

namespace rt{

class Shape{
public:
    Shape(){};
    virtual ~Shape(){};

    virtual Hit intersect(Ray)=0;

protected:
    Material * material;

};
}
#endif

Sphere.h

#ifndef SPHERE_H_
#define SPHERE_H_

#include "math/geometry.h"
#include "core/RayHitStructs.h"
#include "core/Shape.h"

namespace rt{

class Sphere:public Shape{

public:
    Sphere(){};
    Sphere(Vec3f center, float radius):center(center), radius(radius){};

    virtual ~Sphere(){};
    Hit intersect(Ray ray);

private:
    Vec3f center;
    float radius;

};
} 
#endif

Sphere.cpp

#include "Sphere.h"

namespace rt{
    Hit Sphere::intersect(Ray ray){
        Hit h;
        return h;
    }
}

平面.h

#ifndef PLANE_H
#define PLANE_H

#include "math/geometry.h"
#include "core/RayHitStructs.h"
#include "core/Shape.h"

namespace rt {
    class Plane:public Shape {

    public:
        Plane(){};
        virtual ~Plane(){};
        Hit intersect(Ray ray);

    };
}

#endif

Plane.cpp

#include "Plane.h"

namespace rt{
    Hit Plane::intersect(Ray ray){
        Hit h;
        return h;
    }
}

编辑:这是我运行make VERBOSE=1时的跟踪

/usr/local/Cellar/cmake/3.21.3_1/bin/cmake -E cmake_link_script CMakeFiles/raytracer.dir/link.txt --verbose=1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++  -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk -mmacosx-version-min=10.15 -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/raytracer.dir/cameras/Pinhole.cpp.o CMakeFiles/raytracer.dir/cameras/ThinLens.cpp.o CMakeFiles/raytracer.dir/core/Camera.cpp.o CMakeFiles/raytracer.dir/core/LightSource.cpp.o CMakeFiles/raytracer.dir/core/Material.cpp.o CMakeFiles/raytracer.dir/core/RayTracer.cpp.o CMakeFiles/raytracer.dir/core/Scene.cpp.o CMakeFiles/raytracer.dir/core/Shape.cpp.o CMakeFiles/raytracer.dir/lights/AreaLight.cpp.o CMakeFiles/raytracer.dir/lights/PointLight.cpp.o CMakeFiles/raytracer.dir/main/main.cpp.o CMakeFiles/raytracer.dir/materials/BlinnPhong.cpp.o CMakeFiles/raytracer.dir/shapes/BVH.cpp.o CMakeFiles/raytracer.dir/shapes/Sphere.cpp.o CMakeFiles/raytracer.dir/shapes/TriMesh.cpp.o CMakeFiles/raytracer.dir/shapes/Triangle.cpp.o -o raytracer 

【问题讨论】:

  • 编译链接代码的命令是什么?
  • @ChrisMM 我在 MacOS 上,我只输入make,我在第一次构建它时输入了cmake
  • 如果 makefile 是由 cmake 生成的,请尝试运行 make VERBOSE=1 -- 这应该会在运行时显示命令。
  • @G.M.谢谢你,我已经运行了它并用跟踪更新了帖子!
  • 您似乎没有与Plane.cpp.o 链接。

标签: c++ inheritance vtable


【解决方案1】:

事实证明,我的编译器没有看到 Plane.cpp,因此认为 intersect() 函数未定义。当我再次运行cmake 时,它起作用了。当我在头文件Plane.h 中定义intersect() 函数时,它也暂时起作用。

【讨论】:

    猜你喜欢
    • 2020-04-09
    • 2013-02-22
    • 1970-01-01
    • 1970-01-01
    • 2014-10-12
    • 2017-12-08
    • 2021-07-21
    • 2015-01-18
    相关资源
    最近更新 更多