Reputation: 3
Transaction *trans = new Transaction[MAX_NUM];
int currentnum;
addtrans(string balance, double amt){
trans[currentnum] = new Transaction(balance, amt);
currentnum ++;
}
//constructor
Transaction(string type, double amt){
Transtype = type;
Transamt = amt;
}
I have created a pointer to an array.
I'm trying to send balance and amt to the array, but I am getting the error 'no match for 'operator=' '
Upvotes: 0
Views: 60
Reputation: 76240
Use an std::vector
instead:
std::vector<Transaction> trans;
trans.reserve(MAX_NUM);
and addtrans
can be basically be reduced to:
trans.emplace_back(balance, amt);
Upvotes: 0
Reputation: 106116
Transaction *trans = new Transaction[MAX_NUM];
This gives you a pointer to an array of Transaction
objects.
trans[currentnum] = new Transaction(balance, amt);
This tries to assign to a Transaction
object in your array, but the new Transaction(...)
call returns a pointer to a Transaction
, and pointers are not the data element type for the array, so this won't compile.
You could say:
trans[currentnum] = Transaction(balance, amt);
Or, you could do this:
Transaction** trans = new (Transaction*)[MAX_NUM];
trans[currentnum] = new Transaction(balance, amt);
But it's almost certainly better to use:
std::vector<Transaction> trans(MAX_NUM);
If you really must have MAX_NUM
default-constructed elements, or better yet...
std::vector<Transaction> trans;
And just .push_back(Transaction(balance,amt))
as you go, so you basically don't have to worry about any MAX_NUM
or indexing off the end of the container. If you have a C++11 compiler you can use .emplace_back(balance, amt)
for better performance and direct in-container construction of the Transaction object..
Upvotes: 2