[英]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:所以我创建了一个 Person 类,现在我试图将其转换为模板类,但我不断收到以下错误:
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..我对 C++ 很陌生,它有点令人困惑。
here is my code for header and cc header:这是我的 header 和 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.您需要在Person
的定义之前移动operator<<
和operator>>
的声明,否则编译器将不知道它们是友元声明中的模板。 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. PS:最好让operator<<
带上const Person<T>&
并让print
成为一个const
成员函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.