我正在尝试在for循环中输入一个vector [i],并且它说“<<”会出现问题。
我不是为任何人制作这个,我只是为了练习这样做。我不明白为什么我不能吹捧学生的载体。
来源:
int main()
{
struct STUDENT
{
int id = 0;
string name;
};
int inc = 0;
int numstud;
cout << "Enter number of Students: ";
cin >> numstud;
vector<STUDENT> student;
student.resize(numstud);
for (vector<STUDENT>::size_type i = 0; i < numstud; i++)
{
student[i].id = inc++;
}
for (vector<STUDENT>::size_type i = 0; i < numstud; i++)
{
cout << student[i] << "\n"; //THE ERROR IS RIGHT HERE!!! with the "<<"
}
}
我正在尝试运行时的错误:
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\ostream(978): or 'std::basic_ostream<char,std::char_traits<char>> &std::operator <<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,unsigned char)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\ostream(971): or 'std::basic_ostream<char,std::char_traits<char>> &std::operator <<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,const unsigned char *)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\ostream(964): or 'std::basic_ostream<char,std::char_traits<char>> &std::operator <<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,signed char)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\ostream(957): or 'std::basic_ostream<char,std::char_traits<char>> &std::operator <<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,const signed char *)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\ostream(831): or 'std::basic_ostream<char,std::char_traits<char>> &std::operator <<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,char)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\ostream(784): or 'std::basic_ostream<char,std::char_traits<char>> &std::operator <<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,const char *)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\ostream(746): or 'std::basic_ostream<char,std::char_traits<char>> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,char)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\ostream(699): or 'std::basic_ostream<char,std::char_traits<char>> &std::operator <<<char,std::char_traits<char>> (std::basic_ostream<char,std::char_traits<char>> &,const char *)'
答案 0 :(得分:1)
C ++并没有假设如何打印用户类型。您可以告诉它如何通过operator overloading或打印特定字段:
std::cout << student[i].<field>; //replace <field> with name or id