gpt4 book ai didi

c++ - 抽象类类型的无效新表达式

转载 作者:IT老高 更新时间:2023-10-28 21:43:54 30 4
gpt4 key购买 nike

我目前正在为大学类(class)编写光线追踪器。为了从文件中加载场景,我编写了一个 sdfloader 来读取 sdf 文件并为其创建场景。

如果我现在想编译加载器,我会收到以下错误:

rc/sdf_loader.cpp: In member function 'void SDFloader::add_shape(std::istringstream&)':
src/sdf_loader.cpp:95:58: error: invalid new-expression of abstract class type 'box'
&scene_.materials[mat]));
^

我试图找到解决方案但失败了。

sdf_loader 类如下所示:

class SDFloader {
public:
SDFloader();
~SDFloader();

Scene const& scene() const;
void read(std::string file);

private:
void add_material(std::istringstream&);
void add_shape(std::istringstream&);
void add_light(std::istringstream&);
void add_camera(std::istringstream&);
void apply_transformation(std::istringstream&);

private:
Scene scene_;
};

在我的 sdf 加载器实现中,我编写了 read() 方法:

void SDFloader::add_shape(std::istringstream& iss) {

std::string name;
iss >> name;

if(name == "box") {
double x1,y1,z1,x2,y2,z2;
std::string mat;
iss >> name >> x1 >> y1 >> z1 >> x2 >> y2 >> z2 >> mat;
scene_.shapes.insert(new box(point(x1,y1,z1),
point(x2,y2,z2),
name,
&scene_.materials[mat]));
}

对于每个其他形状,相同的调用。

我的代码中的问题在哪里?真的没看出来

我正在使用 g++-4.9 - std=c++0x 来编译和链接所有内容

最佳答案

invalid new-expression of abstract class type 'box'

错误消息没有什么不清楚的地方。您的类 box 至少有一个未实现的成员,这意味着它是抽象的。你不能实例化一个抽象类。

如果这是一个错误,请通过实现缺少的成员来修复您的盒子类。

如果是设计使然,则从 box 派生,实现缺少的成员并使用派生类。

关于c++ - 抽象类类型的无效新表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23827014/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com