14 Replies - 1410 Views - Last Post: 09 October 2012 - 09:41 AM Rate Topic: -----

#1 B_M  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 01-August 11

Passing class into function error:

Posted 09 October 2012 - 02:19 AM

For the following code, the two classes are assignment questions. I am asked to create the implementation part for the classes.

Header
#ifndef HEADER_H
#define	HEADER_H

class Course
{
private:
    int sectionNum;
    int enrollLimit;
    int roomNum;
    
public:
    Course(const int sec, const int lim);
    void showClassRoom(void);
    void roomAssign(Room &aRoom);
};

class Room
{
friend void Course::roomAssign(Room &aRoom);
private:
    int roomNum;
    int seatLimit;
    int roomIsOccupied;
public:
    void setRoomData (const int rm, const int seats, const int occ);
};

#endif



Implementation
#include "HEADER.h"
#include <iostream>
#include <string>
using namespace std;

Course::Course(const int sec, const int lim)
{
    sectionNum = sec;
    enrollLimit = lim;
    roomNum = 0;
}


void Course::showClassRoom()
{
    if (roomNum != 0)
    {
        cout<<"Course Section: "<<sectionNum
        <<"\n Room Number: "<<roomNum<<endl;
    }
    else
        cout<<"No room assignment has been made."<<endl;
}


void Course::roomAssign(Room& aRoom)
{
    
    if (roomNum == 0)
        if (*aRoom.roomIsOccupied == 0 && *aRoom.seatLimit >= enrollLimit)
            *aRoom.setRoomData(1,1,1);
}


void Room::setRoomData (const int rm, const int seats, const int occ)
{
    roomNum = rm;
    seatLimit = seats;
    roomIsOccupied = occ;
    
}




For the above code, I am getting 3 errors for the classes.
HEADER.h:14: error: 'Room' has not been declared
HEADER.h:19: error: prototype for 'void Course::roomAssign(Room&)' does not match any in class 'Course'
HEADER.h:14: error: candidate is: void Course::roomAssign(int&)

And for the code below
void Course::roomAssign(Room& aRoom)
{
    
    if (roomNum == 0)
        if (*aRoom.roomIsOccupied == 0 && *aRoom.seatLimit >= enrollLimit)
            *aRoom.setRoomData(1,1,1);
}


The error code says roomIsOccupied, seatLimit are private... I thought the friend function gives permission to use the variables?

I tried to search online on how to generate friend function but i can't find any that do it in the above way. (the friend function created in the class "Room".)

Any thought?

Is This A Good Question/Topic? 0
  • +

Replies To: Passing class into function error:

#2 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Passing class into function error:

Posted 09 October 2012 - 02:42 AM

Well...I think I can solve your first error. It is because 'Room' is a class, so without inheriting from the room class, the class 'course' cannot use the instances of room.

If you could transfer the function "void roomAssign(Room &aRoom)" to the Room class, it should probably remove your first error.

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6048
  • View blog
  • Posts: 23,473
  • Joined: 23-August 08

Re: Passing class into function error:

Posted 09 October 2012 - 03:03 AM

No, that's wrong.

C++ is read from the top down. Therefore, because Room appears in the file after Course, the compiler knows nothing about the Room class when it tries to compile the Course class.

The answer is using forward declaration.
Was This Post Helpful? 2
  • +
  • -

#4 B_M  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 01-August 11

Re: Passing class into function error:

Posted 09 October 2012 - 03:13 AM

View Postraghav.naganathan, on 09 October 2012 - 02:42 AM, said:

Well...I think I can solve your first error. It is because 'Room' is a class, so without inheriting from the room class, the class 'course' cannot use the instances of room.

If you could transfer the function "void roomAssign(Room &aRoom)" to the Room class, it should probably remove your first error.

regards,
Raghav



Thanks raghav. I believe you are right. But that is the question. So I am guessing, perhaps there is another way to handle it?
Was This Post Helpful? 0
  • +
  • -

#5 B_M  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 01-August 11

Re: Passing class into function error:

Posted 09 October 2012 - 03:31 AM

View PostJackOfAllTrades, on 09 October 2012 - 03:03 AM, said:

No, that's wrong.

C++ is read from the top down. Therefore, because Room appears in the file after Course, the compiler knows nothing about the Room class when it tries to compile the Course class.

The answer is using forward declaration.



Thanks Jack. So that solve the error in the class. So my understanding for the following is that since I have pass the whole class into the roomAssign function, why can't I use the variables?
void Course::roomAssign(Room &aRoom)
{
    
    if (roomNum == 0)
        if (*aRoom.roomIsOccupied == 0 && *aRoom.seatLimit >= enrollLimit)
            *aRoom.setRoomData(1,1,1);
}



I am getting error C2100: illegal indirection.
Was This Post Helpful? 0
  • +
  • -

#6 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Passing class into function error:

Posted 09 October 2012 - 03:57 AM

Did you read the error? It says illegal indirection. Do you understand what indirection is?

*aRoom.roomIsOccupied

The * operator here is the indirection operator. Why are you trying to dereference aRoom?
Was This Post Helpful? 1
  • +
  • -

#7 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Passing class into function error:

Posted 09 October 2012 - 04:10 AM

View PostJackOfAllTrades, on 09 October 2012 - 03:33 PM, said:

No, that's wrong.

C++ is read from the top down. Therefore, because Room appears in the file after Course, the compiler knows nothing about the Room class when it tries to compile the Course class.

The answer is using forward declaration.


Thanks Jack, but I have a minor doubt. Is it possible to use inheritance instead of the friend function? What I mean to say is if we use inheritance, can we probably do away with the friend function? I will be really glad if you could clear my doubt. :)

regards,
Raghav

This post has been edited by raghav.naganathan: 09 October 2012 - 04:11 AM

Was This Post Helpful? 0
  • +
  • -

#8 B_M  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 01-August 11

Re: Passing class into function error:

Posted 09 October 2012 - 05:22 AM

View PostOler1s, on 09 October 2012 - 03:57 AM, said:

Did you read the error? It says illegal indirection. Do you understand what indirection is?

*aRoom.roomIsOccupied

The * operator here is the indirection operator. Why are you trying to dereference aRoom?


No, i do not know what is indirection.
I thought it (the *) was the way to point to the address "&aRoom".

Now I remove the * and it build correctly. Thanks.
Was This Post Helpful? 0
  • +
  • -

#9 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5795
  • View blog
  • Posts: 12,628
  • Joined: 16-October 07

Re: Passing class into function error:

Posted 09 October 2012 - 05:37 AM

Why on is all that crap hidden in Room in the first place? Public methods should expose that data. Then, you don't need any bloody friends. Pointers? Huh?

e.g.
bool Course::roomAssign(const Room& room) {
	// occupied shold be boolean, not int
	if (roomNum == 0 && room.isOccupied() && room.getSeatLimit() >= enrollLimit) {
		// wait, wtf?  why are you screwing with room?
		// *aRoom.setRoomData(1,1,1);
	}
	return false;
}



Hmm... I think there's a disconnect as to how this should work. The reason you'd make a friend of roomAssign is so that Room would have full control of the transaction. Something like:
bool Room::assign(Course &course) {
	if (!isOccupied() && !course.hasRoom() && getSeatLimit() >= course.getEnrollLimit()) {
		course.roomAssign(*this);
		return true;
	}
	return false;
}

void Course::roomAssign(const Room& room) { this->room = room; }



Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#10 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6048
  • View blog
  • Posts: 23,473
  • Joined: 23-August 08

Re: Passing class into function error:

Posted 09 October 2012 - 06:22 AM

View Postraghav.naganathan, on 09 October 2012 - 07:10 AM, said:

Thanks Jack, but I have a minor doubt. Is it possible to use inheritance instead of the friend function? What I mean to say is if we use inheritance, can we probably do away with the friend function? I will be really glad if you could clear my doubt. :)

regards,
Raghav


No! No inheritance! Is a Room a Course? Is a Course a Room? NO! Google Liskov Substitution Principle.

I see it a couple of different ways, depending on what your program would be doing. For a class management program you might say a Course HAS a Room; for a room management program you might say a Room HAS (a) Course(s).
Was This Post Helpful? 0
  • +
  • -

#11 B_M  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 01-August 11

Re: Passing class into function error:

Posted 09 October 2012 - 08:44 AM

Thanks everyone. Below is my final answer. Hope that someone can have a look and correct my code. I am doing the Implementation part and the main function.

Header (given in the question)
#ifndef HEADER_H
#define	HEADER_H
#include <string>
#include <iostream>
using namespace std;

class Room;

class Course
{
private:
    int sectionNum;
    int enrollLimit;
    int roomNum;
    
public:
    Course(const int sec, const int lim);
    void showClassRoom(void);
    void roomAssign(Room &aRoom);
};

class Room
{
friend void Course::roomAssign(Room &aRoom);
private:
    int roomNum;
    int seatLimit;
    int roomIsOccupied;
public:
    void setRoomData (const int rm, const int seats, const int occ);
};



Implementation
#include "HEADER.h"
#include <iostream>
#include <string>
using namespace std;

Course::Course(const int sec, const int lim)
{
    sectionNum = sec;
    enrollLimit = lim;
    roomNum = 0;
}


void Course::showClassRoom()
{
    if (roomNum != 0)
    {
        cout<<"Course Section: "<<sectionNum
        <<"\nRoom Number: "<<roomNum<<endl;
    }
    else
        cout<<"No room assignment has been made."<<endl;
}

void Course::roomAssign(Room &aRoom)
{
    
    if (roomNum == 0)
    {
        if (aRoom.roomIsOccupied == 0 && aRoom.seatLimit >= enrollLimit)
        {
            aRoom.roomIsOccupied = 1;
            roomNum = aRoom.roomNum;
        }
    }
            
}


void Room::setRoomData (const int rm, const int seats, const int occ)
{
    roomNum = rm;
    seatLimit = seats;
    roomIsOccupied = occ;
    
}



Main
#include "HEADER.h"
#include <iostream>
#include <string>
using namespace std;

int main (){
    //A2a,b,c,d
    Course English(801,25);
    Course Accounting(802,15);
    Course Biology(803,40);
    
    //A2e
    Room aryObj[5];
    
    //A2f
    aryObj[0].setRoomData(1,40,0);
    aryObj[1].setRoomData(2,23,0);
    aryObj[2].setRoomData(3,24,0);
    aryObj[3].setRoomData(4,26,1);
    aryObj[4].setRoomData(5,25,0);
    
    //A2g
    for (int i=0;i<=6;i++)
    {
        English.roomAssign(aryObj[i]);
        Accounting.roomAssign(aryObj[i]);
        Biology.roomAssign(aryObj[i]);
    }
    
    //A2h
    cout<<"English course\n";
    English.showClassRoom();
    cout<<"\nAccounting course\n";
    Accounting.showClassRoom();
    cout<<"\nBiology course\n";
    Biology.showClassRoom();
    
    return 0;
}



Here is the question:
All questions in this section are based on the classes defined below.
class Course
{
private:
    int sectionNum;
    int enrollLimit;
    int roomNum;
    
public:
    Course(const int sec, const int lim);
    void showClassRoom(void);
    void roomAssign(Room &aRoom);
};

class Room
{
friend void Course::roomAssign(Room &aRoom);
private:
    int roomNum;
    int seatLimit;
    int roomIsOccupied;
public:
    void setRoomData (const int rm, const int seats, const int occ);
};



Question A1
Given the above class definitions for Course and Room, write the implementation for the following:
a. the Course constructor which assigns a section number and maximum class size, and initializes the assigned room number to 0.
b. the showClassRoom() function which displays the course section and room number with an additional message if no room assignment has been made for the Course.
c. the setRoomData() function of the Room class which assigns the three arguments to the appropriate fields.
d. the roomAssign() function which is a member of the Course class and takes a Room argument. If a Course does not have a room assignment, that is, if the roomNum for a Course is still 0, then the roomAssign function checks two fields belonging to the Room argument. If the Room is not occupied(that is, if the value of roomIsOccupied is 0) and the Room can hold the Course(that is, if the seatLimit for the Room is greater than or equal to the number of potential students in the Course), the Room is then marked as occupied and the room number is assigned to the Course.

Question A2
a. Write the main function that demonstrates how the classes work.

Within the main function,
b. create an object for Course called English which is given section number 801 and enrollment limit of 25.
c. create an object for Course called Accounting which is given section number 802 and enrollment limit of 15.
d. create an object for Course called Biology which is given section number 803 and enrollment limit of 40.
e. create an array of five Room objects.
f. assign each Room a room number, student capacity and a value representing occupied or unoccupied using the setRoomData() member functions.
g. write a for loop to iterate through all the Rooms and determine whether each can accommodate the English, Accounting, or Biology Course. Use the roomAssign() function.
h. write the appropriate statements to display each Course.
Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg  Icon User is offline

  • member icon


Reputation: 4002
  • View blog
  • Posts: 12,346
  • Joined: 25-December 09

Re: Passing class into function error:

Posted 09 October 2012 - 08:47 AM

Do you have some question about your code? You have given us the assignment but you have not asked any questions.

Jim
Was This Post Helpful? 0
  • +
  • -

#13 B_M  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 01-August 11

Re: Passing class into function error:

Posted 09 October 2012 - 08:50 AM

Sorry guys, I do not know how to edit my last post.
The final code I posted above is working fine. Just that I am not sure if I am doing it according to the questions' requirements.

Thanks again for all the help :)
p/s: this is not an assignment that I will get marks for my subject, but for practice purpose.
Was This Post Helpful? 0
  • +
  • -

#14 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5795
  • View blog
  • Posts: 12,628
  • Joined: 16-October 07

Re: Passing class into function error:

Posted 09 October 2012 - 09:14 AM

Wait, that mess was given to you?!? Man, I'm sorry.

Right, aryObj is a crappy name, I'd go with something like more descriptive. I'd also get rid of your magic number.
const int ROOM_COUNT = 5;
Room rooms[ROOM_COUNT];



You have this code: for (int i=0;i<=6;i++). Think it over. That's two more rooms than you have. Always use ROOM_COUNT for your loops.

You are told to "determine whether each can accommodate". You aren't doing that. Your assigning all your Rooms to the same courses with each iteration. You also aren't checking anything, like if the Course was already assigned to a room.
Was This Post Helpful? 1
  • +
  • -

#15 B_M  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 01-August 11

Re: Passing class into function error:

Posted 09 October 2012 - 09:41 AM

View Postbaavgai, on 09 October 2012 - 09:14 AM, said:

Wait, that mess was given to you?!? Man, I'm sorry.

Right, aryObj is a crappy name, I'd go with something like more descriptive. I'd also get rid of your magic number.
const int ROOM_COUNT = 5;
Room rooms[ROOM_COUNT];



You have this code: for (int i=0;i<=6;i++). Think it over. That's two more rooms than you have. Always use ROOM_COUNT for your loops.

You are told to "determine whether each can accommodate". You aren't doing that. Your assigning all your Rooms to the same courses with each iteration. You also aren't checking anything, like if the Course was already assigned to a room.



No worry, just that I didn't explain well enough.
But your previous post has helped me on getting the roomAssign function to what I have now, which is very much different from what I have earlier (with zero understanding).

Appreciate that you had a look at my final code. And thanks for pointing out the above mistakes and the checking part. I will do that.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1