Error C2105: '++' needs l-value



Here is my code
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <string>
#include <iostream>
#include <iomanip>

using namespace std;



	class Acct
	{
	private:
		double balance;
		static const int transactions = 0;
	public:
		static const int numberCreated = 0;


		Acct();
		Acct(double initialAmount);
		void Deposit(double amount);
		double Withdraw(double amount);
		double GetBalance();
		static int GetTransactionCount();
	};



Acct::Acct()
{
	balance = 0;
	numberCreated++;//Error C2105: '++' needs l-value
	

}

Acct::Acct(double initialAmount)
{
	balance = initialAmount;
	numberCreated++;//Error C2105: '++' needs l-value
	

}

void Acct::Deposit(double amount)
{
	balance += amount;
	transactions++;//Error C2105: '++' needs l-value
	
}

double Acct::Withdraw(double amount)
{
	if (balance >= amount)
	{
		balance -= amount;
		transactions++;//Error C2105: '++' needs l-value
	
		return balance;

	}
	else
		return -1.0;
}

double Acct::GetBalance()
{
	transactions++;//Error C2105: '++' needs l-value
	
	return balance;
}

int Acct::GetTransactionCount()
{
	return transactions;
};

static void main()
{
	Acct *myAcct = new Acct(25.00);
	myAcct->Deposit(700.00);
	if (myAcct->Withdraw(300.00) < 0)
		std::cout << "Insufficient funds" << std::endl;
	if (myAcct->Withdraw(450.00) < 0)
		std::cout << "Insufficient funds" << std::endl;

	std::cout << "My balance after completing transactions is "  << myAcct->GetBalance() << std::endl;
	std::cout << "The number of transactions is " << Acct::GetTransactionCount() << std::endl;
	Acct *yourAcct = new Acct();
	yourAcct->Deposit(1234.56);

	std::cout << "Your balance after completing transactions is " << yourAcct->GetBalance() << std::endl;
	std::cout << "The number of transactions is " << Acct::GetTransactionCount() << std::endl;
	std::cout << "Number of accounts created is " + Acct::numberCreated << std::endl;

}


I want my output to look like this:
Insufficient funds
My balance after completing transactions is $425.00
The number of transactions is 2
Your balance after completing transactions is $1,234.56
The number of transactions is 4
Number of accounts created is 2
Press any key to continue . . .




numberCreated looks like it is declared to be constant, so you can't modify it. It's the same for transactions.
Last edited on
okay..how do I create a counter for the number of transactions and accounts created then without static constant integers because i got this code to work when it was written in C# initially this way but trying to convert it over does not work.
Drop the const keyword so you can increment them. The problem, then, is that you cannot initialise static members in place like that. Initialize static members outside of the class (in the top of the source file if you are using separate header and source files).

1
2
3
4
5
6
7
class Acct
{
    static int numberCreated;
    //...
};

int Acct::numberCreated = 0;

Last edited on
1) Remove the 'const' keyword. That makes it constant (ie: you can't modify it)

2) Remove the = 0 from the class body. You can't initiallize non-const members in the class body like that

3) Instantiate the transactions variable by putting it outside the class


Here's what the code should look like:

1
2
3
4
5
6
7
8
9
10
class Acct
{
//...
    static int transactions;  // note, not 'const'
//...
};

// then, globally, in ONE AND ONLY ONE cpp file:
int Acct::transactions = 0;  // instantiate 'transactions' and assign it its initial value

Thank you all here is the working code:
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
	#include <string>
	#include <iostream>
	#include <iomanip>

	using namespace std;

		
		class Acct
		
		{

		private:
			double balance;
			static int transactions;
		public:
			static int numberCreated;
		
			
			Acct();
			Acct(double initialAmount);
			void Deposit(double amount);
			double Withdraw(double amount);
			double GetBalance();
			static int GetTransactionCount();
		};

	
	int Acct::numberCreated = 0;
	int Acct::transactions = 0;
	Acct::Acct()
	{
		balance = 0;
		numberCreated++;
		

	}
	
	Acct::Acct(double initialAmount)
	{
		balance = initialAmount;
		numberCreated++;

	}

	void Acct::Deposit(double amount)
	{
		balance += amount;
		transactions++;
	}

	double Acct::Withdraw(double amount)
	{
		if (balance >= amount)
		{
			balance -= amount;
			transactions++;
			return balance;

		}
		else
			return -1.0;
	}

	double Acct::GetBalance()
	{
		transactions++;
		return balance;
	}

	int Acct::GetTransactionCount()
	{
		return transactions;
	};
	
	static void main()
	{

		Acct *myAcct = new Acct(25.00);
		myAcct->Deposit(700.00);
		if (myAcct->Withdraw(300.00) < 0)
			std::cout << "Insufficient funds" << std::endl;
		if (myAcct->Withdraw(450.00) < 0)
			std::cout << "Insufficient funds" << std::endl;

		std::cout << "My balance after completing transactions is "  << myAcct->GetBalance() << std::endl;
		std::cout << "The number of transactions is " << Acct::GetTransactionCount() << std::endl;
		Acct *yourAcct = new Acct();
		yourAcct->Deposit(1234.56);

		std::cout << "Your balance after completing transactions is " << yourAcct->GetBalance() << std::endl;
		std::cout << "The number of transactions is " << Acct::GetTransactionCount() << std::endl;
		std::cout << "Number of accounts created is " << Acct::numberCreated << std::endl;

	}


Output
Insufficient funds
My balance after completing transaction 425
The number of transactions is 3
Your balance after completing transaction 1234.56
The number of transactions is 5
Number of accounts created is 2
Press any key to continue . . .

One more question why does the code increment one more than my initial C# code? I know I can set the initial value to negative one like so: int Acct::transactions = -1; if I want to get desired result..just wondering.Thanks again all....


Last edited on
One more question why does the code increment one more than my initial C# code? I know I can set the initial value to negative one if I want if I want the desired result..just wondering.Thanks again all....

Are you sure you want line 66 to count getting the balance as a transaction?
Yea I think your right but when I took out line 66 the output is:
Insufficient funds
My balance after completing transactions is 425
The number of transactions is 2
Your balance after completing transactions is 1234.56
The number of transactions is 3
Number of accounts created is 2
Press any key to continue . . .

What should I do? There are suppose to be 4 transactions all together 2 deposits 2 withdrawals.

Last edited on
What happens when you withdraw an amount > balance?
prints "Insufficient funds" and return -1.0;
Is that or is that not a transaction?
withdrawing and depositing are transactions
Great! I'd say it's working as expected, then.

The number of transactions is 3


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Acct *myAcct = new Acct(25.00);
myAcct->Deposit(700.00);
if (myAcct->Withdraw(300.00) < 0)
    std::cout << "Insufficient funds" << std::endl;
if (myAcct->Withdraw(450.00) < 0)
    std::cout << "Insufficient funds" << std::endl;

std::cout << "My balance after completing transactions is "  << myAcct->GetBalance() << std::endl;
std::cout << "The number of transactions is " << Acct::GetTransactionCount() << std::endl;
Acct *yourAcct = new Acct();
yourAcct->Deposit(1234.56);

std::cout << "Your balance after completing transactions is " << yourAcct->GetBalance() << std::endl;
std::cout << "The number of transactions is " << Acct::GetTransactionCount() << std::endl;
std::cout << "Number of accounts created is " << Acct::numberCreated << std::endl;

1
2

<- this one did not go through





3
Last edited on
Topic archived. No new replies allowed.