【问题标题】:C++ error constructor parameterC++ 错误构造函数参数
【发布时间】:2013-11-16 22:27:52
【问题描述】:

我在玩继承。我对编译器完全感到困惑 我得到的错误并查找它似乎与我正在尝试的完全无关 要做的就是初始化我的类。

以下是错误:

    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: error: class ‘Student’没有 有任何名为“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

如果有人知道我的重载可能出了什么问题 操作员

【问题讨论】:

  • 只是一个猜测-gnu 标头之一#definemajor 是什么?如果您将班级的 major 变量更改为其他名称,问题会消失吗?
  • Person::Person(name,age) 是错误的(我认为),当然Person(name,age) 是调用基类构造函数的常用方法。
  • user1118321:天哪,确实有效。虽然它很奇怪,但我在 main 中声明了一个名为 major 的字符串,并且效果很好。可能是什么问题?
  • john: Person::Person(name,age) 与 Person(name,age) 相同。不过你的权利,我不需要范围解析。
  • @ritual_code 见this(和this)。在您的系统上,&lt;iostream&gt;(和/或&lt;string&gt;)间接包含sys/sysmacros.h,其中包含一个邪恶的#define major(dev) gnu_dev_major(dev)(因此major(m) 被损坏但不是例如std::string major;)。显然,这可以在 C 模式下使用编译器选项 -ansi 避免,但这在 C++ 模式下不起作用。我觉得有点难以下咽……

标签: c++ operator-overloading


【解决方案1】:

你需要在你的类中声明你重载的

在 Student 类中声明如下:

class Student {
    // rest of code
    friend std::ostream & operator<<(std::ostream &os,const Student &s);
}

【讨论】:

  • 也需要内联声明。
  • 你的意思是在课堂上实现它?我在我的班级中声明操作员
  • 你不应该在课堂上实现,我刚刚注意到,你需要在你的&lt;&lt; 实现中实现return os;
  • 不,很遗憾这没有帮助 =(
  • 我包含了返回操作系统并使其内联,到目前为止“student.h:33:8: error: no match for 'operator
猜你喜欢
  • 1970-01-01
  • 2014-07-06
  • 1970-01-01
  • 2012-03-25
  • 2021-10-24
  • 1970-01-01
  • 1970-01-01
  • 2012-04-18
相关资源
最近更新 更多