C++初始化错误
问题描述:
我要声明的坐标一类,我试试这个代码: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 '初始化':无法从 '初始化列表' 转换为 '诠释'
答
第一个错误是由于拼写错误Coo r dinate。可能还会修复第二个。
答
通常undefined
表示编译器找不到Coordinate.cpp
文件。你有没有检查过你的项目设置,使链接器链接Coordinate.cpp
文件到你的执行文件?
'this == NULL'将如何成为真实? – tadman
没有显示'initializer_list'。使用加载初始化语法实例化对象时,会创建一个“std :: initializer_list”。 –
当这个人写((Coordinate *)NULL)时,'this == NULL'是真的,你可以测试它@tadman。 –