Composition error in c++ - c++

Newbie to C++ over here. I am trying to create 2 classes and interlink them using Composition, but I keep getting errors.
#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;
}
The errors are as follows:
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"));
Could someone please help me out? I have tried going through forums with the same error but cant figure it out
^

I have modified the code as follows:
#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;
}
and the output is follows:

Line 30: Remove the semicolon.
Line 45: Looks like copy/paste error, should be the constructor of the college class, right? Then change the name from course to college. Same for the destructor on the following line.
Line 52: You create the object s5 2 lines above, I guess you want to pass this object to the constructor of the course object. If so then just remove the expression in the brackets after s5.

Related

C++ Using cin in member functions of a class

I am trying to use cin to read private members of a class within a member function of the class and i am getting an error [Error] no match for 'operator>>' in 'std::cin >> ((const Course*)this)->Course::courseID' ?
Is it possible to do that? Here is my code
//Course.h
#ifndef Course_H
#define Course_H
#include<string>
#include<iostream>
using namespace std;
class Course
{
private:
string courseID;
string courseName;
int credits;
Course * nextCourse;
public:
void EnterCourse() const;
void SetNext(Course *);
string GetCourseID() const;
string GetCourseName() const;
int GetCredits() const;
};
void Course::EnterCourse() const
{
cout<<"=======Enter Course Information========\n"
<<"Enter Course ID: \n";
cin>>courseID;
cout<<"Enter Course Name: \n";
cin>>courseName;
cout<<"Enter credits: \n";
cin>>credits;
}
string Course::GetCourseID() const
{
return courseID;
}
string Course::GetCourseName() const
{
return courseName;
}
int Course::GetCredits() const
{
return credits;
}
void Course::SetNext(Course * cou)
{
nextCourse=cou;
}
#endif
Using std::cin is considered writing to member data. But you can not change the data members using a const qualified member function. Remove the const specifier from the function declaration and definition if you want to achieve that functionality:
void Course::EnterCourse();
More info on the subject in these SO posts:
Meaning of “const” last in a C++ method declaration?
What is meant with “const” at end of function declaration?

Error no member function declared in class when compiling

I am pretty new to c++ and have no idea why I am getting this error, except that I think it's to do with using the string type for getter methods.
The error message:
C:\Users\Robin Douglas\Desktop\week6>g++ -c Student.cpp
Student.cpp:15:31: error: no 'std::string Student::get_name()' member function d
eclared in class 'Student'
Student.cpp:20:43: error: no 'std::string Student::get_degree_programme()' membe
r function declared in class 'Student'
Student.cpp:25:32: error: no 'std::string Student::get_level()' member function
declared in class 'Student'
Student.hpp
#include <string>
class Student
{
public:
Student(std::string, std::string, std::string);
std::string get_name;
std::string get_degree_programme;
std::string get_level;
private:
std::string name;
std::string degree_programme;
std::string level;
};
Student.cpp
#include <string>
#include "Student.hpp"
Student::Student(std::string n, std::string d, std::string l)
{
name = n;
degree_programme = d;
level = l;
}
std::string Student::get_name()
{
return name;
}
std::string Student::get_degree_programme()
{
return degree_programme;
}
std::string Student::get_level()
{
return level;
}
The following code defines fields (variables) rather then methods.
public:
Student(std::string, std::string, std::string);
std::string get_name;
std::string get_degree_programme;
std::string get_level;
Then, when you implement it in the .cpp file the compiler complains that you try to implement a method that was not declared (since you declared get_name to be a variable).
std::string Student::get_name()
{
return name;
}
To fix, just change your code as below:
public:
Student(std::string, std::string, std::string);
std::string get_name();
std::string get_degree_programme();
std::string get_level();

Member function and expected primary-expression errors

Here is my code, I am currently working on my own BigInt class and I am having some problems with getters and setters, and some program structure.
I keep getting these two errors:
prog.cpp: In function 'int main()':
prog.cpp:38:29: error: cannot call member function 'void bigInt::setString(std::string)' without object
bigInt::setString(digString);
^
prog.cpp:39:28: error: expected primary-expression before 'void'
cout << bigInt::getString(void);
^
Code
//my bigInt class
#include <iostream>
#include <vector>
#include <string>
using namespace std;
//class, constructors
//overload operators methods
//add, subtract, multiply, divide
class bigInt{//class
public:
bigInt();
~bigInt();
void setString(string dig);
string getString(void);
std::string digit;
std::string digString;
private:
std::vector<int> big1;
std::vector<int> big2;
};
//constructors
bigInt::bigInt(void){
}
//deconstructor
bigInt::~bigInt(){
}
void bigInt::setString(string dig){
digit= dig;
}
string bigInt::getString(){
return digit;
}
int main(){
string digString= "90867537465982736459087162745938";
bigInt::setString(digString);
cout << bigInt::getString(void);
}
Thanks in advance!
In main you would do something like.
int main(){
string digString= "90867537465982736459087162745938";
bigInt my_int{};
my_int.setString(digString);
cout << my_int.getString();
}
You have to actually instantiate a bigInt object, which I called my_int in the above example. Then you can call the methods setString() and getString() off of that object.
The only class methods that you can call without having an instance to call them off of are static methods.
You need an object to work with. Try in your main:
bigInt myInt;
myInt.setString(...);

What is wrong? - Composition compile error (with family+person classes example)

I got two classes, one named Person that I checked is working (I can create objects of that class so the problem should not be here).
I then have another class called Family with composition from Person:
Family.h
#include "Person.h"
class Family
{
public:
Family();
void printFamily();
private:
Person dad_();
Person mum_();
Person son_();
Person daughter_();
};
Family.cpp
#include "Family.h"
Family::Family()
{
}
void printFamily()
{
dad_.printAll();
mum_.printAll();
son_.printAll();
daughter_.printAll();
//printAll() is a function in the Person class that worked when
//I tested it earlier with only the person class
}
But when i try to compile this I get an error:
left of '.printAll' must have class/struct/union
'son_' : undeclared identifier
This error goes for all the .printAll() calls in family.cpp.
I can't see why this wouldn't work, so I hope you can.
Edit1:
Ok i changed
void printFamily()
to
void Family::printFamily()
That removes one error, but i still get
left of '.printAll' must have class/struct/union
Edit2
Ah my bad with the Person calls i changed them to
Person dad_;
and the same with the rest.
Seems like their might be an error with my Person class so i will post that also
Person.h
#include <string>
using namespace std;
class Person
{
public:
Person( const string & = "000000-0000", const string & = "N", const string & = "",const string & = "N");
~Person();
void setFirstName(const string &);
void setMiddleName(const string &);
void setLastName(const string &);
void getData(string &,string &,string &,string &);
static int getNumberOfPersons();
void printPartially() const;
void printAll() const;
bool checkForSameName(const Person &);
private:
string firstName_;
string middleName_;
string lastName_;
string socialSecNumber_;
static int numberOfPersons_;
};
Person.cpp
#include "Person.h"
#include <iostream>
int Person::numberOfPersons_ = 0;
Person::Person( const string &sNumber, const string &firstName, const string &middleName,const string &lastName )
:firstName_(firstName),middleName_(middleName),lastName_(lastName),socialSecNumber_(sNumber)
{
numberOfPersons_ ++;
}
Person::~Person()
{
numberOfPersons_--;
}
void Person::setFirstName(const string &firstName)
{ firstName_ = firstName; }
void Person::setMiddleName(const string &middleName)
{ middleName_ = middleName; }
void Person::setLastName(const string &lastName)
{lastName_ = lastName;}
void Person::getData(string &fName,string &mName,string &lName,string &sNumber)
{
fName = firstName_;
mName = middleName_;
lName = lastName_;
sNumber = socialSecNumber_;
}
int Person::getNumberOfPersons()
{
return numberOfPersons_;
}
void Person::printPartially() const
{
cout <<"Navn: "<<firstName_<<" "<<middleName_<<" "<<lastName_<<endl;
cout <<"Født: ";
for (int i = 0;i<6;i++)
{
cout <<socialSecNumber_.at(i);
}
}
void Person::printAll() const
{
cout <<"Navn: "<<firstName_<<" "<<middleName_<<" "<<lastName_<<endl;
cout <<"Personnr: "<<socialSecNumber_<<endl;
}
bool Person::checkForSameName(const Person &p)
{
if (p.firstName_ == firstName_ && p.middleName_ ==middleName_ && p.lastName_ == lastName_)
return true;
else
return false;
}
Now i am getting some new errors:
error C2011: 'Person' : 'class' type redefinition
see declaration of 'Person'
'Family::dad_' uses undefined class 'Person'
The "dad" error applies to the whole family
You have a few syntax issues.
First, you're declaring each of what are supposed to be member variables as functions which return Person. They should look like (note, no parens):
Person dad_;
Person mum_;
Person son_;
Person daughter_;
You're also missing the scoping on your definition of printFamily:
void Family::printFamily() {
...
}
Without the preceding Family::, C++ thinks you're defining a free function, and doesn't know to look inside the Family class for the declarations of dad_, mum_, etc.
Additionally, at least with the code you've shown, there's no way to initialize the people in your class. The Family constructor should take arguments to define the people, or you should have setters which allow defining them later. Right now, you'll get 4 identical people, set up however the default person constructor builds them.
I would normally prefer the constructor method, but I have other design reservations about your code to begin with (e.g. Does a family always contain mum, dad, brother, sister?) and that's not really what this question is about.
The line:
Person dad_();
says that dad_ is a function that returns a Person, not an object. Did you mean that? Similarly for others.
Try
Person dad_;
Family.h
#include "Person.h"
class Family
{
public:
Family();
void printFamily();
private:
Person dad_;
Person mum_;
Person son_;
Person daughter_;
};
Family.cpp
#include "Family.h"
Family::Family()
{
}
void Family::printFamily()
{
dad_.printAll();
mum_.printAll();
son_.printAll();
daughter_.printAll();
//printAll() is a function in the Person class that worked when
//I tested it earlier with only the person class
}
The out of line definition of a member function needs to include the class name:
void Family::printFamily()
{
//...
Surprisingly, you already got this right for the constructor but then immediately forgot...
Second, your private class members are functions, not data members (which is odd), but if that's deliberate, you need to call them:
dad_().printAll();
// ^^^

error: incompatible types in assignment of ‘char’ to ‘char [11]’

I'm trying to create a member function that allows an user to set member array variables.
I've been looking everywhere but I can't find the problem in my code,
#include <string>
#include <iostream>
using namespace std;
class Employee
{
protected:
string name;
char ssn[11];
char id[5];
char hired[8];
public:
Employee(char ssn, char id, char hired); //Constructor
Employee(string name);
~Employee(); //Destructor
void setName(string n) { n = name; }
void setSSN(char i) { ssn = i; }
};
int main()
{
return 0;
}
Let's have a look at your setSSN function:
void setSSN(char i) { ssn = i; }
SNN, which most likely means social security number, doesn't consist of just one digit but 11, right? Then why would setSSN take as input only one character (digit) by (char i)? So setSSN function should rather take a string of characters containing SSN of the employee and that string should be of the same flavor as the ssn member variable of your Employee class in order to let you assign one string variable by another in the body of setSSN function. If you are already familiar with the string class of the C++ standard library, you should probably use that class for all your string storage and manipulation.