c++ 错误:预期构造函数、析构函数或在“(”标记之前的类型转换

Posted

技术标签:

【中文标题】c++ 错误:预期构造函数、析构函数或在“(”标记之前的类型转换【英文标题】:c++ error: expected constructor, destructor, or type conversion before ‘(’ token 【发布时间】:2016-02-15 06:35:21 【问题描述】:

我有一个错误:预期的构造函数、析构函数或在“(”标记之前的类型转换。我不确定它期待什么,因为它看起来很好但显然不是。谢谢,所有

这是错误

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;

【问题讨论】:

在第一个声明中acct() 返回int account::acct 应该是构造函数吗?为什么要多次定义某个成员函数?为什么不在源文件中提供返回类型?为什么不在源文件中提供参数类型?也许您应该检查The Definitive C++ Book Guide and List 并找到一本好的初学者书籍? 你在那里缺少一个类型。必须是account::acct(int num, int_balance) 【参考方案1】:

在您的函数实现中,您忘记指定参数的类型:

例如

account::acct(num, int_balance)

应该是

account::acct(int num, float int_balance)

顺便说一句:account::acct 不是构造函数。构造函数必须与类同名,并且不能有返回值。

【讨论】:

以上是关于c++ 错误:预期构造函数、析构函数或在“(”标记之前的类型转换的主要内容,如果未能解决你的问题,请参考以下文章

错误:“(”标记之前的预期构造函数、析构函数或类型转换?

错误:预期的构造函数、析构函数、“<”标记之前的类型转换

错误:“;”之前的预期构造函数、析构函数或类型转换令牌

C++ 构造函数错误 - 标记 '<' 之前应为 ')'

错误:“。”之前的预期构造函数、析构函数或类型转换令牌 - 了解 fstream

OpenCV 预期的构造函数、析构函数或类型转换