C ++错误构造函数参数 [英] C++ error constructor parameter

查看:330
本文介绍了C ++错误构造函数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在玩继承。我完全感到困惑的编译器
错误我得到,看起来它似乎完全不相关的,我试图
做,这只是初始化我的类。

i'm playing around with inheritance. I'm completely baffled by the compiler error i'm getting and looking it up it seems completely unrelated to what i'm trying to do which is simply initialize my class.

这里是错误:

    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

令人困惑的是我得到的第一个错误:

whats confusing me is the very first error i'm getting:


student.h:在构造函数'Student :: Student(std :: string,int,
std :: string,double)':student.h: 13:3:错误:类'学生'不
有任何字段命名为'gnu_dev_major'

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’

  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

有一个指针,在我重载的
运算符<<帮助那也是赞赏=)。

also if anyone has a pointer as to what might be going wrong in my overloaded operator << help with that is also appreciated =).

推荐答案

您需要声明重载的<

You need to declare your overloaded << operator as friend inside your class:

在你的Student类中声明如下:

Inside your Student class declare it as follows:

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

这篇关于C ++错误构造函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆