tags:

views:

88

answers:

3

The error states:
"error: virtual outside class definition"

Cpp members in question:

virtual void Account::creditBalance(double plus)
{
   if(plus > 0)
      balance += plus;
   else
      cout << "Cannot credit negative.";
}

virtual void Account::debitBalance(double minus)
{
   if(minus <= balance)
      balance -= minus;
   else
      cout << "Debit amount exceeded account balance.";
}

The rest of the code is here (although I'm not sure it's necessary): http://pastebin.com/de5e9f77

+13  A: 

Simply strip the virtual off of the method definition. The virtual keyword is only necessary at the declaration.

Example:

class Account {
public:
  virtual void creditBalance(double);
  virtual void debitBalance(double);
};

void Account::creditBalance(double plus)
{
   if(plus > 0)
      balance += plus;
   else
      cout << "Cannot credit negative.";
}

void Account::debitBalance(double minus)
{
   if(minus <= balance)
      balance -= minus;
   else
      cout << "Debit amount exceeded account balance.";
}
JaredPar
+3  A: 

You only need to use the virtual keyword inside the class declaration, i.e. in the header file. So change your cpp file to something like:

void Account::creditBalance(double plus)
{
   if(plus > 0)
      balance += plus;
   else
      cout << "Cannot credit negative.";
}

void Account::debitBalance(double minus)
{
   if(minus <= balance)
      balance -= minus;
   else
      cout << "Debit amount exceeded account balance.";
}
jon hanson
+1  A: 

Use the virtual keyword only in decleration (in header files, within class definition) and not in implementation (.cc files usually)

rmn