簡體   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