首页 > 解决方案 > 尽管定义了虚函数,但 C++“缺少 vtable”错误

问题描述

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

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

球体.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

球体.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

平面.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 

标签: c++inheritancevtable

解决方案


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


推荐阅读