【发布时间】:2013-11-16 23:29:56
【问题描述】:
使用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”。 感谢任何帮助。
【问题讨论】:
-
您忘记输入
), s3(。 -
那些错误信息非常难以阅读。您可以在不重新格式化它们的情况下重新粘贴它们(除了作为代码缩进)吗?
-
std::string maj;但你正在做<< s.major <<。确保 getAge 和 getName 不是私有的,并且也是 const。您至少可以先解决这些问题。您也没有在 main 中使用 endl,因此此输出似乎与您的代码不匹配。 -
您仍然有来自上一个线程的这些错误:
Person::Person在 Student 构造函数中,在重载的<<实现中,您需要将专业重命名为 maj。由于我从以前的线程中获得了您的代码,因此在解决这些问题后,此代码对我有用!您唯一缺少的代码是 person.h。如果有错误,你可以分享一下。 -
不幸的是,唯一能消除大部分错误的是#include
下面的答案。我尝试重新安装 g++,但没有成功。在上一个线程中:gcc.gnu.org/bugzilla/show_bug.cgi?id=17130 向我指出。我开始认为它是编译器
标签: c++ operator-overloading iostream