'newacc'不是类或名称空间

发布于 2025-01-07 03:42:03 字数 2235 浏览 1 评论 0原文

嘿伙计们,我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忱杏 2025-01-14 03:42:03

要调用对象上的方法,您应该使用 . 运算符而不是 :: 运算符,即您应该执行 newacc.getFinalBalance()(注意方法名称也是错误的,我在这里更正一下)。

For calling methods on objects you should use . operator and not :: operator i.e. you should do newacc.getFinalBalance()(Note that method name was also wrong, I corrected it here).

聊慰 2025-01-14 03:42:03

我对 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.

千寻… 2025-01-14 03:42:03

一般语法是:

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

:: 是范围解析运算符,仅在范围不明确时使用。否则有 np 需要使用它。不过,将其用作编程练习还是很好的。

the general synatx are:

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

:: 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文