【发布时间】:2017-02-02 10:46:38
【问题描述】:
我这里有一个可以正常工作的数字类:
数字.hpp
#ifndef NUMBER_HPP
#define NUMBER_HPP
#include <memory>
class Number
{
private:
std::unique_ptr<int[]> mDigits;
public:
// CONSTRUCTORS \\
Number();
};
#endif
数字.cpp
#include "number.hpp"
#define PRECISION 2048
Number::Number()
:mDigits( new int[PRECISION]() )
{
}
当我添加以下运算符时
数字.hpp
#ifndef NUMBER_HPP
#define NUMBER_HPP
#include <memory>
class Number
{
private:
std::unique_ptr<int[]> mDigits;
public:
// CONSTRUCTORS \\
Number();
// CONST OPERATORS \\
bool operator==( Number const& rhs ) const;
bool operator!=( Number const& rhs ) const;
};
#endif
数字.cpp
#include "number.hpp"
#define PRECISION 2048
Number::Number()
:mDigits( new int[PRECISION]() )
{
}
bool Number::operator==( Number const& rhs ) const
{
for( int i = 0; i < PRECISION; ++i )
if( mDigits[i] != rhs.mDigits[i] )
return false;
return true;
}
bool Number::operator!=( Number const& rhs ) const
{
return !( *this == rhs );
}
我从 GCC 5.4、GCC 6.2 和 CLANG idk 收到以下错误
number.cpp:5:16: error: definition of implicitly declared constexpr Number::Number()
Number::Number()
error: number.cpp:12 no bool Number::operator==( const Number& rhs ) const member function declared in class Number
类中的每个方法等等。这里发生了什么?
【问题讨论】:
-
签名不应该是
bool operator==( const Number& rhs ) const;吗? -
我只是把它们调换一下,看看这种方式是否可行。我认为它们是可以互换的。但两者都不起作用。
-
不要总结不起作用的代码。
//same as before经常隐藏错误。发布显示错误的实际代码。 -
编辑过的number.cpp
-
您可以尝试删除 \\ 吗?
标签: c++ gcc compiler-errors