H:\CompSci\classroom management\ClassRooms.cpp|7|error: call of overloaded 'ClassRoom(int&)' is ambiguous

I am working on a homework assignment involving classes and objects and I cannot seem to get it to compile. I keep getting this error. And this is the segment of code I am getting the error in. I do not quite understand why I am getting that error or what it means. The error lies within the bolded text.

1
2
3
4
5
6
ClassRooms::ClassRooms(int rooms)
{
    numRooms = rooms;
    roomIndex = 0;
    rooms = new ClassRoom(rooms);
}


The code also corresponds to the following header:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    #ifndef CLASS_ROOMS_H
#define CLASS_ROOMS_H

#include "ClassRoom.h"

class ClassRooms {
    private:
        int numRooms;
        int roomIndex;
        ClassRoom *rooms;

    public:
        ClassRooms(int rooms = 100);
        void addRoom(ClassRoom room);
        string findRoom(int seats);
};

#endif 


I would post the rest of the class' code, but I first need to know if the reason for the error can be found within the snippets of code I just showed here.
1
2
3
4
5
6
ClassRooms::ClassRooms(int nrooms)
{
    numRooms = nrooms;
    roomIndex = 0;
    rooms = new ClassRoom[nrooms];
}
Already tried that and it is still not compiling. I think maybe I should post the rest of my code in the assignment

Here is the rest of the code of my ClassRooms class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include "ClassRooms.h"

ClassRooms::ClassRooms(int nrooms)
{
    numRooms = nrooms;
    roomIndex = 0;
    rooms = new ClassRoom(nrooms);
}

void ClassRooms::addRoom(ClassRoom room)
{
    if(roomIndex < numRooms){
        rooms[roomIndex] = room;
        roomIndex++;
    }
}

string ClassRooms::findRoom(int seats)
{
    for(int i = 0; i < roomIndex; i++)
        if(rooms[i].getSeats() == seats)
            return &rooms[i].getRoomNumber();

    return NULL;
}

ClassRooms::~ClassRooms()
{
    delete [] rooms;
}



The ClassRoom (singular) header file and class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef CLASS_ROOM_H
#define CLASS_ROOM_H


#include<iostream>
#include<string>

using namespace std;

class ClassRoom {
	private:
	    string roomNum;
		int numSeats;
		double length, width;
    public:
        ClassRoom(string rn = "",int seats = 0, double len = 0, double wid = 0);
        ClassRoom(string line = "");
        string getRoomNumber();
        int getSeats();
        double getAreaPerSeat();
};

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "ClassRoom.h"

#include <sstream>

ClassRoom::ClassRoom(string rn,int seats, double len, double wid)
{
    roomNum = rn;
    numSeats = seats;
    length = len;
    width = wid;
}

ClassRoom::ClassRoom(string line)
{
    istringstream istr(line);
    roomNum = "";
    numSeats = 0;
    length = 0;
    width = 0;
    istr >> roomNum >> numSeats >> length >> width;
}

string ClassRoom::getRoomNumber(){

return roomNum;

}

int ClassRoom::getSeats(){

return numSeats;

}

double ClassRoom::getAreaPerSeat(){

return length*width;

}


And finally here is my Main(still in the works):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include "ClassRoom.h"
#include "ClassRooms.h"
#include <fstream>
#include <sstream>

using namespace std;

ClassRooms classrooms;

int main(){

string roomNum;
int seats;
double length;
double width;

ifstream data ("rooms.txt");

while(data >> roomNum >> seats >> length >> width)
{
    classrooms.addRoom(ClassRoom(roomNum,seats,length,width));
    cout << roomNum << " " << seats << " " << length << " " << width << endl;
}

return 0;

}


So the purpose of program that I am trying to create is to read a text file that has a list of classrooms with the room letter and number, number of seats, and length and width. This is the following info in the text file:

ES2.410 110 50 60
ES2.412 110 60 60
ES2.414 110 60 65
ES2.415 110 50 60
EN2.112 50 30 40
EN2.108 55 40 50
EN2.104 52 35 40
EN2.116 53 40 50
EN2.112 50 50 40

The list is to be read into an array of ClassRooms and allow the user to search the classroom that fits his needs based on the number of seats and dimensions. This is what the input and output should look like.

Enter MAX capacity: 50
Your best room is EN2.108
Enter MAX capacity: 100
Your best room is ES2.414

But I need to fix the issue with that error first. So any help would be much appreciated.
Hate to be all pushy here but I really need help with this ASAP. I only have tonight and tomorrow to finish this but I also have other assignments I need to do as well. So can someone please help?
Take a look again at the error message: call of overloaded 'ClassRoom(int&)' is ambiguous. What it means that when you call the constructor of ClassRoom with an int, it does not know what constructor to use. Create an empty constructor ClassRoom() and change ClassRoom(string rn = "",int seats = 0, double len = 0, double wid = 0) and ClassRoom(string line = "") to ClassRoom(string rn,int seats = 0, double len = 0, double wid = 0) and ClassRoom(string line)
Ok made the changes you told me about but I still am getting the error. Here's the code so you can see if I am doing things right or not.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef CLASS_ROOM_H
#define CLASS_ROOM_H


#include<iostream>
#include<string>

using namespace std;

class ClassRoom {
	private:
	    string roomNum;
		int numSeats;
		double length, width;
    public:
        ClassRoom();
        ClassRoom(string rn,int seats = 0, double len = 0, double wid = 0);
        ClassRoom(string line );
        string getRoomNumber();
        int getSeats();
        double getAreaPerSeat();
};

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "ClassRoom.h"

#include <sstream>

ClassRoom::ClassRoom()
{

}

ClassRoom::ClassRoom(string rn ,int seats , double len , double wid )
{
    roomNum = rn;
    numSeats = seats;
    length = len;
    width = wid;
}

ClassRoom::ClassRoom(string line)
{
    istringstream istr(line);
    roomNum = "";
    numSeats = 0;
    length = 0;
    width = 0;
    istr >> roomNum >> numSeats >> length >> width;
}

string ClassRoom::getRoomNumber(){

return roomNum;

}

int ClassRoom::getSeats(){

return numSeats;

}

double ClassRoom::getAreaPerSeat(){

return length*width;

}
This is so getting on my nerves now...
1
2
        ClassRoom(string rn,int seats = 0, double len = 0, double wid = 0);
        ClassRoom(string line );

This is still ambiguous. Which constructor am I calling if I say new ClassRoom("")?

rooms = new ClassRoom(nrooms);
This should be
rooms = new ClassRoom[nrooms];
Last edited on
In your ClassRooms.cpp:
rooms = new ClassRoom(nrooms);
Shouldn't this be rooms = new ClassRoom[nrooms] (square brackets)?

When I fix that, I'm just left with two more errors:
classrooms.cpp: In member function 'std::string ClassRooms::findRoom(int)':
classrooms.cpp:22:44: error: taking address of temporary [-fpermissive]
             return &rooms[i].getRoomNumber();
                                            ^
classrooms.cpp:22:44: error: could not convert '& ClassRoom::getRoomNumber()()'
from 'std::string* {aka std::basic_string<char>*}' to 'std::string {aka std::basic_string<char>}'
classrooms.cpp: At global scope:
classrooms.cpp:27:25: error: definition of implicitly-declared 'ClassRooms::~ClassRooms()'
 ClassRooms::~ClassRooms()
                         ^

Now, the first two errors are in your ClassRooms::findRoom function. I'm not exactly sure what you're trying to return from this function, but here are my two guesses:
1. You're trying to return the room number (as a string).
In this case, you need to get rid of the & in return &rooms[i].getRoomNumber();, since the & will return the address of the copy of the roomNum variable returned by getRoomNumber, not the actual string itself.
I would also change your return NULL; to something like return string(); or something (to return an empty string), or otherwise return some kind of string representing a "room not found" situation, since typically, NULL is just used for pointers and not for strings like what you have here. (It still compiles either way.)

2. You're trying to return a pointer to the roomNum variable holding the room number.
In this case, your getRoomNumber() function currently returns a temporary copy of roomNum, so that's not what you want, so you'll need to change string ClassRoom::getRoomNumber() to either string& ClassRoom::getRoomNumber() or const string& ClassRoom::getRoomNumber(), depending on whether or not you want to be able to change the room number (my guess is that you don't, so you'll want to use the latter declaration).
Also, you'll have to change string ClassRooms::findRoom(int seats) to either string* ClassRooms::findRoom(int seats) or const string* ClassRooms::findRoom(int seats), depending on which one of the two you picked for ClassRoom::getRoomNumber().

For the last error, you just forgot to declare your destructor for ClassRooms in your header file.

Hopefully that made sense.
Topic archived. No new replies allowed.