繁体   English   中英

类型&#39;std :: vector的非常量引用的无效初始化 <Object*> &”

[英]invalid initialization of non-const reference of type ‘std::vector<Object*>&’

开始使用C ++,因为我想将raytracer从Python转换为C ++。

无论如何,我试图用g++编译我的raytracer,但出现此错误:

In file included from engine.cpp:10:0:
objects.cpp: In function ‘Vector Trace(Ray&, std::vector<Object*>&, float, int)’:
objects.cpp:97:30: error: conversion from ‘Object*’ to non-scalar type ‘Object’ requested
objects.cpp:110:29: error: conversion from ‘Object*’ to non-scalar type ‘Object’ requested
engine.cpp: In function ‘int main(int, char**)’:
engine.cpp:36:55: error: invalid initialization of non-const reference of type ‘std::vector<Object*>&’ from an rvalue of type ‘std::vector<Object*>*’
objects.cpp:86:8: error: in passing argument 2 of ‘Vector Trace(Ray&, std::vector<Object*>&, float, int)’

我知道所有这些错误都与我的objects变量有关,因为我不确定如何创建对象数组并从函数内部正确使用它。

这是我的main()的一部分:

vector<Object*> objects;

Sphere sphere = Sphere();
sphere.pos = Vector(0, 0, 0);
sphere.radius = 1;
sphere.diffuse = Vector(1, 1, 1);

objects.push_back(&sphere);

Trace()的减速:

Vector Trace(Ray &ray, vector<Object*> &objects, float roulette, int n = 0) {

Sphere的声明如下:

class Sphere: public Object {
  public:

我真的不确定该怎么做,因为我已经尝试过调整所有关于vector<>东西!

编辑

这是第97行:

Object target = objects[i];

您没有包括出现问题的行。

objects.cpp:97您正在执行以下操作:

Object x = objects[0];

这是行不通的,因为objectsObject *的向量

使用例如以下之一:

Object * x = objects[0]; // x points to the actual Object in your vector
Object x = *objects[0]; // x is a copy of the Object in your vector
Object & x = *objects[0]; // x is a reference to/alias of the actual Object in your vector

在第二个错误上,您尝试传递vector<Object*> * ,其中的vector<Object*>是预期的。 不传递&objects ,而是传递objects

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM