Cannot allocate an object

So i'm doing this homework problem for my class and I keep getting the error (Cannot allocate an object of abstract type 'Bond') when I'm trying to create bond pointer.

Here is a snippet of how I start to create a new bond pointer.
1
2
3
4
5
6
if (first == "bond")
{
strm >> name >> year >> initialVal >> rate;
Bond* new_bond = new Bond(name, year, initialVal, rate);
cust.back()->add_asset(new_bond);
}

Here is my base class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Assets 
{
public:
	Assets(string newName, double newValue)
	{
		name = newName;
		initialValue = newValue;
	};
	virtual void compute() const = 0;
	string toString() 
	{
		return (name);
	}
protected:	
	string name;
	double initialValue;
	
};

And here is one of the child classes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Bond : public Assets 
{
public:
	Bond(string newName, int newYear, double newValue, double newRate) : Assets (newName, newValue)
	{
		year = newYear;
		rate = newRate;
		startValue = newValue;
		totalValue = newValue;
	};
	int getYear() const;
	double getRate() const;
	double getTotal() const;
	virtual void compute();
private:
	int year;
	double rate;
	double startValue;
	double totalValue;
};


I hope that's all the info you need. I just need help getting pointed in the right direction.
Hey,
I was just tinkering with your code and I found something that you may be interested in.
At line 9 of your base class Asset, I removed the const keyword and supplied a body of your function compute in class Bond. And voila!! I was able to create a new object of class Bond.

I don't know the reason behind this, but somehow I think that declaring a virtual function const must have something with this. Try my advice and let us know what happened.
Topic archived. No new replies allowed.