Looping with classes and pointers

Ok so I'm working on an assignment that prompts the user for a number of rooms and then asks the user for the length and width of each room. When I compile, I get "expected unqualified-id before [ token" on lines 36, 37, 40, 41 Here is the test code I'm working with. Any help would be appreciated.

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
45
46
47
48
#include <iostream>
using namespace std;

class CRoom
{
    public:
        CRoom(void){m_length = m_width = 0;}
        CRoom(int lVal, int wVal){m_length = lVal; m_width = wVal;}
        int GetLength(void){return m_length;}
        int GetWidth(void){return m_width;}
        void SetLength(int lVal){m_length = lVal;}
        void SetWidth(int wVal){m_width = wVal;}
    private:
        int m_length;
        int m_width;
};

int main(void)
{
    auto    CRoom   *rooms;
    auto    int     numRooms;
    auto    int     index;

    cout << "Enter # of rooms: ";
    cin >> numRooms;

    rooms = new CRoom[numRooms];

    auto    int     len, wid;

    for (index = 0; index < numRooms; ++index)
        {
        cout << "Room #" << index + 1 << ": " << endl;
        cout << "\tLength: ";
        cin >> len;
        CRoom[numRooms].GetLength(len);
        CRoom[numRooms].SetLength();
        cout << "\tWidth: ";
        cin >> wid;
        CRoom[numRooms].GetWidth(wid);
        CRoom[numRooms].SetWidth();
        }

    delete []rooms;

    return 0;
}
Last edited on
wow I haven't seen auto used like that in forever.

Just so you know, you don't have to type auto in those cases. It's implied. It's actually kind of weird of you to be using it.

anyway your problem is this:

CRoom[numRooms].GetLength(len);

CRoom is a class, but you're [indexing] it like it were a pointer/array.

You probably meant to do this:

rooms[index].GetLength(len);

Note you should index 'rooms' (your array) not CRoom (the class name)
also note you should use 'index' to index it and not 'numRooms' as numRooms will cause you to go out of bounds of your array.
Hmm, now I'm getting a whole new set of errors

1
2
3
4
5
6
7
8
test.cpp:36: error: no matching function for call to âCRoom::GetLength(int&)â
test.cpp:9: note: candidates are: int CRoom::GetLength()
test.cpp:37: error: no matching function for call to âCRoom::SetLength()â
test.cpp:11: note: candidates are: void CRoom::SetLength(int)
test.cpp:40: error: no matching function for call to âCRoom::GetWidth(int&)â
test.cpp:10: note: candidates are: int CRoom::GetWidth()
test.cpp:41: error: no matching function for call to âCRoom::SetWidth()â
test.cpp:12: note: candidates are: void CRoom::SetWidth(int)


Also I know that 'auto' is deprecated, its how out teacher taught us. I need more than two hands to count the number of times people have commented on that haha.
The way they work is like this:

1
2
3
4
5
6
7
8
9
10
int whatever = rooms[index].GetLength();

// 'whatever' now holds the length of the room

//=====


rooms[index].SetLength( some_new_length );

// this changes the length of the room to 'some_new_length' 
With that last bit of code and a little tweaking, I got it to compile. Thanks Disch.
Topic archived. No new replies allowed.