Hello all smart people!

My problem is that the code below somehow should be able to summarize the double betalat_andras and the double skyldig (not with each other) for each specific person. That is if a Person Bob is added with betalat_andras = 1 and skyldig = 2 and then Bob is added with betalat_andras = 2 and skyldig = 3 then the result should be only one Person in the array but with values betalat_andras = 3 and skyldig = 5.

What happens right now is that I get an error C2106 '=' : left operand must be l-value

So my guess(or maybe it should be hope) is that there is a way around this.

Restrictions: No global variables. No inheritence and other advanced class "techniques"(Not part of this course). No vectors (Not part of this course).

I have cut out the parts of code, I think is what is needed for explaining the problem.

//Just to be safe I added the class definitions
class Person
{
private:
	string namn;
	double betalat_andras;	//how much one person has payed
	double skyldig;		//how much one person owes
public:
	Person(); //Standard Constructor
	Person(string n, double b, double s); //Overloaded(is that the english name?) constructor
	double haemta_betalat();
	double haemta_skyldig();
	string haemta_namn();
	void skrivUt();
	//ev. annat
};

class PersonLista
{
private:
	int antalPers;
	double sumBetalat;
	double sumSkyldig;
	Person pers[MAX_PERSONER];

public:
	PersonLista();
	~PersonLista();
	void laggTillEn( Person pny );
	void skrivUtOchFixa();
	double summaSkyldig();
	double summaBetalat();
	//...eventuellt div. annat...
 }; 

//Here we jump to my problem, it is in the bottom of what is shown of this class:
void PersonLista::laggTillEn(Person pny) //Adds Person[x] to the list of Persons
{
	antalPers++;
	sumBetalat = sumBetalat + pny.haemta_betalat(); //Summarize how much total payed
	sumSkyldig = sumSkyldig + pny.haemta_skyldig(); //Summarize how much total owed

	for (int i = 0; i < antalPers; i++)
	{
		if(pers[i].haemta_namn()== pny.haemta_namn())
		{
			cout << "Finns sen tidigare, lagras ej ekonomin summeras." << endl;
			cout << endl;
			antalPers = antalPers -1;
//Somehow summarizing for each time a specific name is added, this is not working
			pers[i].haemta_betalat() = pers[i].haemta_betalat() + pny.haemta_betalat(); 
//Somehow summarizing for each time a specific name is added, this is not working
			pers[i].haemta_skyldig() = pers[i].haemta_skyldig() + pny.haemta_skyldig(); 

			break;
		}

I am very much thankful for the help I can get.
Ziwx

Recommended Answers

All 2 Replies

>>pers.haemta_betalat() = pers.haemta_betalat() + pny.haemta_betalat();

haemta_betalat() is a function call, you can not add anything to its return value. What you want is this: pers[i].betalat_andras = pers[i].betalat_andras + pny.haemta_betalat(); But that might give you an error too since betalat_andras is a private member of the class. You could fix that too by writing a add function pers[i].add_betalat() = pers[i].haemta_betalat() + pny.haemta_betalat(); where add_betalat() is declare as this:

public:
     void add_betalat(double n) { betalat_andras += n;}

Ah! Of course, it seems so simple when it is explained to me :)

Big thanks for the quick reply!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.