C ++ iostream无法正常工作

时间:2013-11-16 23:29:57

标签: c++ operator-overloading iostream

使用g ++编译器编译以下程序:

  1 #include <iostream>
  2 #include "student.h"
  3 #include <string>
  4 
  5 using namespace std;
  6 
  7 int main(){
  8         string major("hello");
  9         Student s("Henry Markov", 21), s2("Wallace Piirish", 22, "Computer Science", 2.5);
 10         cout << s << s2;
 11 }

我收到了这些错误:

> Press ENTER or type command to continue In file included from
> main.cpp:2:0: student.h: In function ‘std::ostream&
> operator<<(std::ostream&, const Student&)’: student.h:10:9: error: no
> match for ‘operator<<’ in ‘os << "Name\011: "’ student.h:10:9: note:
> candidate is: /usr/include/c++/4.6/bits/basic_string.h:2693:5: note:
> template<class _CharT, class _Traits, class _Alloc>
> std::basic_ostream<_CharT, _Traits>&
> std::operator<<(std::basic_ostream<_CharT, _Traits>&, const
> std::basic_string<_CharT, _Traits, _Alloc>&) student.h:10:33: error:
> passing ‘const Student’ as ‘this’ argument of ‘std::string
> Person::getName()’ discards qualifiers [-fpermissive] student.h:10:38:
> error: ‘endl’ is not a member of ‘std’ student.h:11:9: error: no match
> for ‘operator<<’ in ‘os << "Age\011: "’ student.h:11:9: note:
> candidate is: /usr/include/c++/4.6/bits/basic_string.h:2693:5: note:
> template<class _CharT, class _Traits, class _Alloc>
> std::basic_ostream<_CharT, _Traits>&
> std::operator<<(std::basic_ostream<_CharT, _Traits>&, const
> std::basic_string<_CharT, _Traits, _Alloc>&) student.h:11:24: error:
> ‘const class Student’ has no member named ‘getAge’ student.h:11:36:
> error: ‘endl’ is not a member of ‘std’ student.h:12:9: error: no match
> for ‘operator<<’ in ‘os << "Major\011: "’ student.h:12:9: note:
> candidate is: /usr/include/c++/4.6/bits/basic_string.h:2693:5: note:
> template<class _CharT, class _Traits, class _Alloc>
> std::basic_ostream<_CharT, _Traits>&
> std::operator<<(std::basic_ostream<_CharT, _Traits>&, const
> std::basic_string<_CharT, _Traits, _Alloc>&) student.h:12:26: error:
> ‘const class Student’ has no member named ‘major’ student.h:12:35:
> error: ‘endl’ is not a member of ‘std’ student.h:13:9: error: no match
> for ‘operator<<’ in ‘os << "GPA\011: "’ student.h:13:9: note:
> candidate is: /usr/include/c++/4.6/bits/basic_string.h:2693:5: note:
> template<class _CharT, class _Traits, class _Alloc>
> std::basic_ostream<_CharT, _Traits>&
> std::operator<<(std::basic_ostream<_CharT, _Traits>&, const
> std::basic_string<_CharT, _Traits, _Alloc>&) student.h:13:31: error:
> ‘endl’ is not a member of ‘std’ main.cpp: In function ‘int main()’:
> main.cpp:10:2: error: ‘cout’ was not declared in this scope

shell returned 1

我的student.h档案:

  1 #ifndef STUDENT_H
  2 #define STUDENT_H
  3 
  4 #include <iostream>
  5 #include <string>
  6 #include "person.h"
  7 
  8 class Student : public Person {
  9         friend std::ostream & operator<<(std::ostream &os,const Student &s){
 10                 os << "Name\t: " << s.getName() << std::endl;
 11                 os << "Age\t: " << s.getAge() << std::endl;
 12                 os << "Major\t: " << s.major << std::endl;
 13                 os << "GPA\t: " << s.gpa << std::endl;
 14                 return os;
 15         }
 16 public:
 17         Student(std::string name, int age, std::string m="undecided", double gpa=0.0) :
 18                 Person::Person(name,age),
 19                 maj(m),
 20                 gpa(gpa)
 21         {}
 22 
 23 
 24 
 25 protected:
 26         double gpa;
 27         std::string maj;
 28 };
 29 #endif

我以前从未见过编译器有这些问题,以前有人见过这个问题吗?

顺便说一句,这个问题是this线程的一部分,由于某种原因,g ++抱怨 关于我使用一个名为“major”的成员变量,对我来说也是第一次,我从来没有见过编译器抱怨变量名,特别是'major'。 感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

我从来没有遇到<iostream>未带来<ostream>的实施。而且我甚至不确定它是否合法(否则常见的"Hello World"实现,只包括<iostream>甚至不起作用)。但这是我唯一能理解的错误。

#include <ostream>

尝试将其添加到"student.h"的顶部。