Working with class data

I'm dynamically creating class information but I for some reason, can't figure out how to change the data through the system.

I've tried...
potential->price() = (potential->price() - tradeNegotiation)
However I get mysterious errors... lvalue required as left operand of assignment

Now I'm using something similar to the pointers to classes tutorial by Juan, unfortunately it doesn't cover my topic, or at least I can't find an apt solution.

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
33
34
35
36
37
38
39
40
41
42
class car
{
	private:

	
	string carModel;
	unsigned int price;

	public:
		car 
		(string carModel, unsigned int pri); 
		~car (){}
		string returnModel ()
		{
			return (returnModel);
		}
int returnPrice ()
{
return (price)
};

car:car (string carMod, unsigned int pri):
carModel(carMod),
price(pri){}

void trade(car*potential, car*dealer);

int main()
{
trade(potential, dealer);
return 0;
}

void trade(car*potential, car*dealer)
{
int neg;
cout << "engaged in trade for " << potential->returnModel();
neg = (dealer->//classfunctionhere// - potential->//classfunctionhere// <- this bit appears to work. 
potential->returnPrice() = potential->returnPrice() - neg; //any idea? 

}


I've tried getting the price itself, but to no avail, since it's private. Hint anyone?
Last edited on
Either:

1) make a 'setPrice' function and do potential->setPrice( potential->getPrice() - neg );

or

2) make the 'price' member public and access it directly.


I'd probably recommend option 2, since 'price' likely doesn't have any impact on any other members of the 'car' class, and therefore doesn't really need to be private.
I'm trying 1)

However, I seem to be getting (neg wasn't declared in this scope)

I think it's because I'm not setting it up right however.
Add a little polish and shine and I gather your looking for a little something like this:
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <string>

class car
{
	private:
		std::string carModel;
		unsigned int price;

	public:
		car( std::string carModel, unsigned int pri ); 
		~car(){}

		std::string returnModel ()
		{
			return carModel;
		}
		int getPrice()
		{
			return price;
		}
		void setPrice(int pri)
		{
			price = pri;
		}
};

car::car( std::string carMod, unsigned int pri ):
carModel(carMod),
price(pri)
{
}

void trade(car*potential, car*dealer);

int main()
{
	car mod1("Holden", 21300);
	car mod2("Ford", 23100);

	trade(&mod1, &mod2);
	return 0;
}

void trade(car*potential, car*dealer)
{
	int neg;
	std::cout << "engaged in trade for " << potential->returnModel() << std::endl;
	neg = (dealer->getPrice() - potential->getPrice());
	potential->setPrice((potential->getPrice() - neg));
}
Topic archived. No new replies allowed.