我使用 C++11 标准编写了以下代码:
.h file:
#include "Auxiliaries.h"
class IntMatrix {
private:
Dimensions dimensions;
int *data;
public:
int size() const;
IntMatrix& operator+=(int num);
};
我得到的错误是:
错误:重载运算符“+”的使用不明确(操作数类型
'const mtm::IntMatrix' 和 'int')
返回矩阵+标量;
知道导致这种行为的原因以及如何解决它吗?
您在中声明了运算符mtm
命名空间,因此定义应该位于mtm
命名空间。
由于您在外部定义它们,因此实际上有两个不同的功能:
namespace mtm {
IntMatrix operator+(IntMatrix const&, int);
}
IntMatrix operator+(IntMatrix const&, int);
当你这样做时matrix + scalar
in operator+(int, IntMatrix const&)
,两个函数均已找到:
- 命名空间中的一个通过参数相关查找 https://en.cppreference.com/w/cpp/language/adl.
- 全局命名空间中的一个,因为您位于全局命名空间中。
您需要定义operator
s 在您声明它们的名称空间中,mtm
:
// In your .cpp
namespace mtm {
IntMatrix operator+(IntMatrix const& matrix, int scalar) {
// ...
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)