0

My code doesnt compile and I'm really confused.When I run the code I get an error in the header file that says "Declaration of 'T' shadows template parameter. My teacher told us in class to put template prefix template before declaration of the operators inside a class definition. I don't know if she's wrong or not but I tried removing the template that were above the declaration of friend functions and when i tried compiling the code i get some errors that have linker Id issues. One of them refers to Matrix::Matrix(int). Honestly I still think what my teacher said about putting the template prefix above the declaration of the operator is right because the operators have the class type in their parameter but can someone help me out!

In my header file I have:

#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

In my implementation file I have:

#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;
}

In my main file I have:

#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;

}
Brogrammer
  • 191
  • 1
  • 1
  • 10
  • 2
    That's quite a lot of code to peruse in order to find the line that caused your error. Why not provide a fuller error statement? Hopefully one that could pinpoint which line has the problem? – Ami Tavory Jun 26 '15 at 22:16
  • You need the template prefix, because you're referring to template functions. But you probably only want to friend the instantiation for the given type T, so you have to use `template<> friend Matrix operator* // ...` – Daniel Jour Jun 26 '15 at 22:26
  • It's possible that your teacher forgot to mention *where* the template member functions must be defined. – molbdnilo Jun 26 '15 at 22:31

3 Answers3

1

So, you just need do as compiler says. There is nothing interesting and I just put link to fixed sources here: http://rextester.com/NSAPP25204

grisha
  • 1,247
  • 1
  • 14
  • 20
  • did u just put my implementation file into my header file? – Brogrammer Jun 26 '15 at 22:40
  • @guploo, no, I just put all your code together into one translation unit (.cpp file, in a nutshell). Note: if you are using templates you need implement them in header (.h) file not in source (.cpp). – grisha Jun 26 '15 at 23:03
  • how would I create an object with elements inputted from a file? I already have the file with the numbers inside the folder but I dont know where to go from there? – Brogrammer Jun 26 '15 at 23:25
  • are you talking about some file that contains matrix elements ? In your case you have matrix 3x3. Just try to use your `operator>>`. In this case you need to have all 9 elements in one line – grisha Jun 26 '15 at 23:41
  • awesome it worked thanks! I just have one last question though.I tried overloading == but for some reason i keep getting true returned. In public I have it declared template bool operator ==(const Matrix &A,const Matrix &B) { Matrix equality; for(int i=0;i – Brogrammer Jun 27 '15 at 00:19
0

Your problems seems to be that you put the implementation of the template member functions in a separate implementation file (probably .cpp). Put them into the header file vector.h, because every implementation file that wants to use your template class needs to see the implementation of the member functions as well. Note that the compiler has no access to the object files that resulted from earlier compiles.

nv3
  • 400
  • 4
  • 9
0

You have separated out the implementation of Template class in a separate (.cpp) file. When compiler tries to compile Matrix<int> class, it goes through the vector.h file but as this file does not contain the full definition of the class (i.e. the code in your vector.cpp) the template class implementation of Matrix class for int type is incomplete and the constructors and overloaded operators

You can fix this in two different ways:

1) move everything from vector.cpp to vector.h so the entire code gets compiled with the template. or

2) if you still want to have it in separate file then include vector.cpp file also in your main file.

the first way is better way to do it as the entire code for your class is available in the same file.

Mital Vora
  • 2,199
  • 16
  • 19