Error: a nonstatic member reference must be relative to a specific object

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
#include <iostream>
#include <string>
#include "order.h"
#include "date.h"
#include "room_list.h"
#include "room_node.h"
#include "room.h"
#include "hotel.h"

Order::Order(Date check_in, Date check_out,int owner_id,int order_id){
 //..

}

void Order::add_room_to_order(int num_of_beds) {
	found=0; 

	while (!found ) {
		nextRoomNode = Room_List::get_first(); //Error: a nonstatic member reference must be relative to a specific object.
		if (  num_of_beds){
			found=1;
			nextRoomNode->set_order_id(Order::get_order_id());
		}
	num_of_rooms++;
	}
}

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
#ifndef _ORDER_H_
#define _ORDER_H_

#include "date.h"
#include "room_node.h"
#include "room_list.h"

class Order{
public:
//..
	void print_order_info();
	void add_room_to_order(int num_of_beds);
	string get_check_in_date();
	string get_check_out_date();
	int get_owner_id();
	int get_order_id();

private:
	int num_of_rooms,i,found;
	Room_Node* nextRoomNode;
	Room* nextRoom;
	int _order_id;
	int _owner_id;
	int _num_of_nights;
	long int _new_num_of_nights;
	Date _new_check_in;
	Date _new_check_out;
	Date _check_in;
	Date _check_out;

};
#endif  


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include "room_list.h"
#include "room.h"
#include "room_node.h"

Room_List::Room_List(){
	firstRoomNode = NULL;
}

void Room_List::add_room(Room* room) {
	Room_Node* tmpRoomNode = new Room_Node(room,firstRoomNode);
	firstRoomNode = tmpRoomNode;
}
Room_Node* Room_List::get_first(){
	return firstRoomNode;
}


I don`t know what causing this error under - Room_List -!
Please I need some help.
Last edited on
Room_List is a class and get_first is a method. method is a function that can operate on this pointer. Then you call a method, the identifier before . or -> specifies what this will be. You can think of it as another argument. Now you call get_first without specifying what object will be this, while get_first needs to know it.
If get_first didn't need to know it you could declare it static, however it clearly does (it returns this->firstRoomNode ).
The problem in your code is that Object has no knowledge of any Room_List so it can't pass anything as this. You could either add a member my_room_list of type Room_List and change Room_List:: to my_room_list. . Or you could have Order inherit from Room_List (I cannot know if that would make sense though) so that this from add_room_to_order can be used in get_first.
Topic archived. No new replies allowed.