gpt4 book ai didi

C++ 类型和函数

转载 作者:行者123 更新时间:2023-11-30 00:42:00 25 4
gpt4 key购买 nike

我在编译我的代码时遇到了一些问题 - 它与我传入的类型有关。编译器是这样说的:

R3Mesh.cpp: In copy constructor 'R3Mesh::R3Mesh(const R3Mesh&)':
R3Mesh.cpp:79: error: no matching function for call to 'R3Mesh::CreateHalfEdge(R3MeshVertex*&, R3MeshFace*&, R3MeshHalfEdge*&, R3MeshHalfEdge*&)'
R3Mesh.h:178: note: candidates are: R3MeshHalfEdge* R3Mesh::CreateHalfEdge(const R3MeshVertex*&, const R3MeshFace*&, const R3MeshHalfEdge*&, const R3MeshHalfEdge*&)
R3Mesh.cpp: In constructor 'R3MeshHalfEdge::R3MeshHalfEdge(const R3MeshVertex*&, const R3MeshFace*&, const R3MeshHalfEdge*&, const R3MeshHalfEdge*&)':
R3Mesh.cpp:1477: error: invalid conversion from 'const R3MeshVertex*' to 'R3MeshVertex*'
R3Mesh.cpp:1477: error: invalid conversion from 'const R3MeshFace*' to 'R3MeshFace*'
R3Mesh.cpp:1477: error: invalid conversion from 'const R3MeshHalfEdge*' to 'R3MeshHalfEdge*'
R3Mesh.cpp:1477: error: invalid conversion from 'const R3MeshHalfEdge*' to 'R3MeshHalfEdge*'

这是我定义 R3MeshHalfEdge 的方式:

struct R3MeshHalfEdge {
// Constructors
R3MeshHalfEdge(void);
R3MeshHalfEdge(const R3MeshHalfEdge& half_edge);
R3MeshHalfEdge(const R3MeshVertex*& vertex, const R3MeshFace*& face,
const R3MeshHalfEdge*& opposite, const R3MeshHalfEdge*& next);


R3MeshVertex *vertex;
R3MeshFace *face;
R3MeshHalfEdge *opposite;
R3MeshHalfEdge *next;
int id;
};

这是第一个错误提示的内容:

R3MeshHalfEdge *R3Mesh::
CreateHalfEdge(const R3MeshVertex*& vertex, const R3MeshFace*& face,
const R3MeshHalfEdge*& opposite, const R3MeshHalfEdge*& next)
{
// Create half_edge
R3MeshHalfEdge *half_edge = new R3MeshHalfEdge(vertex, face, opposite, next);

// Set half_edge ID
half_edge->id = half_edges.size();

// Add to list
half_edges.push_back(half_edge);

// Return half_edge
return half_edge;
}

这是第二个错误的提示:

R3MeshHalfEdge::
R3MeshHalfEdge(const R3MeshVertex*& vertex, const R3MeshFace*& face,
const R3MeshHalfEdge*& opposite, const R3MeshHalfEdge*& next)
: vertex(vertex),
face(face),
opposite(opposite),
next(next),
id(0)
{
}

这里是我调用 CreateHalfEdge 函数的地方:

   for(int i=0; i<mesh.NFaces(); i++)
{
R3MeshFace *f = mesh.Face(i);
vector<R3MeshVertex *> face_vertices; // assume vertices are stored in order around the perimeter of the face
for(unsigned int j = 0; j<f->vertices.size(); j++)
{
R3MeshVertex *v1 = f->vertices[j];
R3MeshVertex *v2;
if(j==f->vertices.size()-1)
v2 = f->vertices[0];
else
v2 = f->vertices[j+1];

int v1_id = v1->id;
int v2_id = v2->id;
R3MeshHalfEdge *next = NULL;
R3MeshHalfEdge *opposite = NULL;
R3MeshHalfEdge *half_edge = CreateHalfEdge(v1, f, opposite, next);

}

... }

最佳答案

构造函数错误:

  R3MeshHalfEdge(const R3MeshVertex*& vertex, const R3MeshFace*& face, 
const R3MeshHalfEdge*& opposite, const R3MeshHalfEdge*& next);

您将指针传递给 const 并将它们分配给指向非常量的指针,这失败了。

像这样更正它:

  R3MeshHalfEdge(R3MeshVertex* vertex, R3MeshFace* face, 
R3MeshHalfEdge* opposite, R3MeshHalfEdge* next);

作为备注:

  • const 有两层指针:const 指针(const X*)和 const 指针(X* const) 前者可以指向别的东西,但不能改变指向的对象,后者不能反弹到另一个对象,但可以改变指向的对象。您可以将它们组合起来,使一个 const 指针指向 const (const X* const)
  • 不要通过引用传递指针 (*&),除非您打算修改指针本身,而不是指向的对象。

关于C++ 类型和函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2312231/

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