PDA

View Full Version : Darnit g++



tdrusk
April 3rd, 2009, 03:13 AM
I had to do an assignment in my csci class. Everyone in there uses Microsoft Visual Studio. I wrote my program which had

class BankAccount
{
private:
int accountNumber;
double accountBalance;
public:
void enterAccountData(int);
void computeInterest(int, int);
void displayAccount(int);
double const static annualInterestRate = .03;
};

It compiled and ran great with g++, but failed to compile in Microsoft Visual Studio on this line

double const static annualInterestRate = .03;

I explained to my professor how I use Linux and g++. He told me I should compile at the lab and send it in. He never said that Microsoft Visual Studio was "required", but it is obvious that's what he wants us to use. After I fixed the code I recieved a 75% for turning in a second attempt. I suppose I should be grateful, but I wish he was more understanding.

FML

dwhitney67
April 3rd, 2009, 03:20 AM
You cannot initialize non-int values within a class. What compiler allowed you to do that?

Try


class BankAccount
{
...

static const double annualInterestRate;
};

double BankAccount::annualInterestRate = 0.03;

...

tdrusk
April 3rd, 2009, 03:32 AM
You cannot initialize non-int values within a class. What compiler allowed you to do that?

Try


class BankAccount
{
...

static const double annualInterestRate;
};

double BankAccount::annualInterestRate = 0.03;

...

g++ 4:4.3.1-1ubuntu2 from the Ubuntu repos.

I did as you suggested and submitted. Still got a 75%.

eye208
April 3rd, 2009, 06:07 AM
Compile with the -pedantic option to avoid these troubles in the future.

dwhitney67
April 3rd, 2009, 12:24 PM
And perhaps with '-Wall -ansi'

tdrusk
April 3rd, 2009, 01:31 PM
Okay thanks. So by doing that it will make g++ a more standard compiler?

stevescripts
April 3rd, 2009, 03:50 PM
FWIW, g++ is probably *more* standards compliant than the MS compiler ...

Steve