繁体   English   中英

C ++中的合成错误

[英]Composition error in c++

C ++的新手。 我正在尝试创建2个类,并使用Composition使其相互链接,但是我一直遇到错误。

#include <iostream>
#include <string>
using namespace std;

class student
{
    public: int roll_no;
    string name;
    string dob;
    void student_display()
    {
        cout<<roll_no<< "  "<<name<<"  "<<dob<<endl;
    }
    student(int roll,string names,string dateofb)
    {
        roll_no=roll;
        name=names;
        dob=dateofb;
    }
    ~student(){};
};

class course
{
    public:string course_name;
   int  duration;

     void course_display()
    {
        cout<<course_name<< "  "<<duration<<"  "<<endl;

            s1.student_display()<<endl;
        }
    course (string c_name,int dur,student s2):course_name(c_name),duration(dur),s1(s2){}
    ~course(){};
 private : student s1;

};

class college
{
   public: string college_name;
    string location;
    course c1;
    course (string col_name,string loc,course c2):college_name(col_name),location(loc),c1(c2){}

    ~course(){};
};
int main() {
    student s5(001,"Noel","28/04/1994");
    s5.student_display();
    course c1("Engineering",4,s5(001,"Noel","28/04/1994"));
    c1.course_display();
    return 0;
}

错误如下:

prog.cpp: In member function 'void course::course_display()':
prog.cpp:32:35: error: invalid operands of types 'void' and '<unresolved overloaded function type>' to binary 'operator<<'
             s1.student_display()<<endl;
                                   ^
prog.cpp: At global scope:
prog.cpp:45:20: error: expected ')' before 'col_name'
     course (string col_name,string loc,course c2):college_name(col_name),location(loc),c1(c2){}
                    ^
prog.cpp:47:13: error: declaration of '~course' as member of 'college'
     ~course(){};
             ^
prog.cpp: In function 'int main()':
prog.cpp:52:54: error: no match for call to '(student) (int, const char [5], const char [11])'
  course c1("Engineering",4,s5(001,"Noel","28/04/1994"));

有人可以帮我吗? 我曾尝试过具有相同错误的论坛,但无法弄清楚^

我将代码修改如下:

#include <iostream>
#include <string>
using namespace std;
class student
{
public: int roll_no;
string name;
string dob;
void student_display()
{
    cout<<roll_no<< "  "<<name<<"  "<<dob<<endl;
}
student(int roll,string names,string dateofb)
{
    roll_no=roll;
    name=names;
    dob=dateofb;
}
~student(){};
};

class course
{
public:
string course_name;
int  duration;

private: 
student s1;

public:
void course_display()
{
    cout<<course_name<< "  "<<duration<<"  "<<endl;
    s1.student_display();
}
course (string c_name,int dur,student  s2):course_name(c_name),duration(dur),s1(s2){}
~course(){};

//private: // shifted before course_display()
//  student s1;
};

// Not used
//class college
//{
//public: 
//  string college_name;
//  string location;
//  course c1;
//
//  course (string col_name,string loc,course     c2):college_name(col_name),location(loc),c1(c2){}
//  
//  //~course(){}; // destructor mismatch
//};

int main() {
student s5(001,"Noel","28/04/1994");
s5.student_display();

//course c1("Engineering",4,s5(001,"Noel","28/04/1994"));
course c1("Engineering",4,s5);

c1.course_display();
return 0;
}

输出如下:

在此处输入图片说明

第30行:删除分号。

第45行:看起来像复制/粘贴错误,应该是大学班级的构造函数,对吗? 然后将名称从课程更改为大学。 下一行的析构函数相同。

第52行:您在上面2行中创建了对象s5,我想您想将此对象传递给Course对象的构造函数。 如果是这样,则只需删除s5之后方括号中的表达式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM