【问题标题】:c++ initializing Errorc++初始化错误
【发布时间】: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


【解决方案1】:

通常undefined表示编译器找不到Coordinate.cpp文件。您是否检查了使链接器将Coordinate.cpp 文件链接到执行文件的项目设置?

【讨论】:

    【解决方案2】:

    第一个错误是由于拼写错误的 Coordinate。也可以修复第二个。

    【讨论】:

      【解决方案3】:

      修正你的错字:

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

      Coodinate 应该是Coordinate

      【讨论】:

      • @Alireza 您能否向我们展示新的编译器输出以查看错误?它在我的系统上编译得很好(由于某种原因不得不将 NULL 更改为 0)。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-25
      • 2019-07-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多