所以我在一个简单的坐标系统长时间休息之后今天写了一些C++,我想我想要一个构造函数来接受2个值,这样我就可以写出像"Coordinates c = new Coordinates(1,2);"这样的东西.
struct Coordinates {
int x;
int y;
Coordinates(int a, int b) {
x = a;
y = b;
}
};
Run Code Online (Sandbox Code Playgroud)
在Cygwin中编译时,我得到:
$ g ++ -o adventure adventure.cpp adventure.cpp:36:错误:没有匹配函数来调用`Coordinates :: Coordinates()'adventure.cpp:22:注意:候选者是:Coordinates :: Coordinates(const Coordinates&)冒险.cpp:26:注意:Coordinates :: Coordinates(int,int)
不知道这里出了什么问题,我在C++ struct构造函数上找不到太多信息.有任何想法吗?
在代码的第36行(您没有显示),您正在创建此类的对象,但您没有将任何参数传递给构造函数.唯一有效的构造函数是带有两个int的构造函数,或者是默认的复制构造函数.添加任一个构造的w/o参数,或改变代码传递a和b给构造.