lvalue error

Started by
7 comments, last by phresnel 15 years, 3 months ago
Hello. When I try to compile a certain file, I get the following error:
Quote: lvalue required as left operand of assignment
In the following piece of code:

			GET_PULLED_BY(GET_PULLING(ch), 0) = NULL;
		if (GET_PULLED_BY(GET_PULLING(ch), 1) == ch)
			GET_PULLED_BY(GET_PULLING(ch), 1) = NULL;
Advertisement
What is the return type of GET_PULLED_BY? If it's not a reference, you are not allowed to assign to it.
Is GET_PULLED_BY looks like a function to me.

With:
GET_PULLED_BY(GET_PULLING(ch), 0) = NULL;

You try to asign NULL to that function. But you can't asign anything to functions.
Quote:Original post by JvdWulp
You try to asign NULL to that function. But you can't asign anything to functions.

He's not assigning anything to the function. He's assigning to the return value of the function. As DevFred said, if the return value is a reference (and a non-constant such), you can assign to it.

But that assumes C++, which was not stated in the original post. In fact, not much was stated at all, really.
The entire function:
void extract_char(Creature ch) {	void display_statistics_to_char(Creature ch);	Creature k, temp;	Descr t_desc;	Object obj;	int i, freed = 0;	perform_dismount(ch);	if (GET_FED_ON_BY(ch)) {		GET_FEEDING_FROM(GET_FED_ON_BY(ch)) = NULL;		GET_FED_ON_BY(ch) = NULL;		}	if (GET_FEEDING_FROM(ch)) {		GET_FED_ON_BY(GET_FEEDING_FROM(ch)) = NULL;		GET_FEEDING_FROM(ch) = NULL;		}	if (GET_LEADING(ch)) {		GET_LED_BY(GET_LEADING(ch)) = NULL;		GET_LEADING(ch) = NULL;		}	if (GET_LED_BY(ch)) {		GET_LEADING(GET_LED_BY(ch)) = NULL;		GET_LED_BY(ch) = NULL;		}	if (GET_PULLING(ch)) {		if (GET_PULLED_BY(GET_PULLING(ch), 0) == ch)			GET_PULLED_BY(GET_PULLING(ch), 0) = NULL;		if (GET_PULLED_BY(GET_PULLING(ch), 1) == ch)			GET_PULLED_BY(GET_PULLING(ch), 1) = NULL;		GET_PULLING(ch) = NULL;		}	if (!IS_NPC(ch) && !ch->desc) {		for (t_desc = descriptor_list; t_desc; t_desc = t_desc->next)			if (t_desc->original == ch)				do_return(t_desc->character, NULL, 0, 0);		}	if (ch->in_room == NOWHERE) {		log("SYSERR: NOWHERE extracting char %s. (%s, extract_char)", GET_NAME(ch), __FILE__);		exit(1);		}	if (ch->followers || ch->master)		die_follower(ch);	/* Forget snooping, if applicable */	if (ch->desc) {		if (ch->desc->snooping) {			ch->desc->snooping->snoop_by = NULL;			ch->desc->snooping = NULL;			}		if (ch->desc->snoop_by) {			SEND_TO_Q("Your victim is no longer among us.\r\n", ch->desc->snoop_by);			ch->desc->snoop_by->snooping = NULL;			ch->desc->snoop_by = NULL;			}		}	/* transfer objects to room, if any */	while (ch->carrying) {		obj = ch->carrying;		obj_from_char(obj);		obj_to_room(obj, ch->in_room);		}	/* transfer equipment to room, if any */	for (i = 0; i < NUM_WEARS; i++)		if (GET_EQ(ch, i))			obj_to_room(unequip_char(ch, i), ch->in_room);	if (FIGHTING(ch))		stop_fighting(ch);	for (k = combat_list; k; k = temp) {		temp = k->next_fighting;		if (FIGHTING(k) == ch)			stop_fighting(k);		}	char_from_room(ch);	/* pull the char from the list */	REMOVE_FROM_LIST(ch, character_list, next);	if (ch->desc && ch->desc->original)		do_return(ch, NULL, 0, 0);	if (!IS_NPC(ch))		Crash_delete_crashfile(ch);	else {		if (GET_MOB_RNUM(ch) > -1)		/* if mobile */			mob_index[GET_MOB_RNUM(ch)].number--;		free_char(ch);		freed = 1;		}	if (!freed && ch->desc != NULL) {		STATE(ch->desc) = CON_MENU;		display_statistics_to_char(ch);		SEND_TO_Q(MENU, ch->desc);		}	else {  /* if a player gets purged from within the game */		if (!freed)			free_char(ch);		}	}


The code is C.
The problem is not the entire function, but the definition of GET_PULLED_BY and GET_PULLING.
No offense: I guess you really copied and pasted the code from somewhere without understanding it at all, because you would have recognized that we were not asking for that fuction, but rather for the definition of
GET_FEEDING_FROM(...)
and company.
Sorry, I've missed that.

Here's the definition:
#define GET_PULLED_BY(obj, i)	(i == 0 ? (obj)->pulled_by1 : (obj)->pulled_by2)


#define GET_PULLING(ch)			((ch)->char_specials.pulling)
Should be possible if "...->pulled_by[12]" and "....pulling" are non-const references, or if you overloaded the assignment operators for their types. Plus "...->pulled_by[12]" must be of the same type.

Example:
#include <iostream>struct Foo {    int &a;    int &b;    Foo (int& a, int& b) : a(a), b(b) {}};int main () {    int a, b;    Foo foo (a,b);    ((1<2) ? foo.a : foo.b) = 0;    ((1>2) ? foo.a : foo.b) = 1;    ::std::cout << "a=" << foo.a << '\n';    ::std::cout << "b=" << foo.b << '\n';    }

This topic is closed to new replies.

Advertisement