C++ Constructor With Parameters Won't Initialize, Errors C2059 and C2228
Posted
by
Some Girl
on Stack Overflow
See other posts from Stack Overflow
or by Some Girl
Published on 2012-06-03T04:26:38Z
Indexed on
2012/06/03
4:40 UTC
Read the original article
Hit count: 202
I'm a C# programmer trying to muddle through C++ to create a Windows Forms Application.
I have a Windows Form that makes use of a user-created class. Basically I'm trying to use a constructor that takes parameters, but my form won't let me initialize the object with parameter. Here's the code, hopefully somebody can explain the problem to me because I'm completely baffled...
Here's my header file: BankAcct.h
public ref class BankAcct
{
private:
int money;
public:
BankAcct();
BankAcct(int);
void Deposit(int);
void GetBalance(int&);
};
And my definition file: BankAcct.cpp
#include "StdAfx.h"
#include "BankAcct.h"
BankAcct::BankAcct()
{
money = 0;
}
BankAcct::BankAcct(int startAmt)
{
money = startAmt;
}
void BankAcct::Deposit(int depAmt)
{
money += depAmt;
}
void BankAcct::GetBalance(int& balance)
{
balance = money;
}
And finally my main form. Won't copy the whole thing, of course, but I'm trying to declare the new bank account object, and start it with a balance of say $50.
private:
BankAcct myAccount(50); //does not work! WHY??
//private:
//BankAcct myAccount; //works
then in the form constructor my code is this:
public:
frmBank(void)
{
InitializeComponent();
int bal;
myAccount.GetBalance(bal);
lblBankBalance->Text += Convert::ToString(bal);
}
I've included the BankAcct.h file at the top of my frmBank.h, what else am I doing wrong here? It works great if I use the default constructor (the one that starts the bank balance at zero). I get the following error messages:
error C2059: syntax error: 'constant'
and
error C2228: left of '.GetBalance' must have class/struct/union
Thank you for any and all help on this one!!
© Stack Overflow or respective owner