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

#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++;
 }
}


#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 

#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;
}

 

对上面错误的解释

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.

posted on 2012-01-18 17:34  不知道  阅读(3783)  评论(0编辑  收藏  举报

导航