C++ C++;类型和功能
我在编译代码时遇到了一些问题-这与我传递的类型有关。以下是编译器所说的:C++ C++;类型和功能,c++,function,parameters,types,C++,Function,Parameters,Types,我在编译代码时遇到了一些问题-这与我传递的类型有关。以下是编译器所说的: R3Mesh.cpp: In copy constructor 'R3Mesh::R3Mesh(const R3Mesh&)': R3Mesh.cpp:79: error: no matching function for call to 'R3Mesh::CreateHalfEdge(R3MeshVertex*&, R3MeshFace*&, R3MeshHalfEdge*&, R3Mes
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*'
以下是我如何定义我的半边缘:
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)
{
}
这就是第二个错误所抱怨的:
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);
}
... }
for(inti=0;河流[j];
r3*v2;
如果(j==f->vertices.size()-1)
v2=f->顶点[0];
其他的
v2=f->顶点[j+1];
int v1_id=v1->id;
int v2_id=v2->id;
R3MeshHalfEdge*next=NULL;
R3MeshHalfEdge*对边=空;
R3MeshHalfEdge*half_edge=CreateHalfEdge(v1,f,相反,下一个);
}
... }
您正在将非常量指针传递给一个需要常量指针参数的函数,看起来是这样的
注意:
…但这不应该是一个问题。我怀疑这里还有其他问题,但问题缺少一些信息。编译器错误与复制构造函数R3Mesh::R3Mesh(const R3Mesh&)中的某些内容有关,这在问题中没有显示
编辑:好的,现在显示出来。我建议先清除指针问题的常量引用,然后查看剩下的内容
事实上,第二个错误很明显——R3MeshHalfEdge构造函数的参数不应该是对常量指针的引用(这会告诉编译器您不打算更改它们指向的对象)或者,您将这些参数分配给的数据成员本身应该是常量指针。您正在将非常量指针传递给一个需要常量指针参数的函数 注意: …但这不应该是一个问题。我怀疑这里还有其他问题,但问题缺少一些信息。编译器错误与复制构造函数R3Mesh::R3Mesh(const R3Mesh&)中的某些内容有关,这在问题中没有显示 编辑:好的,现在显示出来。我建议先清除指针问题的常量引用,然后查看剩下的内容
事实上,第二个错误很明显——R3MeshHalfEdge构造函数的参数不应该是对常量指针的引用(这会告诉编译器您不打算更改它们指向的对象)或者,您分配这些参数的数据成员本身应该是常量指针。您需要查看您的参数,并重新评估哪些应该是常量,哪些不应该是常量 <>你的编译器错误是因为你指向一个非常量指针(例如,顶点)在常量内存(顶点参数),在C++中,常量和非常量变量是不同的类型。 通读更多细节 如果您只想让它工作,请删除所有常量,它就会编译。如果您想让参数保持不变,则必须进行复制和数据赋值(而不是指针!)以删除错误
另外,重命名参数,这样就不会与成员变量发生名称冲突。您需要查看参数,并重新评估什么应该是常量,什么不应该是常量 <>你的编译器错误是因为你指向一个非常量指针(例如,顶点)在常量内存(顶点参数),在C++中,常量和非常量变量是不同的类型。 通读更多细节 如果您只想让它工作,请删除所有常量,它就会编译。如果您想让参数保持不变,则必须进行复制和数据赋值(而不是指针!)以删除错误
此外,请重命名参数,以免与成员变量发生名称冲突。构造函数错误:
R3MeshHalfEdge(const R3MeshVertex*& vertex, const R3MeshFace*& face,
const R3MeshHalfEdge*& opposite, const R3MeshHalfEdge*& next);
您将指针传递给const,并将它们分配给指向non-const的指针,这将失败
这样更正:
R3MeshHalfEdge(R3MeshVertex* vertex, R3MeshFace* face,
R3MeshHalfEdge* opposite, R3MeshHalfEdge* next);
作为评论:
- 带有指针的
常量有两个级别:指向
常量的指针(
)和常量指针(const X*
)前者可以指向其他对象,但不能更改所指向的对象;后者不能反弹到另一个对象,但可以更改所指向的对象。您可以将它们组合在一起,使常量指针指向常量(X*const
)const X*const
- 不要通过引用传递指针(
),除非您打算修改指针本身,而不是指向的对象*&
R3MeshHalfEdge(const R3MeshVertex*& vertex, const R3MeshFace*& face,
const R3MeshHalfEdge*& opposite, const R3MeshHalfEdge*& next);
您将指针传递给const,并将它们分配给指向non-const的指针,这将失败
这样更正:
R3MeshHalfEdge(R3MeshVertex* vertex, R3MeshFace* face,
R3MeshHalfEdge* opposite, R3MeshHalfEdge* next);
作为评论:
- 带有指针的
常量有两个级别:指向
常量的指针(
)和常量指针(const X*
)前者可以指向其他对象,但不能更改所指向的对象;后者不能反弹到另一个对象,但可以更改所指向的对象。您可以将它们组合在一起,使常量指针指向常量(X*const
)const X*const
- 不要通过引用传递指针(
),除非您打算修改指针本身,而不是指向的对象*&
常量类型*&参数
意味着您可以修改参数
:
void foo(const int *¶meter)
{
int bar= 0;
parameter= &bar;
}
我怀疑您不想这样做。与其通过引用传递指针,不如通过值传递它们(因为您没有修改它们),或者通过常量引用传递它们(
const-type*const¶meter
)。说const-type*¶meter
意味着您可以修改参数
:
void foo(const int *¶meter)
{
int bar= 0;
parameter= &bar;
}
我怀疑您不想这样做。与其通过引用传递指针,不如通过值传递它们(因为您没有修改它们),或者通过常量引用传递它们(
const type*const¶meter
)。有问题的代码将在R3Mes中