我有一个错误:在'('标记之前需要构造函数,析构函数或类型转换。我不确定所期望的是什么,因为看起来不错,但显然不是。谢谢,全部
这是错误
account.cpp:16:14: error: expected constructor, destructor, or type conversion before ‘(’ token
account::acct(num, int_balance) {
^
account.cpp:22:17: error: expected constructor, destructor, or type conversion before ‘(’ token
account::deposit(amount) {
头文件
//account.h
#ifndef account_h_
#define account_h_
#include <string>
#include <iostream>
using namespace std;
class account
{
public:
//----------------------------------------------------
//account
int acct(int num, float int_balance);
//----------------------------------------------------
//account number
int account_num() const {
return acctnum;
}
//----------------------------------------------------
//constructs bank account with inital_balance
double balance() const {
return bal;
}
//----------------------------------------------------
//deposit into account
void deposit(float amount) {
bal += amount;
}
//----------------------------------------------------
//withdrawal from account
void withdraw(float amount) {
amount - bal;
}
private:
//----------------------------------------------------
//account number
int acctnum;
//----------------------------------------------------
//balance
double bal;
};
#endif
程序文件
//account.cpp
#include <string>
#include <iostream>
using namespace std;
#include "account.h"
//----------------------------------------------------
//Account details
account::acct(num, int_balance) {
acctnum = num;
bal = int_balance;
}
//----------------------------------------------------
//Depositing into account
account::deposit(amount) {
if (amount < 0) {
std::cout << endl <<"The deposit you've enter is negative."
<< amount << " on account " << acctnum << endl;
}
else {
balance = amount;
}
}
//----------------------------------------------------
//Withdrawing from account
//If withdrawel exceeds balance provide error and leave balance
//Else subtract withdrawel from account and update balance
account::withdraw(amount) {
if (amount < balance) {
std::cout << "Debit amount exceeded account balance."
<< amount << " on account "<< acctnum << " with balance "
<< balance << endl;
}
else if(amount < 0) {
std::cout <<"The withdrawel you've enter is defined as negative."
<< amount << " on account "<< acctnum << " with balance "
<< balance << endl;
}
else {
balance -= amount;
}
}
//----------------------------------------------------
//Insert intial balance of account
//If no balance included then give error message and set account balance to 0
account::int_balance(float amount){
if (amount >= 0) {
balance = amount;
}
else {
balance = 0;
std::cout << "Error intial balance invalid" << endl;
}
}
//----------------------------------------------------
account::balance(){
return bal;
}
最佳答案
在函数实现中,您忘记了指定参数的类型:
例如account::acct(num, int_balance) {
应该account::acct(int num, float int_balance) {
顺便说一句:account::acct不是构造函数。构造函数必须与类具有相同的名称,并且不能具有返回值。
关于c++ - C++错误: expected constructor, destructor, or type conversion before ‘(’ token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35402905/