gpt4 book ai didi

c++错误编译器原型(prototype)和期望构造函数

转载 作者:行者123 更新时间:2023-11-28 00:06:33 25 4
gpt4 key购买 nike

我在编译 account.h 和 account.cpp 文件时遇到问题。编译时它给出了 account::account 的原型(prototype)错误,并说它不匹配任何类的“帐户”我不确定我错过了什么是因为我称它为 account::account。或者我是否需要在头文件中定义帐户。如果我这样做,我该怎么做,它将如何携带 num 和 abal?这是确切的错误

account.cpp:14:1: error: prototype for ‘account::account(int, float)’ does not match any in class ‘account’
account::account(int num, float abal){
^
In file included from account.cpp:12:0:
account.h:15:7: error: candidates are: account::account(const account&)
class account
^
account.h:15:7: error: account::account()
account.cpp:20:17: error: expected constructor, destructor, or type conversion before ‘(’ token
account::deposit(amount){

这是程序文件

//account.cpp

#include <string>
#include <iostream>

using namespace std;

#include "account.h"

account::account(int num, float abal){
acctnum = num;
intbal = abal;
}
//----------------------------------------------------
//Depositing into account
account::deposit(amount){
if (amount < 0)
{
std::cout << endl <<"The deposit you've enter is negative."
<< amount << " on account " << acctnum << endl;
return 0;
}
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;
return 0;
}
else if(amount < 0){
std::cout <<"The withdrawel you've enter is defined as negative."
<< amount << " on account "<< acctnum << " with balance "
<< balance << endl;
return 0;
}
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.h

#ifndef account_h_
#define account_h_

#include <string>
#include <iostream>

using namespace std;

class account
{
public:
//----------------------------------------------------
//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

最佳答案

您尝试在 .cpp 文件中定义的这个签名

account::account(int num, float abal)

尚未在头文件中的类定义中声明。

你必须声明构造函数,像这样

account(int num, float abal);

在头文件中的类定义中。

关于c++错误编译器原型(prototype)和期望构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35400469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com