Help with pointers to classes and Dynamic Memory

I am trying to make a short program that tells you how much money you have when you enter a number of pennies, nickels, dimes, and so on. I defined a class for the coins named Coin and a few simple member acquisition functions for it, but I suppose that's not the point.

Anyway, below is the main() function which I was just beginning to make:
1
2
3
4
5
6
7
8
9
int main(){
	int response;
	cout << "\t\tWelcome to Coin Calculator!\n\n";
	Coin *pPennies, *pNickels, *pDimes, *pQuarters, *pHalves, *pDollars;
	cout << "# of Pennies: ";
	cin >> response;
	pPennies = new Coin(PENNY)[response];
	return 0;
}

When I got to the dynamic memory allocation (line 7), a red line appeared below "new" saying "Error: no suitable conversion function from "Coin" to "Coin*" exists".
I know there is a simpler way to make this program, but I would still like to know why that line doesn't work.

Here is the rest of the program, if you would like to see it:

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
//Coin Calculator
//Determines how much money is in an amount of coins
#include <iostream>
using namespace std;

enum CoinValue { PENNY = 1, NICKEL = 5, DIME = 10, QUARTER = 25, HALF = 50, DOLLAR = 100, NUM_VALUES = 6};

class Coin {
public:
	int getValue();
	Coin(CoinValue value);
	void reserve(int num);
	
private:
	CoinValue m_Value;
};

Coin::Coin(CoinValue value){
	m_Value = value;
}

int Coin::getValue(){
	return (m_Value);
}

int main(){
	int response;
	cout << "\t\tWelcome to Coin Calculator!\n\n";
	Coin *pPennies, *pNickels, *pDimes, *pQuarters, *pHalves, *pDollars;
	cout << "# of Pennies: ";
	cin >> response;
	pPennies = new Coin(PENNY)[response];
	return 0;
}


Thanks,
Halfbreed
pPennies = new Coin(PENNY)[response];

If you are using new[] to create an array of objects, you cannot pass any parameters to the constuctor. new[] only allows you to call the default constructor for all objects.

Instead, you might want to consider passing the number of coins as a parameter to the constructor. So instead of creating one Coin object for each penny, you create just one Coin object for all of them, but it knows how many it has:

1
2
3
pPennies = new Coin(PENNY, response);

// of course, you'll have to change your constructor to match. 
Wow, thanks. I didn't know you couldn't pass parameters to a constructor with new[]. Anyway, I revised my program to be simpler, not needing classes at all. Here it is:

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
//Coin Calculator
//Determines how much money is in an amount of coins
#include <iostream>
#include <string>
using namespace std;

enum CoinValue { PENNY = 1, NICKEL = 5, DIME = 10, QUARTER = 25, HALF = 50, DOLLAR = 100, NUM_VALUES = 6};
float calculateValue(CoinValue coin, int numCoins);
float output(string coin, CoinValue value);

int main(){
	float money;
	cout << "\t\tWelcome to Coin Calculator!\n\n";
	
	money = output("Pennies", PENNY);
	money += output("Nickels", NICKEL);
	money += output("Dimes", DIME);
	money += output("Quarters", QUARTER);
	money += output("Halves", HALF);
	money += output("Dollars", DOLLAR);

	money /= 100;

	cout << "\nYour total money is: $" << money << endl;

	return 0;
}

float calculateValue(CoinValue coin, int numCoins){
	float value = coin * numCoins;
	return (value);
}

float output(string coin, CoinValue value){
	int response;
	float money;
	cout << coin << ": ";
	cin >> response;
	money = calculateValue(value, response);
	return (money);
}
Topic archived. No new replies allowed.