ritual_code
ritual_code

Reputation: 147

C++ iostream isn't working properly

using g++ compiler to compile the following program:

  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 }

I'm getting these errors:

> 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

My student.h file:

  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

I've never seen the compiler have these problems before, anyone seen this before?

BTW, this problem was part of this thread where for some reason g++ was complaining about me using a member variable called "major", also a first time for me, i've never before seen the compiler complaining about a variable name, especially 'major'. appreciate any help.

Upvotes: 0

Views: 1194

Answers (1)

Benjamin Lindley
Benjamin Lindley

Reputation: 103693

I've never encountered an implementation where <iostream> did not bring <ostream> with it. And I'm not even sure it's legal for it not to (otherwise the common "Hello World" implementation, which only includes <iostream> wouldn't even work). But that's the only thing I can fathom would explain that error.

#include <ostream>

Try adding that to the top of "student.h".

Upvotes: 1

Related Questions