c++ Errors with Class Codes - c++

I keep getting various errors in my code can someone please run it through their compiler and help me resolve them?? I fixed a few and the one I am getting now is no matching function for call to 'classRooms::ClassRooms(std::string&, int&, .....
//classRoom.h
#ifndef CLASSROOMS_H
#define CLASSROOMS_H
#include <iostream>
#include <string>
#include "ClassRooms.h"
using namespace std;
class ClassRooms
{
public:
ClassRooms(int nRooms=100)
: numRooms(0), rooms(new ClassRooms[nRooms]) { }
~ClassRooms()
{
delete [] rooms;
}
void addRoom(const ClassRooms& cr)
{
rooms[numRooms++] = cr;
}
string findRoom(int seats)const;
int getSeats();
double getAreaPerSeat();
string getRoomNumber();
private:
int numRooms;
ClassRooms *rooms;
int numSeats;
double length, width;
};
#endif //CLASSROOMS_H
//ClassRooms.h
#ifndef CLASSROOMS_H
#define CLASSROOMS_H
#include <iostream>
#include <string>
#include "classRoom.h"
using namespace std;
class ClassRooms
{
public:
ClassRooms(int nRooms=100)
: numRooms(0), rooms(new ClassRooms[nRooms]) { }
~ClassRooms()
{
delete [] rooms;
}
void addRoom(const ClassRooms& cr)
{
rooms[numRooms++] = cr;
}
string findRoom(int seats)const;
private:
int numRooms;
ClassRooms *rooms;
};
#endif //CLASSROOMS_H
//main.cpp
#include <iostream>
#include <fstream>
#include "ClassRooms.h"
using namespace std;
int main()
{
ifstream fin("rooms.txt");
if (!fin)
{
cout << "Cannot open file\n";
return 1;
}
string roomNum;
int seats;
double len, wid;
ClassRooms classRooms;
while (fin >> roomNum >> seats >> len >> wid)
classRooms.addRoom(ClassRooms(roomNum, seats, len, wid));
fin.close();
while (seats > 0)
{
cout << "Enter MAX capacity: ";
cin >> seats;
cout << "Your best room is " << classRooms.findRoom(seats) << endl;
}
}
//ClassRooms.cpp
#include "classRoom.h"
string ClassRooms::findRoom(int seats)const
{
int maxIndex = 0;
while (maxIndex < numRooms && rooms[maxIndex].getSeats() < seats)
++maxIndex;
if (maxIndex == numRooms) return "";
for (int i = 0; i < numRooms; ++i)
if (rooms[i].getSeats() >= seats
&& rooms[i].getAreaPerSeat() > rooms[maxIndex].getAreaPerSeat())
maxIndex = i;
return rooms[maxIndex].getRoomNumber();
}

You have no Classrooms constructor that takes a (string, int, double, double), so therefore, this ClassRooms(roomNum, seats, len, wid) is an error. You can easily add an appropriate constructor to Classrooms though.
ClassRooms(string roomNum, int seats, double len, double wid){
//do something
}

Related

c++ this is insertion sorting for food and price and calorie but the sorting function doesn't work

#include <iostream>
#include <string>
#ifndef FOOD_H
#define FOOD_H
using namespace std;
this class for insertion sorting for food's price or food's calorie
class Food
{
private:
string Name;
float Calorie;
float Price;
public:
void setName(string n);
void setCalorie(float c);
void setPrice(float p);
string getName();
float getCalorie();
float getPrice();
Food();
Food(string n,float c,float p);
void Print(Food FoodArray[],int);
void Sort(Food FoodArray[], float);
void Sort2(Food FoodArray[], float);
};
#endif
#include <iostream>
#include <string>
#include "Food.h"
#ifndef IMPLEMENTER_CPP
#define IMPLEMENTER_CPP
using namespace std;
void Food::setName(string n){Name=n;}
void Food::setPrice(float p){Price=p;}
void Food::setCalorie(float c){Calorie=c;}
string Food::getName(){return Name;}
float Food::getPrice(){ return Price;}
float Food::getCalorie(){ return Calorie;}
Food::Food(string n,float c,float p)
{
Food::Name=n;
Food::Calorie=c;
Food::Price=p;
}
Food::Food()
{
Food::Name="";
Food::Calorie=0.00;
Food::Price=0.00;
}
void Food::Print(Food FoodArray[],int num)
{
for (int i=0;i>num;i++)
{
cout<<"-------------------------------------------------"<<endl;
cout<<"NAME: "<<FoodArray[i].getName()<<" CALORIE: "<<FoodArray[i].getCalorie()<<" PRICE: "<<FoodArray[i].getPrice()<<endl;
cout<<"-------------------------------------------------"<<endl;
}
}
void Food::Sort(Food FoodArray[], float pri)
{
int i,j;
float tmp;
string t;
for(i=1;i<=pri-1;i++)
{
tmp=FoodArray[i].getPrice();
t=FoodArray[i].getName();
j=i-1;
while((tmp<FoodArray[j].getPrice())&&(j>=0))
{
FoodArray[j+1]=FoodArray[j];
j--;
}
FoodArray[j+1].setName(t);
FoodArray[j+1].setPrice(tmp);
}
cout<<endl<<"Sorted list is shown below: "<<endl;
}
void Food::Sort2(Food FoodArray[], float cal)
{
int i, j;
float tmp;
string t;
for(i=1;i<=cal-1;i++)
{
tmp=FoodArray[i].getCalorie();
t=FoodArray[i].getName();
j=i-1;
while((tmp<FoodArray[j].getCalorie())&&(j>=0))
{
FoodArray[j+1]=FoodArray[j];
j=j-1;
}
FoodArray[j+1].setCalorie(tmp);
FoodArray[j+1].setName(t);
}
cout<<endl<<"Sorted list is shown below: "<<endl;
}
#endif
#include <iostream>
#include <string>
#include "Food.h"
#include "Implementer.cpp"
using namespace std;
contains the main method. From this class an array of food will be created and the sorting function
will be called to sort the array.
class client
{
public:
Food *foodArray[];
void info()
{int num;
string name,answer;
float calorie,price;
loop2:
cout<<"how many do you want ? ";
cin>>num;
if (num>0)
{
for (int i=0; i<num; i++)
{
cout<<i+1<<"- ";
cout<<"Enter Food name: ";
cin>>name;
cout<<"Enter Calories: ";
cin>>calorie;
cout<<"Enter Price: ";
cin>>price;
Food food=Food(name,calorie,price);
*foodArray[i]=food;
}
foodArray[num]->Print(*foodArray,num);
loop:
cout<<"Would you like to sort the foods based on Price or Calories? P/C"<<endl;
cin>>answer;
if (answer=="P")
{
foodArray[num]->Sort(*foodArray,price);
foodArray[num]->Print(*foodArray,num);
}
else if (answer=="C")
{
foodArray[num]->Sort2(*foodArray,calorie);
foodArray[num]->Print(*foodArray,num);
}
else
{
cout<<"please choose P or C."<<endl;
goto loop;
}
}
else
{
goto loop2;
}
}
};
int main()
{
client C;
C.info();
return 0;
}

Class name does not name a type - Pulling my hair out

I've been working on this for several hours and I can't figure out which class is giving me problems. I get a bunch of 'VendSlot' does not name a type" errors. I appreciate any help you can provide. I am new to C++ and extremely frustrated.
//class Snack
#ifndef SNACK_HPP
#define SNACK_HPP
#include <iostream>
#include <string>
using namespace std;
class Snack
{
private:
string name;
double price;
int calories;
public:
Snack();
Snack(string name, double price, int calories);
string getName();
double getPrice();
int getNumCalories();
};
#endif
Snack.cpp
#include "Snack.hpp"
#include <iostream>
#include <string>
using namespace std;
Snack::Snack()
{
name = "bottled water";
price = 1.75;
calories = 0;
}
Snack::Snack(string n, double p, int c)
{
name = n;
price = p;
calories = c;
}
string Snack::getName()
{
return name;
}
double Snack::getPrice()
{
return price;
}
int Snack::getNumCalories()
{
return calories;
}
VendSlot.hpp
#ifndef VENDSLOT_HPP
#define VENDSLOT_HPP
#include "Snack.hpp"
class VendSlot
{
private:
Snack s;
int amount;
public:
VendSlot();
VendSlot(Snack s, int a);
Snack getSnack();
int getAmount();
int decrementAmount();
};
#endif
VendSlot.cpp
#include "VendSlot.hpp"
#include <iostream>
#include <string>
using namespace std;
VendSlot();
VendSlot::VendSlot()
{
Snack s1;
s = s1;
amount = 5;
}
VendSlot::VendSlot(Snack s1, int a)
{
s = s1;
amount = 5;
}
Snack VendSlot::getSnack()
{
Snack s1;
s = s1;
return s;
}
int VendSlot::getAmount()
{
return amount;
}
int VendSlot::decrementAmount()
{
amount--;
return amount;
}
MiniVend.hpp
#ifndef MINIVEND_HPP
#define MINIVEND_HPP
#include "VendSlot.hpp"
class MiniVend
{
private:
VendSlot vs1;
VendSlot vs2;
VendSlot vs3;
VendSlot vs4;
double money;
public:
MiniVend();
MiniVend(VendSlot v1, VendSlot v2, VendSlot v3, VendSlot v4, double money);
int numEmptySlots();
double getMoney();
double valueOfSnacks();
void buySnack();
};
#endif
MiniVend.cpp
#include "VendSlot.hpp"
using namespace std;
MiniVend::MiniVend(VendSlot v1, VendSlot v2, VendSlot v3, VendSlot v4, double m)
{
vs1 = v1;
vs2 = v2;
vs3 = v3;
vs4 = v4;
money = m;
}
double MiniVend::getMoney()
{
return money;
}
int MiniVend::numEmptySlots()
{
int numSlots = 0;
if (vs0.getAmount() == 0)
slots++;
if (vs1.getAmount() == 0)
slots++;
if (vs2.getAmount() == 0)
slots++;
if (vs3.getAmount() == 0)
numSlots++;
return numSlots;
}
double MiniVend::valueOfSnacks()
{
double value = 0;
value = (vs0.getSnack().getPrice() * vs0.getAmount() +
(vs1.getSnack().getPrice() * vs1.getAmount()) +
(vs2.getSnack().getPrice() * vs2.getAmount()) +
(vs3.getSnack().getPrice() * vs3.getAmount()))
return value;
}
void MiniVend::buySnack(int slot);
{
if (slot == 0 && vs0.getAmount >= 1)
{
money = money + vs0.getSnack.getPrice();
vs0.decrementAmount();
}
else if (slot == 1 && vs1.getAmount >= 1)
{
money = money + vs1.getSnack.getPrice();
vs1.decrementAmount();
}
else if (slot == 2 && vs2.getAmount >= 1)
{
money = money + vs2.getSnack.getPrice();
vs2.decrementAmount();
}
else if (slot == 3 && vs3.getAmount >= 1)
{
money = money + vs3.getSnack.getPrice();
vs3.decrementAmount();#include "Snack.hpp"
}
else
cout << "Sold out. Please select another Snack." << endl;
}
Main
#include "MiniVend.hpp"
#include "Snack.hpp"
#include "VendSlot.hpp"
#include <iostream>
#include <string>
using namespace std;
int main()
{
Snack s2("candy bar", 1.25, 300);
Snack s3("root beer", 2.00, 450);
VendSlot groucho(s1, 2);
VendSlot harpo(s2, 1);
VendSlot chico(s3, 0);
VendSlot zeppo; // five bottles of water
MiniVend machine(groucho, harpo, chico, zeppo, 0);
cout << machine.numEmptySlots() << endl;
cout << machine.valueOfSnacks() << endl;
cout << machine.getMoney() << endl;
machine.buySnack(1);
cout << machine.numEmptySlots() << endl;
cout << machine.valueOfSnacks() << endl;
cout << machine.getMoney() << endl;
return 0;
}
At MiniVend.hpp your buySnack does not take any parameters void buySnack();, but at Minivend.cpp you are defining an argument at your function header
void MiniVend::buySnack(int slot);, and you shouldn't be using an semicolon too.
Your MiniVend.cpp file should also include the "MiniVend.hpp" header as well.
At MiniVend.cpp in the declaration of function int MiniVend::numEmptySlots() vs0 is not defined anywhere in the code as well as slots
At you main function you are trying to declare this VendSlot groucho(s1, 2);, but you have not defined s1 anywhere in your main function
That's what I have found so far. Hope this helps and please use comments when posting a question so that everyone can understand the purpose of the code.

compiler is not recognizing call to function from class as member of class

Trying to figure out why im getting an error: unresolved external main reference in function
header file:
#define SURGERY_H
#include <string>
using namespace std;
class Surgery {
private:
static int didhave[5];
static float costs[5], total;
static string types[5];
public:
friend void requestInput();
friend float sendTotal();
};
static float costs[5] = { 100.00, 200.00, 300.00, 400.00, 500.00 };
static string types[5] = { "Tonsil", "Foot", "Knee", "Shoulder", "Appendix" } ;
static int didhave[5] = { 0, 0, 0, 0, 0 };
#endif
cpp file of the surgery.h class header file that contains function definitions:
i feel as if im at a complete lost because I have been working on this for a large amount of hours
#include <iostream>
#include "Surgery.h"
#include <string>
using namespace std;
void requestInput() {
string input;
while(input[0]!='1'){
cout << "Which types of surgery did the patient have? 1) for finished.\n";
cin >> input;
if(input[0]=='T' || input[0]=='t')
didhave[0]=1;
else if(input[0]=='F' || input[0]=='f')
didhave[1]=1;
else if(input[0]=='K' || input[0]=='k')
didhave[2]=1;
else if(input[0]=='S' || input[0]=='s')
didhave[3]=1;
else if(input[0]=='A' || input[0]=='a')
didhave[4]=1;
else
cout << "Invalid Surgery Type.";
}
}
float sendTotal() {
int i;
float total;
for(i=0; i<5; i++){
if(didhave[i]==1)
total+=costs[i];
}
return total;
}
main:
#include <iostream>
#include "Surgery.h"
int main(void){
Surgery surgeries;
surgeries::requestInput();
system("pause");
return 0;
}
You need to define the function 'main' somewhere, which is the entry point for the process

What is the proper way of passing a vector?

Im on year 10 and our teacher wants us to create an original project and using pointers
What I want to do is to create Members and be able to sort the members by there names and print them
When I run my code it says Invalid Access
Team.h
#ifndef TEAM_H
#define TEAM_H
#include "Staff.h"
#include <vector>
#include <iostream>
using std::vector;
class Team: public Staff
{
public:
Team();
~Team();
vector<Staff *> &getVector();
private:
vector<Staff *> myStaffs;
};
#endif // TEAM_H
Team.cpp
Team::Team()
{
for(unsigned int iStaff = 0; iStaff < myStaffs.size(); iStaff++)
{
myStaffs[iStaff] = createStaff(iStaff);
}
}
vector<Staff*>& Team::getVector()
{
return myStaffs;
}
Command class will do the sorting of team and print all team members
Command.cpp
void Command::printStaffs(vector<Staff*>&myStaffs)
{
for(unsigned int iStaff = 0; iStaff < myStaffs.size(); iStaff++)
{
std::cout << "Staff ID number: "<< myStaffs[iStaff]->getStaId() << std::endl
<< "Staff Skills 1: " << *myStaffs[iStaff]->getStaSkill() << std::endl
<< "Staff Skills 2: " << *myStaffs[iStaff]->getStaSkill() << std::endl
<< "Staff Skills 3: " << *myStaffs[iStaff]->getStaSkill() << std::endl
<< std::endl;
}
}
Command.h
#ifndef CommandH
#define CommandH
#include "Team.h"
#include <vector>
#include <iostream>
using std::vector;
class Command: public Team
{
public:
Command(){}
~Command(){}
void sortVector(vector<Staff* >&vectorTemp);
void printStaffs(vector<Staff* >&);
private:
vector<Staff *> vectEmployee;
};
//--------------------------------------------------------------------------
#endif
main.cpp
#include <iostream>
#include <conio.h>
#include "Team.h"
#include "Command.h"
int main()
{
Team t;
Command c;
c.printStaffs(t.getVector());
getch();
return 0;
}
Staff.h
#ifndef STAFF_H
#define STAFF_H
#include <cstdlib>
#include <ctime>
#include <string>
using std::rand;
class Staff
{
public:
Staff();
~Staff();
static Staff* createStaff(int); // creates staffs
int** getStaSkill();
int getStaId(); // returns Staff ID
static int genRanNum(int); //Generate random number
private:
int *staSkill[3];
int staId;
//int staDeptAsigned;
};
#endif
Staff.cpp
#include "Staff.h"
Staff::Staff()
{
*staSkill = new int[3];
}
Staff *Staff::createStaff(int s)
{
Staff *staff = new Staff();
staff->staId = s;
*(staff->staSkill[0]) = genRanNum(10);
*(staff->staSkill[1]) = genRanNum(10);
*(staff->staSkill[2]) = genRanNum(10);
return staff;
}
int** Staff::getStaSkill()
{
return staSkill;
}
int Staff::getStaId()
{
return staId;
}
int Staff::genRanNum(int num)
{
return 1 +(std::rand()%num);
}
Staff::~Staff(){}
When you construct a Team, you have the following constructor:
Team::Team()
{
for(unsigned int iStaff = 0; iStaff < myStaffs.size(); iStaff++)
{
myStaffs[iStaff] = createStaff(iStaff);
}
}
However, myStaffs is a member of Team and gets default constructed as empty, so nothing happens here since myStaffs.size() == 0.
Calling printStaffs on this Team::getVector() will correctly inform you that the vector is empty:
int main()
{
Command c;
Team t; // t.myStaffs will be empty
c.printStaffs(t.getVector()); // passes an empty vector to printStaffs
return 0;
}
You might want to pass a number to your Team constructor to create that many staffs:
Team::Team(int number_of_staff)
{
for(unsigned int iStaff = 0; iStaff < number_of_staff; iStaff++)
{
myStaffs.push_back(createStaff(iStaff));
}
}
int main()
{
Command c;
Team t(5); // t.myStaffs will contain 5 staff members
c.printStaffs(t.getVector()); // passes vector of 5 staff
return 0;
}

Classes in C++: Declaring an Instance

I have consulted many websites but have not successfully completed my code.
I am trying to write some code that spits out information about a car, once the user provides information. After compiling, the compiler says that "declaration does not declare anything." Here's the code:
#include <iostream>
#include <string>
#include "Car.h"
using namespace std;
Car::Car()
{
}
void Car::accel()
{
speed += 5;
}
void Car::brake()
{
speed -= 5;
}
void Car::setSpeed(int newSpeed)
{
speed = newSpeed;
}
int Car::getSpeed()
{
return speed;
}
void Car::setMake(string newMake)
{
make = newMake;
}
string Car::getMake()
{
return make;
}
void Car::setYearModel(int newYearModel)
{
yearModel = newYearModel;
}
int Car::getYearModel()
{
return yearModel;
}
int main()
{
Car auto; //instance of class Car
int autoYear; //year of auto
string autoMake; //make of auto
int autoSpeed; //speed of auto
cout << "Enter the year model of your car. ";
cin >> autoYear;
cout << "Enter the make of your car. ";
cin >> autoMake;
auto.setYear(autoYear); //stores input year
auto.setMake(autoMake); //stores input make
}
And the header, Car.h:
#include <iostream>
#include <string>
using namespace std;
#ifndef CAR_H
#define CAR_H
class Car
{
private:
int yearModel;
string make;
int speed;
public:
Car();
void accel();
void brake();
void setSpeed(int newSpeed);
int getSpeed();
void setMake(string newMake);
string getMake();
void setYearModel(int newYearModel);
int getYearModel();
};
#endif
I also have errors for missing semicolons every time my object auto appears ("expected ; before auto" and "expected primary-expression before auto"). What could be the problem?
auto is a keyword. You'll need to pick a different name.