尝试过载输出运算符时出错
Error when trying to overload output operator
我正在学习C++,但在构建项目时遇到了链接错误。
该错误与我的代码重载输出<<运算符有关。
这是我收到的错误:
1>lab4.obj : error LNK2019: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Matrix<2,2> const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABV?$Matrix@$01$01@@@Z) referenced in function _main
1>c:usersmattdocumentsvisual studio 2010Projectslab4Debuglab4.exe : fatal error LNK1120: 1 unresolved externals
如何修复此错误?我在下面附上了我的代码
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
template <int m, int n>
class Matrix{
public:
int data[m][n];
public:
Matrix(){};
void setup(){
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
data[i][j] = 0;
}
}
}
void setAll(int integer){
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
data[i][j] = integer;
}
}
}
void output(){
for(int i=0;i<m;i++){
cout << "[ ";
for(int j=0;j<n;j++){
cout << data[i][j] << " ";
}
cout<<"]"<<endl;
}
}
Matrix<m,n> operator+ (const Matrix<m,n> &rhs)
{
Matrix<m,n> result;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
result.data[i][j] = this->data[i][j] + rhs.data[i][j];
}
}
return result;
}
Matrix<m,n> operator- (const Matrix<m,n> &rhs)
{
Matrix<m,n> result;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
result.data[i][j] = this->data[i][j] - rhs.data[i][j];
}
}
return result;
}
template <int l>
Matrix <m,l> operator*(const Matrix<n,l> &rhs) //const
{
//only if number of columns in the first matrix equals the number of rows in the second matrix.
Matrix<m,l> result;
for(int i = 0; i < m; i++){
for(int j = 0 ; j < l ; j++){
result.data[i][j] = 0;
for(int k = 0; k < n; k++){
result.data[i][j] += this->data[i][k] * rhs.data[k][j];
}
}
}
return result;
};
Matrix <m,n> operator*(int scalar){
Matrix <m,n> result;
for(int i= 0; i < m; i++){
for(int j = 0; j < n; j++){
result.data[i][j] = this->data[i][j] * scalar;
}
}
return result;
};
friend ostream& operator<<(ostream &os, const Matrix<m,n> &rhs);
friend Matrix <m,n> operator++(const Matrix<m,n> &rhs, int);
friend Matrix <m,n> operator--(const Matrix<m,n> &rhs, int);
};
template <int m, int n>
ostream& operator<< (ostream &os, const Matrix<m,n> &rhs){
for(int i=0;i<m;i++){
os << "[ ";
for(int j=0;j<n;j++){
os << rhs.data[i][j] << " ";
}
os<<"]"<<endl;
}
return os;
}
template <int m, int n>
Matrix <m,n> operator++(const Matrix<m,n> &rhs, int){
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
rhs.data[i][j]++;
}
}
}
template <int m, int n>
Matrix <m,n> operator--(const Matrix<m,n> &rhs, int){
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
rhs.data[i][j]--;
}
}
}
int main(){
Matrix<2,2> m = Matrix<2,2>();
m.setup();
//m.output();
Matrix<2,2> p = Matrix<2,2>();
p.setup();
Matrix<2,2> z = Matrix<2,2>();
z = m + p;
//z.output();
z = z * z;
cout << z;
string stopper;
getline(cin, stopper);
}
您的整个源代码是否真的位于一个文件中,如此处发布?您是否知道必须在模板类的标头中定义其函数成员?(实际上,这只是最简单的解决方案,有一些解决方法)
编号: http://www.parashift.com/c++-faq/separate-template-fn-defn-from-decl.html
如果不是,则可能是错误的原因。
顺便说一句,这个来源是错误的:
template <int m, int n>
ostream& operator<< (ostream &os, const Matrix<m,n> &rhs){
for(int i=0;i<m;i++){
os << "[ ";
for(int j=0;j<n;j++){
os << data[i][j] << " ";
}
os<<"]"<<endl;
}
return os;
}
data
这里没有声明,它必须被引用为 rhs.data
,因为这个operator<<
不是类matrix
的成员。
更新。
你必须将类中的所有友元函数声明为
template <int f_m, int f_n>
friend ostream& operator<<(ostream &os, const Matrix<f_m,f_n> &rhs);
它们是单独的模板,它们的模板参数必须是单独的。奇怪的是,MSVC++没有发出该错误。海湾合作委员会 说:
matrix.cc:91:67: warning: friend declaration «std::ostream& operator<<(std::ostream&, const Matrix<m, n>&)» declares a non-template function [-Wnon-template-friend]
matrix.cc:91:67: note: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here)
相关文章:
- 在 myVector 类中重载运算符 + 时出错
- 使用运算符调用 void 函数时出错<set>
- 通过 [] 运算符访问unordered_map时出错
- 在重载 + 和 - 运算符时出错
- 重载运算符时出错<在 sf::Vector 中
- 将运算符<<与隐式转换的非基本数据类型一起使用时出错
- Rcpp/C++/R:比较日期时间矢量与日期时间时出错(错误:"运算符>"的不明确重载)
- 令牌之前缺少二进制运算符 "#" 当 #define 在 #if 内时出错
- 在运算符中将联合作为参数传递时出错<<
- 迭代器关系运算符出错(带单独链接和迭代器的自定义哈希表)
- 尝试重载模板类友元<<运算符时出错
- 使用 getline 重载 istream 运算符>> 会出错
- 灵气:用期望运算符替换序列时出错
- 在类外部定义显式运算符 bool() 时出错
- 擦除矢量中的对象时出错.如何将移动作业运算符添加到我的类中
- 过载运算符出错>>
- 在使用C (Microsoft vs)中使用指针和新运算符时出错
- 在C++中复制矢量时,矢量运算符出错=
- 具有std::原子成员变量的类的复制构造函数/赋值运算符出错
- JavaScript到C++的转换:%运算符出错