C++ 未定义的引用和运算符<&书信电报;对我

C++ 未定义的引用和运算符<&书信电报;对我,c++,operator-overloading,undefined-reference,C++,Operator Overloading,Undefined Reference,我试图使用运算符在使用后定义的运算符,这样编译器就不知道在那个点上存在这样的运算符。您需要在使用之前移动运算符的定义,或者最好在定义或声明main()和struct matrix之前声明它: ostream& operator<< (ostream& output, matrix& mat); ostream&operator您的运算符是在使用后定义的,所以编译器不知道在该点上存在这样的运算符。您需要在使用之前移动运算符的定义,或者最好在定义或声明main

我试图使用运算符在使用后定义的运算符,这样编译器就不知道在那个点上存在这样的运算符。您需要在使用之前移动运算符的定义,或者最好在定义或声明
main()
struct matrix
之前声明它:

 ostream& operator<< (ostream& output, matrix& mat);

ostream&operator您的运算符是在使用后定义的,所以编译器不知道在该点上存在这样的运算符。您需要在使用之前移动运算符的定义,或者最好在定义或声明
main()
struct matrix
之前声明它:

 ostream& operator<< (ostream& output, matrix& mat);

ostream&operator这是因为您使用了
operator这是因为您在声明之前使用了
operator。添加声明或将
main
移动到文件底部可以是一个非常慢的方法。考虑使用类似的东西。在声明之前使用它。添加声明或将
main
移动到文件底部可以是一个非常慢的方法。考虑使用类似的东西。
 ostream& operator<< (ostream& output, const matrix& mat);
struct matrix
{
    int** data;       // Pointer to 2-D array that will simulate matrix
    int row, col;

    // Note the output operator should not modify the object.
    // So you can pass it as a const reference in the second parameter.
    friend std::ostream& operator<<(std::ostream& output, matrix const& mat)
    {
        for(int i=0; i < mat.row; ++i)     // prefer prefix increment.
        {
            for(int j=0; j < mat.col; ++j)
            {
                output << mat.data[i][j] << " ";
            }
            output << "\n";
        }
        return output;
    }
};