【发布时间】:2017-04-05 14:12:45
【问题描述】:
我想为坐标声明一个类,我试试这个代码:
坐标.h:
typedef unsigned short Short; class Coordinate { private : Short _row; Short _col; public: Coordinate(Short row, Short col); bool operator ==(const Coordinate* other); };坐标.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; }Main.cpp:
#include "Coordinate.h" int main() { Coordinate a( 2,2 ); }
但 Visual Studio 2015 返回此错误:
-
错误 C2079 'a' 使用未定义的类 'Coordinate'
-
错误 C2440“正在初始化”:无法从“初始化程序列表”转换为 'int'
【问题讨论】:
-
this==NULL怎么会是真的? -
没有显示
initializer_list。当您使用大括号初始化语法实例化对象时,会创建一个std::initializer_list。 -
this == NULL当有人写类似 ((Coordinate*)NULL) 时为真,你可以测试它@tadman。 -
@JohnZeng 就是说,坐快车去未定行为小镇的时候。
标签: c++ initializer-list