【问题标题】:c++ Error: expected constructor with declared header filec ++错误:预期的构造函数与声明的头文件
【发布时间】:2016-02-15 23:40:36
【问题描述】:

我的程序文件有问题。我已经在我的头文件中声明了“存款”,但仍然期待程序文件中有一些东西。我是否需要定义金额和我错过了什么?谢谢

account.cpp:22:17: error: expected constructor, destructor, or type conversion before ‘(’ token
 account::deposit(amount) {
                 ^

Program.cpp

 #include <string>
    #include <iostream>

    using namespace std;

    #include "account.h"

//----------------------------------------------------
//Account details
int account::acct( int num, int int_balance){
        acctnum = num;
        bal = int_balance;
}
//----------------------------------------------------
//Depositing into account
account::deposit(amount) {
    if (amount < 0) {
        std::cout << "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
int_balance(amount){
    if (amount >= 0) {
        balance = amount;
    }
    else {
        balance = 0;
        std::cout << "Error intial balance invalid" << endl;
    }
}
//----------------------------------------------------
balance(){
    return bal;
}

account.h 标头

#ifndef account_h_
#define account_h_

#include <string>
#include <iostream>

using namespace std;

class account
{
public:
    //----------------------------------------------------
    //account
    int acct(int num, int int_balance);
    //----------------------------------------------------
    //account number
    int account_num() const {
        return acctnum;
    }
    //----------------------------------------------------
    //constructs bank account with inital_balance
    double balance() const {
        return bal;
    }
    //----------------------------------------------------
    //withdrawal from account
    void withdraw(float amount) {
        amount - bal;
    }
    //----------------------------------------------------
    //deposit into account
    void deposit(float amount) {
        amount + bal;
    }
private:
    //----------------------------------------------------
    //account number
    int acctnum;
    //----------------------------------------------------
    //balance
    double bal;
};

#endif

【问题讨论】:

  • 定义时需要指定account::deposit的返回类型。应该是:void account::deposit(amount) { ... }

标签: c++ constructor compiler-errors


【解决方案1】:

实现有一个数量类型。

 void deposit(float amount) {
     amount + bal;
 }

您似乎在 .cc 文件中忘记了几个方法。参数需要一个类型。

【讨论】:

  • 所以我需要重新定义它,即使我在头文件中定义了它? @DOUGLAS O. MOEN
  • 为什么即使在这种情况下,存款也只有一个错误,为什么在提款时不给出错误? @DOUGLAS O. MOEN
  • @Bigboy6 不,实现文件不被视为定义。是的,你必须定义它(hh) 是的,你必须实现它,但是如果头文件与实现文件不匹配,编译器无法将这两项配对。也许编译器在第一个错误时退出?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-15
  • 1970-01-01
相关资源
最近更新 更多