我一直在我的代码中遇到各种错误,有人可以通过编译器运行它并帮我解决它们吗?我修复了一些,我现在得到的那个是没有匹配函数来调用'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();
}
答案 0 :(得分:0)
您没有Classrooms
构造函数需要(string, int, double, double)
,因此,此ClassRooms(roomNum, seats, len, wid)
是错误的。您可以轻松地将适当的构造函数添加到Classrooms
。
ClassRooms(string roomNum, int seats, double len, double wid){
//do something
}