c++ - C++错误: expected constructor, destructor, or type conversion before ‘(’ token

标签 c++ constructor compiler-errors

我有一个错误:在'('标记之前需要构造函数,析构函数或类型转换。我不确定所期望的是什么,因为看起来不错,但显然不是。谢谢,全部

这是错误

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/

相关文章:

c++ - 无法在 Windows 上构建 Soundpipe DSP 库

c++ - 在 C++03 中返回类似 `std::auto_ptr` 的集合的最佳方法是什么?

jsf - 尝试在构造函数中访问 @Inject bean 时出现 NullPointerException

java - 在类中使用构造函数中的变量/数组

uiview - Swift 编译器错误 "failed with exit code 254"

plsql - PLS-00103 : Encountered the symbol “END” when expecting one of the following: . ; The symbol “;” was substituted for “END” to continue

c++ - 结构模板的默认参数

c++ - 二叉查找树,中序遍历,Step 3 逻辑辅助

Java无限新对象

java - 新手 需要帮助 制作 Java 类和签名