2016-11-22 60 views
-3

我要声明的坐标一类,我试试这个代码:C++初始化错误

Coordinate.h:

typedef unsigned short Short; 
class Coordinate 
{ 
private : 
    Short _row; 
    Short _col; 
public: 
    Coordinate(Short row, Short col); 
    bool operator ==(const Coordinate* other); 
}; 

Coordinate.cpp:

#include "Coordinate.h" 

Coordinate::Coordinate(Short row, Short col) 
    : _row(row) , _col(col){} 

bool Coordinate::operator== (const Coordinate* other) 
{ 
    if (other == NULL || this == NULL) 
     return false; 
    if (this == other) 
     return true; 
    if (other->_row != this->_row || other->_col != this->_col) 
     return false; 
    return true; 
} 

主。 cpp:

#include "Coordinate.h" 
int main() 
{ 
    Coordinate a(2,2); 
} 

但视觉工作室2015年返回此错误:

  • 错误C2079 'A' 使用未定义类 '坐标'

  • 错误C2440 '初始化':无法从 '初始化列表' 转换为 '诠释'

+7

'this == NULL'将如何成为真实? – tadman

+0

没有显示'initializer_list'。使用加载初始化语法实例化对象时,会创建一个“std :: initializer_list”。 –

+0

当这个人写((Coordinate *)NULL)时,'this == NULL'是真的,你可以测试它@tadman。 –

回答

4

解决您的错字在:

#include "Coordinate.h" 
int main() 
{ 
    Coodinate a(2,2); 
} 

Coodinate应该是Coordinate

+0

我修正了它,但问题没解决 – Alireza

+2

@Alireza你能告诉我们新的编译器输出来查看错误吗?它在我的系统上编译得很好(不过由于某种原因,必须将NULL更改为0)。 – BUCH

+1

在C++中,您[应该使用'nullptr'而不是'NULL'](http://stackoverflow.com/a/13816448/1848578)。 – qxz

0

第一个错误是由于拼写错误Coo r dinate。可能还会修复第二个。

0

通常undefined表示编译器找不到Coordinate.cpp文件。你有没有检查过你的项目设置,使链接器链接Coordinate.cpp文件到你的执行文件?