简体   繁体   中英

c++ template class unable to fix ostream and istream functions

So I created a Person class that now i'm trying to convert into a template class but I keep getting the following errors:

  error: template-id ‘operator<< <std::vector<int, std::allocator<int> > >’ for ‘std::ostream& operator<<(std::ostream&, Person<std::vector<int, std::allocator<int> > >&)’ does not match any template declaration
      friend std::ostream& operator<<<T>(std::ostream&, Person<T>& m);

error: template-id ‘operator>><std::vector<int, std::allocator<int> > >’ for ‘std::istream& operator>>(std::istream&, Person<std::vector<int, std::allocator<int> > >&)’ does not match any template declaration
  friend std::istream& operator>><T>(std::istream&, Person<T>& lass);

I had a look at similar issues people faced and posted here but I couldn't find a solution that would work for me so I decided to ask my own question. Would mean a lot if you guys can help! I'm very new to C++ and its sort of confusing..

here is my code for header and cc header:

#ifndef _Person_H_
#define _Person_H_

#include <iostream>
#include <sstream>
#include <vector>
template<class T>
class Person{
private:
    vector<T> myVector;

public:
    Person(const T vec);
    T getVector();
    ostream& print_on(std::ostream& o);
    friend std::istream& operator>><T>(std::istream&, Person<T>& lass);
    friend std::ostream& operator<<<T>(std::ostream&, Person<T>& m);
};

template<class T> class Person;
template <class T> std::istream& operator>>(std::istream&, Person<T>&);
template <class T> std::ostream& operator<<(std::ostream&, Person<T>&);

#include "Person.cc"
#endif

and here is my cc file

#include <iostream>
#include <vector>
#include <string>

using namespace std;

template <class T>
Person<T>::Person(const T vec) : myVector(vec)
{ }

template <class T>
T Person <T>::getVector() {
    return myVector;
}

template <class T>
ostream& Person<T>::print(ostream& o) {
    return o << "success ";
}

template <class T>
std::ostream& operator<<(std::ostream& o, Person<T>& m) {
    return m.print_on(o);
}

template <class T>
std::istream& operator >> (istream& input, Person<T>& lass)
{
    vector<T> vectors;
    int Vsize,numbers;
    cout<<"Enter size of vector"<<endl;
    input >> Vsize;
            for (int i = 0; i < Vsize; i++)
            {
                input>> numbers;
                vectors.push_back(numbers);
            }

            lass=Person(vectors);
    return input;
}

You need to move the declaration of operator<< and operator>> before the definition of Person , otherwise the compiler won't know that they're templates in the friend declaration. eg

template<class T> class Person;
template <class T> std::istream& operator>>(std::istream&, Person<T>&);
template <class T> std::ostream& operator<<(std::ostream&, Person<T>&);

template<class T>
class Person{
private:
    vector<T> myVector;

public:
    Person(const T vec);
    getVector();
    ostream& print_on(std::ostream& o);
    friend std::istream& operator>><T>(std::istream&, Person<T>& lass);
    friend std::ostream& operator<<<T>(std::ostream&, Person<T>& m);
};

PS: It'll be better to make operator<< taking const Person<T>& and make print a const member function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM