【问题标题】:C++ iostream isn't working properlyC++ iostream 无法正常工作
【发布时间】: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; 但你正在做&lt;&lt; s.major &lt;&lt;。确保 getAge 和 getName 不是私有的,并且也是 const。您至少可以先解决这些问题。您也没有在 main 中使用 endl,因此此输出似乎与您的代码不匹配。
  • 您仍然有来自上一个线程的这些错误:Person::Person 在 Student 构造函数中,在重载的 &lt;&lt; 实现中,您需要将专业重命名为 maj。由于我从以前的线程中获得了您的代码,因此在解决这些问题后,此代码对我有用!您唯一缺少的代码是 person.h。如果有错误,你可以分享一下。
  • 不幸的是,唯一能消除大部分错误的是#include 下面的答案。我尝试重新安装 g++,但没有成功。在上一个线程中:gcc.gnu.org/bugzilla/show_bug.cgi?id=17130 向我指出。我开始认为它是编译器

标签: c++ operator-overloading iostream


【解决方案1】:

我从未遇到过&lt;iostream&gt; 没有带来&lt;ostream&gt; 的实现。而且我什至不确定它是否合法(否则常见的"Hello World" 实现,它只包括&lt;iostream&gt; 甚至不起作用)。但这是我能理解的唯一可以解释该错误的原因。

#include <ostream>

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

【讨论】:

  • @ritual_code:你介意为我做一个实验吗?编写一个基本的 hello world 程序,仅包括 iostream:#include &lt;iostream&gt; int main() { std::cout &lt;&lt; "hello world"; } -- 并让我知道它是否有效?另外,你有什么版本的 gcc?
  • 抱歉延迟回复,我收到错误:'cout' is not a member of 'std' =(
  • 编辑:嘿,我通过完全重新安装整个 shabang 解决了这个问题
  • 记录在案:我认为在 C++98/03 中这是合法的,其中(严格来说)&lt;iostream&gt; 不需要包含任何内容(参见例如 stroustrup.com/3rd_issues.html 第 633 页的条目) ,因此通常建议在使用std::endl 时明确#include &lt;ostream&gt;。但在 C++11 中,&lt;iostream&gt; 需要包含 &lt;ostream&gt;(和其他三个标头),标准(N3337、27.4 [iostream.objects])。 (不过,我认为始终明确包含每个符号的“原始”标题是“不错的做法”。)
猜你喜欢
  • 1970-01-01
  • 2011-08-31
  • 2020-08-01
  • 2013-08-26
  • 2010-11-29
  • 2015-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多