我正在玩继承。我完全被编译器困惑了 错误我得到并查找它似乎与我正在尝试的完全无关 这样做只是初始化我的课程。
这里是错误:
In file included from main.cpp:2:0:
student.h: In constructor ‘Student::Student(std::string, int, std::string, double)’:
student.h:13:3: error: class ‘Student’ does not have any field named ‘gnu_dev_major’
student.h: In function ‘std::ostream& operator<<(std::ostream&, const Student&)’:
student.h:25:8: error: no match for ‘operator<<’ in ‘os << "Name\011: "’
student.h:25:8: note: candidates are:
student.h:24:16: note: std::ostream& operator<<(std::ostream&, const Student&)
student.h:24:16: note: no known conversion for argument 2 from ‘const char [8]’ to ‘const Student&’
/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:25:32: error: ‘endl’ is not a member of ‘std’
student.h:26:8: error: no match for ‘operator<<’ in ‘os << "Age\011: "’
student.h:26:8: note: candidates are:
student.h:24:16: note: std::ostream& operator<<(std::ostream&, const Student&)
student.h:24:16: note: no known conversion for argument 2 from ‘const char [7]’ to ‘const Student&’
/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:26:30: error: ‘endl’ is not a member of ‘std’
student.h:27:8: error: no match for ‘operator<<’ in ‘os << "Major\011: "’
student.h:27:8: note: candidates are:
student.h:24:16: note: std::ostream& operator<<(std::ostream&, const Student&)
student.h:24:16: note: no known conversion for argument 2 from ‘const char [9]’ to ‘const Student&’
/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:27:34: error: ‘endl’ is not a member of ‘std’
student.h:28:8: error: no match for ‘operator<<’ in ‘os << "GPA\011: "’
student.h:28:8: note: candidates are:
student.h:24:16: note: std::ostream& operator<<(std::ostream&, const Student&)
student.h:24:16: note: no known conversion for argument 2 from ‘const char [7]’ to ‘const Student&’
/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:28:30: error: ‘endl’ is not a member of ‘std’
main.cpp: In function ‘int main()’:
main.cpp:8:2: error: ‘cout’ was not declared in this scope
令我困惑的是我得到的第一个错误:
student.h:在构造函数'Student :: Student(std :: string,int, std :: string,double)':student.h:13:3:错误:类'学生'没有 有任何名为'gnu_dev_major'的字段
这是我的代码:
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 &,const Student &);
10 public:
11 Student(std::string name, int age, std::string m="undecided", double gpa=0.0) :
12 Person::Person(name,age),
13 major(m),
14 gpa(gpa)
15 {}
16
17
18
19 protected:
20 double gpa;
21 std::string major;
22 };
23
24 std::ostream & operator<<(std::ostream &os,const Student &s){
25 os << "Name\t: " << s.name << std::endl;
26 os << "Age\t: " << s.age << std::endl;
27 os << "Major\t: " << s.major << std::endl;
28 os << "GPA\t: " << s.gpa << std::endl;
29 }
30
31 #endif
如果有人指出我的重载可能出错的地方 运算符&lt;&lt;对此的帮助也很感激=)。
答案 0 :(得分:0)
你需要声明你的重载&lt;&lt;作为你班上朋友的操作员:
在您的Student类中声明如下:
class Student {
// rest of code
friend std::ostream & operator<<(std::ostream &os,const Student &s);
}