试图重载运算符并使用类模板时发生C++错误

C++ Error with trying to overload operators and use class template

本文关键字:错误 C++ 重载 运算符      更新时间:2023-10-16

我的代码没有编译,我真的很困惑。当我运行代码时,我在头文件中得到一个错误,上面写着"'T'阴影模板参数的声明。我的老师在课堂上告诉我们,在类定义中的运算符声明之前,先放模板前缀模板。我不知道她是否错了,但我尝试删除了朋友函数声明之上的模板,当我尝试编译代码时,我遇到了一些链接器Id问题的错误。其中一个是指rix::矩阵(int)。老实说,我仍然认为我的老师所说的将模板前缀放在运算符声明之上是正确的,因为运算符的参数中有类类型,但有人能帮我吗!

在我的头文件中,我有:

#ifndef __testing_more_stuff__vector__
#define __testing_more_stuff__vector__
#include<iostream>
#include<fstream>
using namespace std;
template <class T>
class Matrix{
public:
    Matrix();
    Matrix(T diagonal);
    //template <class T>
    friend ostream& operator <<(ostream& outs, const Matrix<T> &obj);
    //template <class T>
    friend istream& operator >>(istream& in, Matrix<T> &obj);
    //template <class T>
    friend Matrix<T> operator *(Matrix<T> A, Matrix<T> B);
private:
    const static int n=3;
    T a[n][n];

};//class declaration
#endif

在我的实现文件中,我有:

#include "vector.h"
template <class T>
Matrix<T>::Matrix(){
    for (int i=0;i<n;i++)
        for (int j=0;j<n;j++)
            a[i][j]=0;
}
template <class T>
Matrix<T>::Matrix(T diagonal){
    for (int i=0;i<n;i++)
        for (int j=0;j<n;j++)
            if (i==j)
                a[i][j]=diagonal;
            else
                a[i][j]=0;
}
template <class T>
ostream& operator <<(ostream& outs, const Matrix<T> & obj)
{
    for (int i = 0; i < obj.n; i++){
        for (int j = 0; j < obj.n; j++)
            outs << " "<< obj.a[i][j];
        outs<<endl;
    }
    outs<<endl;
    return outs;
}
template <class T>
istream& operator >>(istream& in, Matrix<T> & obj)
{
    for (int i = 0; i < obj.n; i++){
        for (int j = 0; j < obj.n; j++){
            in >> obj.a[i][j];
        }
    }
    return in;
}
template<class T>
Matrix<T> operator *(Matrix<T> A, Matrix<T> B){
    Matrix<T> product;
    for (int i=0;i<A.n; i++)
        for (int j=0;j<A.n; j++) {
            T sum=0;
            for (int k=0; k<A.n; k++)
                sum = sum+A.a[i][k]*B.a[k][j];
            product.a[i][j]=sum;
        }
    return product;
}

在我的主文件中,我有:

#include "vector.h"

int main(){
    Matrix<int>  A;
    Matrix<int>  B(2);
    cout << A;
    cout <<B;
    cout <<"stop here";
    ifstream fin;
    ofstream fout;
    fout.open("output.txt");
    if (fout.fail()){
        cout <<"error openning output file";
        exit(1);
    }
    fin.open("input.txt");
    if (fin.fail()){
        cout <<"error openning input file";
        exit(1);
    }
    //input matrix C
    Matrix<int> C;
    cin >>C;
    //fin >>C;
    cout <<"C = "<<endl<<C<<endl;
    cout <<"B = "<<endl<<B<<endl;
    cout <<"C*B = "<<endl<<C*B<<endl;
    //fout <<C;
    //cout <<C.det();
    return 0;
}

所以,您只需要按照编译器所说的去做。没有什么有趣的,我只是在这里放了固定来源的链接:http://rextester.com/NSAPP25204

您的问题似乎是将模板成员函数的实现放在一个单独的实现文件(可能是.cpp)中。将它们放在头文件vector.h中,因为每个想要使用模板类的实现文件也需要查看成员函数的实现。请注意,编译器无法访问早期编译产生的对象文件。

您已经在一个单独的(.cpp)文件中分离出Template类的实现。当编译器试图编译Matrix<int>类时,它会通过vector.h文件,但由于该文件不包含类的完整定义(即vector.cpp中的代码),int类型的Matrix类的模板类实现不完整,构造函数和重载运算符

你可以用两种不同的方法来解决这个问题:

1) 将所有内容从vector.cpp移动到vector.h,以便使用模板编译整个代码。或

2) 如果您仍然想将其放在单独的文件中,那么也可以将vector.cpp文件包含在主文件中。

第一种方法是更好的方法,因为类的整个代码都在同一个文件中。