Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have made two classes Department and Student and then made third Class College in which i declared one object each of Department and Student class.
In the constructor of College class:

C++
College(string clg_name, int num_dept, Department &dp,  Student &s)
    {
        m_clgName=clg_name;  
        m_numDept=num_dept;
        dep=dp;
        st=s;
    }


I am getting error:
error: No matching function for call to 'College::College(const char[5],int ,Department, student)
 no known conversion for argument 3 'Department' from 'Department&'

-------------------------------------------------------------------------
 
 Solution either: College(string clg_name, int num_dept, Department dp,  Student s)
  or    College(string clg_name, int num_dept, const Department &dp, const Student &s)

so what difference does const make in constructor definition ?


---------------------------------------------------------------------------

Whole code is like this:
First I have a Student.h file and Department.h for student and department class
C++
#ifndef STUDENT_H
#define STUDENT_H

#include <iostream>
#include <cstring>

using namespace std;

class Student
{
    string m_stName;
    int m_stID;
    //
public:
    Student(){}
    Student(string name, int id){StdInfo(name,id);}
    string GetStdName() {return m_stName;}
    int GetStdID(){return m_stID;}
    void StdInfo(string name, int id)
    {
        m_stName=name;
        m_stID=id;
    }
    friend ostream& operator<<(ostream &os, const Student &st);
    friend istream& operator>>(istream &is, Student &st);
};
ostream& operator<<(ostream &os, const Student &st)
{
    os<<"\nStudent's name: "<<st.m_stName<<", ID: "<<st.m_stID<<endl;
    return os;
}
istream& operator>>(istream &is, Student &st)
{
    cout << "\nEnter student's name: ";
    is >> st.m_stName;
    cout << "\nEnter student's ID: ";
    is >> st.m_stID;
    return is;
}
#endif // STUDENT_H 



C++
#ifndef DEPARTMENT_H
#define DEPARTMENT_H

#include <iostream>
#include <cstring>

using namespace std;

class Department
{
    string m_DeptName;
//
public:
    Department(){}
    Department(string name){ setDepName(name);}
    void setDepName(string name){ m_DeptName=name;}
    string getDeptName(){ return m_DeptName;}
    friend ostream& operator<<(ostream &os, const Department &dp);
    friend istream& operator>>(istream &is, Department &dp);
};

ostream& operator<<(ostream &os, const Department &dp)
{
    os<<"Department's name: "<<dp.m_DeptName<<endl;
    return os;
}
istream& operator>>(istream &is, Department &dp)
{
    cout << "\nEnter Department name: ";
    is >> dp.m_DeptName;
    return is;
}
#endif // DEPARTMENT_H 



finally main script:


C++
#include <iostream>
#include <string>
#include <cstring>
#include "Student.h"
#include "Department.h"

using namespace std;


class College
{
    string m_ClgName;
    int m_numDept;
    Department dep;
    Student st;
    //Department *dep=new Department[3];
    //Student *st=new Student[4];
    College(){}
public:
    College(string clg_name, int num_dept, Department &dp, Student &s)
    {
        m_ClgName=clg_name;
        m_numDept=num_dept;
        dep=dp;
        st=s;
    }
    friend ostream& operator<<(ostream &os, const College &cl);
    friend istream& operator>>(istream &is , College &cl);
    void SetClgDetails()
    {cin >> dep;cin >> st;}
};

ostream& operator<<(ostream &os, const College &cl)
{
    os <<cl.m_ClgName<<" has "<<cl.m_numDept<<" departments\n";
    cout <<cl.dep;
    cout <<cl.st<<endl;
}
istream& operator>>(istream &is , College &cl)
{
    cout << "\nEnter college name :";
    is >> cl.m_ClgName;
    cout << "\nEnter no. of departments: ";
    is >> cl.m_numDept;
}
int main()
{

    College clg("ABCD", 1, Department("Computer_Science"),Student("Rajender",249));
    cout << clg;
    cout << "===============================================";
    cin >>clg;
    clg.SetClgDetails();
    cout <<clg;
    return 0;
} 



I also want to know how can I make array of objects of Department and Student in College class something like:


C++
Department *dp = new Department[size_d];
Student *st= new Student[size_s]; 


where size_d and size_s will be objects of College class and will take user input to decide how many department and students user wants and how should constructor definition should be in these cases.
Posted
Updated 6-Sep-15 23:56pm
v4
Comments
CPallini 7-Sep-15 5:42am    
Could you please show the code where the function call is performed?

 
Share this answer
 
The error is probably not sourced by the Department and Student parameters but by the clg_name parameter. The functions expects a parameter of type string but you are passing a const char * (it would be helpful to see the function call).

The const makes no real difference but is the preferred way: It ensures that the passed objects can't be changed by the College constructur.

So you should change the constructor to (either one or both):
College(const string & clg_name, int num_dept, const Department &dp, const Student &s)
{
}
College(const char * clg_name, int num_dept, const Department &dp, const Student &s)
{
}

Note that the first version passes also the name by reference. When implementing the second version you can define the first one using the second one:
College(const string & clg_name, int num_dept, const Department &dp, const Student &s)
{
    College(clg_name.c_str(), num_dept, dp, s);
}


[UPDATE]
The problem is sourced by the copy assignment operators generated by the compiler for the Department and Student classes which are used in the College constructor. When passing a reference, the parameter must be const (const Department &dp). Non-const parameters are only allowed when passing parameters by value (Department dp). See the Copy assignment operator[^] and Copy-and-swap[^].

Regarding the arrays you are on the right way. The usual solution is to provide a function to set the members and call that for each array item.
 
Share this answer
 
v3
Comments
the_beginner 7-Sep-15 5:35am    
It was a error in my question i have corrected it but if u see 2nd line in error, its because of Department and student,
Jochen Arndt 7-Sep-15 5:41am    
Please show the line where you call the constructor and the definitions of the passed variables (use the Improve question option).
Then we can see what is wrong.
the_beginner 7-Sep-15 6:57am    
thanks, I just want to ask as I am new to coding, generally in case of declaring array of objects do we need to initialize them in constructor or not ?
Jochen Arndt 7-Sep-15 7:39am    
With arrays, you can not use the constructors with parameters. This would make no sense because you usually want to set the items to individual settings.

You need a constructor without parameters (which you have already). These constructors should initialise members with default values (e.g. set Student::m_stID to -1 or zero). The string members will be initialised to empty strings by their own constructors. It is good programming style to initialise all members in the default constructor. If not doing so you will have random values for members without own constructor (all literal values and pointers).

Once the array is created you can initialise the items with the individual settings.
You are passing a temporary object to a function accepting a reference. That's not allowed. Changing the signature in order to accept a const reference fixes it because you are providing the warranty of not touching it.
 
Share this answer
 
Comments
the_beginner 7-Sep-15 6:58am    
thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900