没有匹配的函数调用Foo::Foo
no matching function for call to Foo::Foo
我相信这个问题以前有人问过。我试着找了一下,但还是找不出问题所在。如果我能得到任何帮助,我将不胜感激。由于
我得到以下错误。我得到一个想法,我没有初始化顶点一旦我调用边缘的构造函数。但我不知道该怎么做。
10369.cpp: In constructor ‘Edge::Edge(Vertex, Vertex)’:
10369.cpp:34: error: no matching function for call to ‘Vertex::Vertex()’
10369.cpp:30: note: candidates are: Vertex::Vertex(int, int)
10369.cpp:6: note: Vertex::Vertex(const Vertex&)
10369.cpp:34: error: no matching function for call to ‘Vertex::Vertex()’
10369.cpp:30: note: candidates are: Vertex::Vertex(int, int)
10369.cpp:6: note: Vertex::Vertex(const Vertex&)
#include <iostream>
#include <math.h>
using namespace std;
//Vertex in a graph, with x & y coordinates
class Vertex{
int x , y;
public:
Vertex(int, int);
~Vertex();
int GetX() {return x;};
int GetY() {return y;};
};
// Edge of a graph calculated from two vertices
class Edge{
Vertex U , V;
float edge_weight;
public:
Edge(Vertex , Vertex);
~Edge();
float GetEdgeWeight() { return edge_weight; }
};
Vertex::Vertex(int _x , int _y){
x = _x;
y = _y;
}
// The edge weight is calculated using the pythagoras
Edge::Edge(Vertex _U , Vertex _V){
U = _U;
V = _V;
int x = V.GetY() - U.GetY();
int y = V.GetX() - U.GetX();
edge_weight = sqrt(pow(x,2) + pow(y,2));
}
int main (int argc , char *argv[]){
Vertex V1(0,1) ;
Vertex V2(0,3);
Edge E(V1, V2);
float x = E.GetEdgeWeight();
cout << x << endl;
return 0;
}
始终使用构造函数初始化列表来初始化数据成员-您的Vertex
没有默认构造函数,这是您绝对必须使用初始化列表的情况之一。
Edge::Edge(Vertex U, Vertex V) : U(U), V(V), edge_weight(...) { }
Vertex::Vertex(int x, int y) : x(x), y(y) { }
您的Vertex
类没有默认构造函数。默认初始化顶点,然后为它们赋值:
Edge::Edge(Vertex _U , Vertex _V){ // BTW _U is an identifier reserved for the implementation
U = _U;
V = _V;
通常不需要默认初始化:
Edge::Edge(Vertex U , Vertex V) : U(U), V(V) {
按如下方法修改Edge
构造函数:
Edge::Edge(Vertex _U , Vertex _V) :
U(_U),
V(_V)
{
int x = V.GetY() - U.GetY();
int y = V.GetX() - U.GetX();
edge_weight = sqrt(pow(x,2) + pow(y,2));
}
相关文章:
- 函数调用中参数的顺序重要吗
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 变量没有改变?通过向量的函数调用
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数
- 我知道函数调用中存在歧义.有没有办法调用foo()函数
- 模板函数调用
- 获取从C++中同一类中的构造函数调用的方法返回的值
- 析构函数调用
- 成员函数调用和C++对象模型
- 使用共享指针的函数调用,其对象应为 const
- C++:编译时检查匹配的函数调用对?
- 函数调用C++中的参数太少
- 来自 DLL 的函数调用 [表观调用的括号前面的表达式必须具有(指向-)函数类型]
- 返回指向对象的指针的函数调用是否为 prvalue?
- C++ 如何重载 [] 运算符并进行函数调用
- 代码的效率. 转到和函数调用
- 为什么我需要双括号在构造函数调用中,如:foo x((bar());
- 让 foo(derived_object) 调用 foo(Base const&) 而不是模板函数?
- 没有匹配的函数调用Foo::Foo