'newacc'不是类或名称空间
嘿伙计们,我对 OOP 和 C++ 总体来说还很陌生,所以请耐心等待。
我试图定义一个类,它的函数,然后使用它们。我将展示到目前为止我所拥有的以及我在哪里遇到错误。
在一个名为“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;
}
你能告诉我我做错了什么吗!!
编辑:谢谢纳文!总是一些小事让我感到厌烦。
Hey guys I'm fairly new to OOP and c++ in general so please bear with me.
I'm trying to define a class, it's functions, and then use them. I'll show what I have so far and where I'm encountering errors.
In a file called "account.h" I have:
#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();
};
In a file called "account.cpp" I have:
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;
}
And finally in my last file called "ass2012.cpp" I have:
#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;
}
Can you please tell me what I'm doing wrong!!
EDIT: Thanks Naveen!! It's always the little things that run me up the wall.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要调用对象上的方法,您应该使用
.
运算符而不是::
运算符,即您应该执行newacc.getFinalBalance()
(注意方法名称也是错误的,我在这里更正一下)。For calling methods on objects you should use
.
operator and not::
operator i.e. you should donewacc.getFinalBalance()
(Note that method name was also wrong, I corrected it here).我对 C++ 和编程知之甚少,我也是一个新手。但我会更改行 newacc::getFinalStatement();到 newacc.getFinalStatement();。如果这不起作用我不确定。
I know very little about C++ and programming in general, I too am a novice. But I would change the line newacc::getFinalStatement(); to newacc.getFinalStatement();. If this doesn't work I'm not sure.
一般语法是:
:: 是范围解析运算符,仅在范围不明确时使用。否则有 np 需要使用它。不过,将其用作编程练习还是很好的。
the general synatx are:
:: is scope resolution operator and used only when there is a scope ambiguity. else there s np need to use it. its good to use it as a programming practice though.