c ++中的组合错误

时间:2016-10-26 05:18:19

标签: c++ oop composition

新手到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"));

有人可以帮帮我吗?我试过通过论坛遇到同样的错误,但无法弄明白                                                       ^

2 个答案:

答案 0 :(得分:1)

我修改了代码如下:

#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;
}

,输出如下:

enter image description here

答案 1 :(得分:0)

第30行:删除分号。

第45行:看起来像复制/粘贴错误,应该是大学课程的构造函数,对吧?然后将名称从课程更改为大学。对于以下行中的析构函数也是如此。

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