【问题标题】:Error when trying to overload output operator尝试重载输出运算符时出错
【发布时间】:2012-11-18 22:15:38
【问题描述】:

我正在学习 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:\users\matt\documents\visual studio 2010\Projects\lab4\Debug\lab4.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);
}

【问题讨论】:

    标签: c++ linker-errors outputstream


    【解决方案1】:

    您的整个源代码是否真的位于单个文件中,如此处发布的? 您是否知道,您必须在其标题中定义模板类的函数成员? (其实这只是最简单的解决方案,有一些变通方法)

    参考: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&lt;&lt;不是matrix类的成员。

    更新。

    你必须在类中声明你所有的友元函数为

    template <int f_m, int f_n> 
    friend ostream& operator<<(ostream &os, const Matrix<f_m,f_n> &rhs);
    

    它们是独立的模板,它们的模板参数必须是独立的。奇怪的是,MSVC++ 没有发出那个错误。 gcc 说:

    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) 
    

    【讨论】:

    • 我使用 rhs.data 编辑更改了我的代码。但是,这并没有解决我的问题。我不知道与标头有什么关系,我该怎么做呢?
    • @user906153 只需将有关模板的所有来源放在其头文件中即可。并更新朋友声明,然后我的 gcc 会在没有警告的情况下编译源代码。这个链接可能会很有帮助:parashift.com/c++-faq/separate-template-fn-defn-from-decl.html
    猜你喜欢
    • 2014-03-18
    • 2021-10-29
    • 1970-01-01
    • 2018-09-17
    • 1970-01-01
    • 1970-01-01
    • 2012-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多