0

How should I declare and use overloaded istream and ostream? This is my code:

in .hh file:

    template <typename TypWart>
class matrix{
    private:
    bool poprawny;
    int rozmiar;
    TypWart *tab;
    TypWart *wyniki;
    TypWart *wyrazy;
    wektor *z;
public:
    void alokuj();
    void gauss();
    TypWart rozwiaz();
    void wypisz();
    void zamien(int k, int l);
    void sprawdz();
    zespolona& operator[](int x){
        return tab[x];
    }
    wektor pomnoz(wektor W, wektor A);
    wektor odejm(wektor W, wektor A);
    wektor przypisz();
    //matrix skopiuj(matrix M);
    wektor przypisz2();
    //  wektor pierwiastek(wektor W);
    wektor operator*(wektor W);
    template <typename Typ>
    friend istream& operator>>(istream& strm, matrix<Typ>& M);
    template <typename Typ>
    friend ostream& operator<<(ostream& strm, matrix<Typ>& M);

    friend ostream& operator<<(ostream& strm, wektor W);

};

in .ccp file:

template <typename Typ> 
istream& operator>>(istream& strm, matrix<Typ>& M){
  int x,y;
    //strm >> x >> y;
    for(x=0; x<M.rozmiar; x++){
      for(y=0; y<M.rozmiar; y++){
            if(!(strm >> M.tab[y+(x*M.rozmiar)])) return strm;
        }
    }
    for(x=0; x<M.rozmiar; x++) if(!(strm >> M.wyniki[x])) return strm;
    return strm;
}

template <typename Typ> 
ostream& operator<<(ostream& strm, matrix<Typ>& M){
  int x,y;
    //strm << x << " " << y << endl;
    for(x=0; x<M.rozmiar; x++){
      for(y=0; y<M.rozmiar; y++){
            strm << M.tab[y+(x*M.rozmiar)];
            strm << " ";
      }
      strm << endl;
    }
    return strm;
}

And the error I'm getting:

main.o: In function `main':
main.cpp:(.text+0x24): undefined reference to `std::basic_istream<char, std::char_traits<char> >& operator>><zespolona>(std::basic_istream<char, std::char_traits<char> >&, matrix<zespolona>&)'
collect2: ld returned 1 exit status
make: *** [a.out] Error 1

I tried adding <> to the declaration in .hh file but it didn't work. How do I fix this?

wojtaso9
  • 1
  • 4
  • 1
    Incomplete and unanswerable question, but I think giving this a read may help: http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file – user4581301 May 09 '16 at 18:44

1 Answers1

0

In case it's not clear, collect2: ld returned 1 exit status is a linker error, not a compiler error. The linker was asked to find a function by a particular name, and didn't.

What name did it not find? I don't know; your question abbreviated the message:

std::basic_istream<char, std::char_traits

At a guess, the compiler is generating (or would) the code for istream& operator>>(istream& strm, matrix<Typ>& M) -- else your .cpp file wouldn't compile -- but something is amiss with the command line in your compiler/linker invocation.

If you edit your question to show the whole error message, and the command that produced it, you'll have a better chance at getting a better answer.

James K. Lowden
  • 7,574
  • 1
  • 16
  • 31