2012-02-21 1 views
0

안녕하세요 여러분, 저는 OOP와 C++에 대해 처음 보았습니다.'newacc'는 클래스 또는 이름 공간이 아닙니다.

나는 클래스를 정의하려고하는데, 그것의 함수이고, 그것들을 사용한다. 나는 내가 지금까지 가지고있는 것과 내가 어디서 오류를 겪고 있는지를 보여줄 것이다. 라는 파일에서

"account.h는"내가 가진 :라는 파일에

#include <iostream> 
#include <string> 
using namespace std; 

class Account{ 
string fname; 
string lname; 
string sinnum; 
string accttype; 
int numtrans; 
double balance; 

public: 
Account(string,string,string,string); 
double DepositAmt(double); 
double WithdrawAmt(double); 
void PrintStatement(); 
void getFinalBalance(); 
}; 

"account.cpp"나는이 :

Account::Account(string firstname, string lastname, string sinnumber, string acc 
{ 
fname = firstname; 
lname = lastname; 
sinnum = sinnumber; 
accttype = accounttype; 
numtrans = 0; 
balance = 0; 
} 

double Account::DepositAmt(double deposit) 
{ 
balance = balance + deposit; 
return balance; 
} 

double Account::WithdrawAmt(double withdraw) 
{ 
balance = balance - withdraw; 
return balance; 
} 


void Account::PrintStatement() 
{ 
cout << "First Name: " << fname << endl; 
cout << "Last Name: " << lname << endl; 
cout << "SIN: " << sinnum << endl; 
cout << "Account Type: " << accttype << endl; 
cout << "Total Transactions: " << numtrans << endl; 
cout << "Final balance: $" << balance << endl; 
} 

void Account::getFinalBalance() 
{ 
cout << "Your Final balance is: $" << balance << endl; 
} 

그리고 마지막으로 나의 마지막 파일에서 호출 " ass2012.cpp "가지고 있어요 :

#include "account.h" 
#include "account.cpp" 

int main() 
{ 
string fname, lname, sinnum, accttype; 
int tempaccttype; 


cout << "\nPlease enter your last name: " << endl; 
cin >> lname; 
cout << "\nPlease enter your SIN number: " << endl; 
cin >> sinnum; 
cout << "\nPlease choose your account type: "<< endl; 
cout << "1: Checking" << endl; 
cout << "2: Savings" << endl; 
cin >> tempaccttype; 

if (tempaccttype == 1) 
{ 
accttype = "Checking"; 
} 
else 
{ 
accttype = "Savings"; 
} 

Account newacc (fname, lname, sinnum, accttype); // HERE IS WHERE I GET THE ERROR 
newacc::getFinalStatement(); 
return 0; 
} 

내가 뭘 잘못하고 있는지 말해 줄 수 있어요 !!

편집 : 감사합니다. Naveen !! 저를 벽에 태우는 것은 항상 작은 것들입니다.

답변

5

개체의 호출 방법은 . 연산자가 아니고 :: 연산자를 사용해야합니다. 즉, newacc.getFinalBalance()을 수행해야합니다. 메서드 이름도 잘못되었으므로 여기에서 수정했습니다.

0

저는 C++과 프로그래밍에 대해 거의 알지 못합니다. 나는 초보자이기도합니다. 하지만 줄 newacc :: getFinalStatement(); ~에 newacc.getFinalStatement() ;. 이것이 작동하지 않으면 잘 모르겠습니다.

0

일반 synatx은 다음과 같습니다 범위의 모호성이있는 경우에만

Objectname.memberfunction();  //public 
Objectname.datzmember    //public 
classname::staticmembers   //public 

이 :: 범위 결정 연산자와 사용. 그렇지 않으면 그것을 사용할 필요가 없습니다. 프로그래밍 연습으로 사용하는 것이 좋다.

관련 문제