Trying to construct an object with another object as parameter - c++

https://imgur.com/gallery/pEw8fBs
https://pastebin.com/xvWFHrTU
My errors are posted above..I don't understand what's wrong with this code..I'm trying to construct a book with a date object inside of it. Please help, this is my frist post as well!! :)
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
inline void keep_window_open() { char ch; cin >> ch; }
class Date {
int y, m, d;
public:
Date(int d, int m, int y);
};
class Book {
string title;
string author;
string isbn;
Date date;
public:
Book(string t, string a, string id, Date d);
};
int main() {
Book Charlie("charlie", "gates", "333H", Date(1, 2, 3));
}

Both of your constructors are declared but not defined
class Date {
int y, m, d;
public:
Date(int _d, int _m, int _y) : y(_y), m(_m), d(_d) {} // definition
};
class Book {
string title;
string author;
string isbn;
Date date;
public:
Book(string t, string a, string id, Date d)
: title(t), author(a), isbn(id), data(d) {} // definition
};

Your problem will be solved if you declare a default constructor for Date:
#include <iostream>
#include <string>
using namespace std;
class Date {
int y, m, d;
public:
// Default constructor which will be used in 'Book'
Date() {}
// Don't semicolon here, just two braces required
Date(int d, int m, int y) {}
};
class Book {
string title;
string author;
string isbn;
// Default constructor called here
Date date;
public:
// You can code everything inside the braces now
// NOTE 2: Default constructor called here
Book(string t, string a, string id, Date d) {}
};
int main() {
Book Charlie("charlie", "gates", "333H", Date(1, 2, 3));
return 0;
}

Related

Code Error: "Is not a nonstatic data member or base class of"

I have three classes in C++: Project, Supervisor, Date. I am trying to use composition, but the error I am encountering the following error message:
"Class Date
"Date" is not a nonstatic data member or base class of class "Project"".
This error occurs on this line, in Project.cpp:
Project::Project(char projectName, int sDay, int sMonth, int sYear, char supervisorName)
: Date(sDay, sMonth, sYear),
: Supervisor(supervisorName)
Complete Code:
Date.h:
#pragma once
class Date
{
public:
Date(int = 1, int = 1, int = 1);
void setDate(int, int, int);
void setDay(int);
void setMonth(int);
void setYear(int);
~Date();
private:
int day, month, year;
};
Date.cpp
#include "stdafx.h"
#include "Date.h"
Date::Date(int a, int b, int c)
{
setDate(a, b, c);
}
void Date::setDate(int d, int m, int y)
{
setDay(d);
setMonth(m);
setYear(y);
}
void Date::setDay(int da)
{
day = da;
}
void Date::setMonth(int mo)
{
month = mo;
}
void Date::setYear(int ye)
{
year = ye;
}
Date::~Date()
{
}
Supervisor.h:
#pragma once
class Supervisor
{
public:
Supervisor(char);
~Supervisor();
private:
char name;
};
Supervisor.cpp:
#include "stdafx.h"
#include "Supervisor.h"
Supervisor::Supervisor(char n)
{
name = n;
}
Supervisor::~Supervisor()
{
}
Project.h:
#pragma once
#include "Supervisor.h"
#include "Date.h"
class Project
{
public:
Project(char, int, int, int, char);
void setProject(char, int, int, int, char);
~Project();
private:
char pname;
Date startDate;
Supervisor supervisor;
};
Project.cpp:
#include "stdafx.h"
#include "Project.h"
#include "Supervisor.h"
#include "Date.h"
Project::Project(char projectName, int sDay, int sMonth, int sYear, char supervisorName)
: Date(sDay, sMonth, sYear),
: Supervisor(supervisorName)
{
setProject(projectName, sDay, sMonth, sYear, supervisorName);
}
void Project::setProject(char pN, int d, int m, int y, char sN)
{
pname = pN;
startDate = (d, m, y);
supervisor = sN;
}
Project::~Project()
{
}
The error message is reasonably self explanatory, you are using the syntax for passing arguments to the constructor of the base class of your class but the class names you are using aren't base classes of the current class.
To initialise class members use the name of the member not its class:
Project::Project(char projectName, int sDay, int sMonth, int sYear, char supervisorName)
: statrtDate(sDay, sMonth, sYear),
: supervisor(supervisorName)
{
setProject(projectName, sDay, sMonth, sYear, supervisorName);
}

Stl list of struct as a class member

For my homework I have to do a class Game in C++ that has name, size, and a list of updates that contain date of update and some information about that update. (for example : 22.05.2018 Bug fixed at quest 3). Here is what i tried, but it doesn't work. Game.h:
class Game{
public:
struct update{
string date;
string info;
};
string name;
double size;
list<update>l;
Game(string name, double size, list<update>l);
virtual ~Game();
};
and in Game.cpp:
Game::Game(string name, double size, list<update>l){
this->name=name;
this->size=size;
this->l=l;
}
In int main I created a list:
int main()
{
list<update>mylist;
update u1,u2,u3;
u1.date="20.05.2018";
u1.info="Mission 3 bug fixed";
u2.date="25.05.2018";
u2.info="New quest";
mylist.push_back(u1);
mylist.push_back(u2);
Game g("Gta5",60.0,mylist);
return 0;
}
i get this error:
no matching function for call to 'Game::Game(const char [4], double, std::__cxx11::list<update>&)'|
Or if you want to keep the nested class update:
#include <string>
#include <list>
using std::string;
using std::list;
class Game{
public:
struct update{
string date;
string info;
};
string name;
double size;
list<update>l;
Game(string name, double size, list<update>l);
virtual ~Game() {}
};
Game::Game(string name, double size, list<update>l){
this->name=name;
this->size=size;
this->l=l;
}
int main()
{
list<Game::update> mylist; // use Game::update to access nested class
Game::update u1,u2,u3;
u1.date="20.05.2018";
u1.info="Mission 3 bug fixed";
u2.date="25.05.2018";
u2.info="New quest";
mylist.push_back(u1);
mylist.push_back(u2);
Game g("Gta5",60.0,mylist);
return 0;
}
Try this,
#include<bits/stdc++.h>
using namespace std;
struct update
{
string date;
string info;
update(string date,string info){
this->date=date;
this->info=info;
}
};
class Game{
public:
string name;
double size;
list<update>l;
Game(string name, double size, list<update>l);
// virtual ~Game();
};
Game::Game(string name, double size, list<update>l){
this->name=name;
this->size=size;
this->l=l;
}
int main(){
list<update> l,m;
l.push_front(update("10/10/2017","some bug fixed"));
double size=100;
string name="Game1";
Game obj(name,size,l);
cout<<obj.name<<" "<<obj.size<<" "<<endl;
m=obj.l;
list<update>::iterator i;
for(i=m.begin();i!=m.end();i++){
update structObj=*i;
cout<<structObj.date<<" "<<structObj.info<<endl;
}
return 0;
}
Output
Game1 100
10/10/2017 some bug fixed

Error with Class in C++ for my project

I am new to this. Basically I just learnt how to use class in C++. When I try to print out, the values just seem to be 0. Can anyone help me out? Its supposed to print out:
Susan Myers 47899 Accounting Vice President
Mark Jones 39119 IT Position
Joy Rogers 81774 Manufacturing Engineer
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class Employee
{
private:
string name;
int idNumber;
string department;
string position;
public:
Employee()
{
name=" ";
idNumber=0;
department=" ";
position=" ";
}
Employee(string, int, string, string)
{
int id;
string n,d,p;
name=n;
idNumber=id;
department=d;
position=p;
}
Employee(string, int)
{
string n;
int id;
name=n;
idNumber=id;
}
void setName(string)
{
string n;
name=n;
}
void setId(int)
{
int id;
idNumber=id;
}
void setDepartment(string)
{
string d;
department=d;
}
void setPosition(string)
{
string p;
position=p;
}
string getName() const
{
return name;
}
int getId() const
{
return idNumber;
}
string getDepartment() const
{
return department;
}
string getPosition() const
{
return position;
}
};
int main()
{
Employee e1;
Employee e2;
Employee e3;
e1.setName("Susan Meyers");
e2.setName("Mark Jones");
e3.setName("Joy Rogers");
e1.setId(47899);
e2.setId(39119);
e3.setId(81744);
e1.setDepartment("Accounting");
e2.setDepartment("IT");
e3.setDepartment("Manufacturing");
e1.setPosition("Vice President");
e2.setPosition("Programmer");
e3.setPosition("Engineer");
cout<<"---------------------------------------"<<endl;
cout<<"Name"<<setw(6)<<"ID Number"<<setw(10)<<"Department"<<setw(12)<<"Position"<<endl;
cout<<e1.getName()<<setw(6)<<e1.getId()<<setw(10)<<e1.getDepartment()<<setw(12)<<e1.getDepartment()<<endl;
cout<<e2.getName()<<setw(6)<<e2.getId()<<setw(10)<<e2.getDepartment()<<setw(12)<<e2.getDepartment()<<endl;
cout<<e3.getName()<<setw(6)<<e3.getId()<<setw(10)<<e3.getDepartment()<<setw(12)<<e3.getDepartment()<<endl;
return 0;
}
This is what you get when you rely on guesswork rather than properly reading an introductory textbook on C++
A constructor of the Employee class which (apart from a blank line that I've removed) you define as
Employee(string, int, string, string)
{
int id;
string n,d,p;
name=n;
idNumber=id;
department=d;
position=p;
}
has the following effects.
The four arguments passed by the caller are ignored, since they are not named.
Four default-initialised variables (id, n, d, and p) are defined local to the constructor body. id will be uninitialised. The others, since they are std::string, are default-initialised (to an empty string)
The next four statements copy those variables into class members. The result is that initialising idNumber has undefined behaviour (since id is uninitialised) and the three strings are initialised to empty strings.
To get the effect that (I assume) you intend, change this to;
Employee(std::string n, int id, std::string d, std::string p)
{
name=n;
idNumber=id;
department=d;
position=p;
}
Note that I'm calling string by its full name std::string. That allows removing the using namespace std which (among other things) is BAD practice in header files.
Even better, change this to
Employee(const std::string &n, int id, const std::string &d, const std::string &p) :
name(n), idNumber(id), department(d), position(p)
{
}
which passes the strings by const reference (avoids additional copies of std::strings) and uses an initialiser list instead of assigning to members in the constructor.
Similar comments apply to ALL of the member functions of Employee, except that only constructors can have initialiser lists.
Errors made
Presentation
Your code is extremely cluttered, and has much irrelevant stuff.
Syntax
void setPosition(string){
Here your function has no argument! What is string?
Code
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class Employee{
public:
string name;
int idNumber;
string department;
string position;
void setName(string n){
name=n;
}
void setId(int k){
int id;
idNumber=id;
}
void setDepartment(string d){
department=d;
}
void setPosition(string p){
position=p;
}
string getName(){
return name;
}
int getId(){
return idNumber;
}
string getDepartment(){
return department;
}
string getPosition(){
return position;
}
};
int main(){
Employee e1;
Employee e2;
Employee e3;
e1.setName("Susan Meyers");
e2.setName("Mark Jones");
e3.setName("Joy Rogers");
e1.setId(47899);
e2.setId(39119);
e3.setId(81744);
e1.setDepartment("Accounting");
e2.setDepartment("IT");
e3.setDepartment("Manufacturing");
e1.setPosition("Vice President");
e2.setPosition("Programmer");
e3.setPosition("Engineer");
cout<<"---------------------------------------"<<endl;
cout<<"Name"<<" "<<"ID Number"<<" "<<"Department"<<" "<<"Position"<<endl;
cout<<e1.getName()<<" "<<e1.getId()<<" "<<e1.getDepartment()<<" "<<e1.getPosition()<<endl;
cout<<e2.getName()<<" "<<e2.getId()<<" "<<e2.getDepartment()<<" "<<e2.getPosition()<<endl;
cout<<e3.getName()<<" "<<e3.getId()<<" "<<e3.getDepartment()<<" "<<e3.getPosition()<<endl;
}
Output
---------------------------------------
Name ID Number Department Position
Susan Meyers 32767 Accounting Vice President
Mark Jones 32767 IT Programmer
Joy Rogers 32767 Manufacturing Engineer
Explanation
I have shortened your code by 50%(shows how much redundant stuff you had), and here is a working code.
void setDepartment(string d){
department=d;
}
Here, string d is defined IN the function as an argument. Note that your code also cout<< department twice, and I have corrected that for you in my above code.
Hope this helps.

Need Help Identifying Issue with Source Code C++ (Header, Struct, Class complication)

error: "prototype for WeatherForecaster::WeatherForecaster(std::all of the variables) does not match any class in WeatherForecaster"
I'm out of ideas on how to avoid this. My main code has nothing to do with the error btw.
MOST RECENT ERROR, THE REST FIXED. I now get the error in main "no matching function to call to WeatherForecast::WeatherForecast()". After I create the variable wf WeatherForecast.
Source:
#include "WeatherForecaster.h" //header being included
#include<iostream>
using namespace std;
//error comes here
WeatherForecaster::WeatherForecaster(string d, string fd, int h, int l,
int hum,int avgw, string avgwd, int maxw, string maxwd, double p)
{
string day=d;
string forecastDay=fd;
int highTemp=h;
int lowTemp =l;
int humidity=hum;
int avgWind= avgw;
string avgWindDir=avgwd;
int maxWind=maxw;
string maxWindDir= maxwd;
double recip=p;
}
WeatherForecaster::~WeatherForecaster(){
//dtor
};//end of block of source code
Header: I am making such a simple mistake, I'm just not sure what it exactly is.
#ifndef WEATHERFORECASTER_H
#define WEATHERFORECASTER_H
#include <iostream>
using namespace std;
//does my code have a problem with how it interacts with this struct?
struct ForecastDay{
std::string day;
std::string forecastDay;
int highTemp;
int lowTemp;
int humidity;
int avgWind;
std::string avgWindDir;
int maxWind;
std::string maxWindDir;
double precip;
};
class WeatherForecaster
{
public://most recent error ") expected before 'd'"
WeatherForecaster(string d, string fd, int h, int l,
int hum,int avgw, string avgwd, int maxw, string maxwd, double p);
~WeatherForecaster();
void addDayToData(ForecastDay);
void printDaysInData();
void printForecastForDay(std::string);
void printFourDayForecast(std::string);
double calculateTotalPrecipitation();
void printLastDayItRained();
void printLastDayAboveTemperature(int); //argument is the
temperature
void printTemperatureForecastDifference(std::string);
void printPredictedVsActualRainfall(int);
std::string getFirstDayInData();
std::string getLastDayInData();
protected:
private:
int arrayLength;
int index;
ForecastDay yearData[984];
};
#endif // WEATHERFORECASTER_H
Where is your declaration of constructor taking these parameter (string d, string fd, int h, int l, int hum,int avgw, string avgwd, int maxw, string maxwd, double p) in the header?
It is exactly as the error message states: you need to add a prototype for "WeatherForecaster(string d, string fd, int h, int l,int hum,int avgw, string avgwd, int maxw, string maxwd, double p)" in your WeatherForecaster class.

Trouble with using parent constructors

I'm currently working on an assignment to expand on a program we previously made, involving the use of header files, and parent classes. In the original, I have 2 header files. Person.h, and OCCCDate.h. In the new one, I am creating one called OCCCPerson.h. It's an incredibly simple class that basically just uses Person, just with 1 added variable.
My problem is, I cant figure out how to use the parent constructor properly.
Here is the Person.h file.
#ifndef PERSON_H
#define PERSON_H
#include <string>
#include "OCCCDate.h"
using namespace std;
class Person{
private:
string firstName;
string lastName;
OCCCDate dob;
public:
Person();
Person(string, string);
Person(string, string, OCCCDate);
string getFirstName();
string getLastName();
void setFirstName(string);
void setLastName(string);
int getAgeInYears();
bool equals(Person);
string toString();
};
#endif
And here is my OCCCPerson.h file
#ifndef OCCCPERSON_H
#define OCCCPERSON_H
#include <string>
#include "OCCCDate.h"
#include "Perosn.h"
using namespace std;
class OCCCPerson : Person{
protected:
string studentID;
public:
OCCCPerson(string firstName, string lastName, OCCCDate dob, string studentID);
OCCCPerson(Person p, string studentID);
string getStudentID();
bool equals(OCCCPerson p);
string toString();
};
#endif;
I cant seem to call on the parents constructor to get things like the firstname, lastname, and dob(date of birth). From my handout, it says the parent constructor has to be initialized with, : Person(parameters), where parameters are things in the parent class. However, I have no idea where to put that. Sorry for writing so much. I just couldn't figure out how to shrink that down.
Oh, and here is OCCCDate.h just in case
#ifndef OCCCDATE_H
#define OCCCDATE_H
#include<string>
using namespace std;
class OCCCDate{
private:
bool OCCCDate_US;
bool OCCCDate_EURO;
int dayOfMonth, monthOfYear, year;
bool dateFormat;
public:
OCCCDate();
OCCCDate(int dayOfMonth, int monthOfYear, int year);
int getDayOfMonth();
int getMonth();
string getNameOfMonth();
int getYear();
string getDate();
int getDifference(OCCCDate d1, OCCCDate d2);
int getDifference(OCCCDate d1);
void setDateFormat(bool);
bool equals(OCCCDate d);
string toString();
};
#endif
And here is my OCCCDate.cpp file
#include<iostream>
#include<ctime>
#include "OCCCPerson.h"
using namespace std;
OCCCPerson::OCCCPerson(string firstName, string lastName, OCCCDate dob, string studentID):Person(firstName, lastName, dob){
string firstName = Person::getFirstName();
string lastName = Person::getLastName();
OCCCDate dob = dob;
this->studentID = studentID;
}
OCCCPerson::OCCCPerson(Person p, string studentID){
Person p = p;
this->studentID = studentID;
}
What you need is member initializer lists.
From cppreference:
In the definition of a constructor of a class, member initializer list specifies the initializers for direct and virtual base subobjects and non-static data members.
A simple example:
class MyClass : BaseClass
{
public:
MyClass(int arg) : BaseClass(arg) {
//Rest of code
}
}
In your case, you can do:
OCCCPerson(string firstName, string lastName, OCCCDate dob, string studentID) :
Person(firstName, lastName, dob) {
this.studentID = studentID;
}
In OCCCPerson.cpp,
OCCCPerson(string firstName, string lastName, OCCCDate dob, string
studentID) : Person(firstName, lastName, dob)
{
//more stuff
}
You basically have to use an initializer list.
Read more here: What are the rules for calling the superclass constructor?
What this does is it calls the parent constructor Person(string, string, OCCCDate) and basically performs this->firstName = firstName. Similarly for lastName and dob. But not studentID because the Person(string, string, OCCCDate) constructor doesn't provide initialization for studentID.