Dynamic Allocation with pointers c++ -


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 pointer array.

i'm trying send balance , amt array, getting error 'no match 'operator=' '

transaction *trans = new transaction[max_num]; 

this gives pointer array of transaction objects.

trans[currentnum] = new transaction(balance, amt); 

this tries assign transaction object in array, new transaction(...) call returns pointer transaction, , pointers not data element type array, won't compile.

you say:

trans[currentnum] = transaction(balance, amt); 

or, this:

transaction** trans = new (transaction*)[max_num]; trans[currentnum] = new transaction(balance, amt); 

but it's better use:

std::vector<transaction> trans(max_num); 

if must have max_num default-constructed elements, or better yet...

std::vector<transaction> trans; 

and .push_back(transaction(balance,amt)) go, don't have worry max_num or indexing off end of container. if have c++11 compiler can use .emplace_back(balance, amt) better performance , direct in-container construction of transaction object..


Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -