声明与类型不兼容。

15

头文件:

#ifndef H_bankAccount;
#define H_bankAccount;


class bankAccount
{
public:
    string getAcctOwnersName() const;
    int getAcctNum() const;
    double getBalance() const;
    virtual void print() const;

    void setAcctOwnersName(string);
    void setAcctNum(int);
    void setBalance(double);

    virtual void deposit(double)=0;
    virtual void withdraw(double)=0;
    virtual void getMonthlyStatement()=0;
    virtual void writeCheck() = 0;
private:
    string acctOwnersName;
    int acctNum;
    double acctBalance;
};
#endif

cpp文件:

#include "bankAccount.h"
#include <string>
#include <iostream>
using std::string;


string bankAccount::getAcctOwnersName() const
{
    return acctOwnersName;
}
int bankAccount::getAcctNum() const
{
    return acctNum;
}
double bankAccount::getBalance() const
{
    return acctBalance;
}
void bankAccount::setAcctOwnersName(string name)
{
    acctOwnersName=name;
}
void bankAccount::setAcctNum(int num)
{
    acctNum=num;
}
void bankAccount::setBalance(double b)
{
    acctBalance=b;
}
void bankAccount::print() const
{
    std::cout << "Name on Account: " << getAcctOwnersName() << std::endl;
    std::cout << "Account Id: " << getAcctNum() << std::endl;
    std::cout << "Balance: " << getBalance() << std::endl;
}

请帮忙解决以下问题:在getAcctOwnersName和setAcctOwnersName下出现错误,错误信息为声明与“< error-type > bankAccount::getAcctOwnersName() const”不兼容。


1
如所示,代码不应该编译,因为头文件没有包含<string>。我认为问题可能是头文件选择了与std::string不同的string含义。尝试将#include <string>放入头文件中,并在那里使用std::string而不是普通的string。看看是否有帮助。 - Angew is no longer proud of SO
2
除非这是编译器显示的第一个错误,否则最好忽略它。始终从上到下逐个解决错误列表; 不要从最后一个开始,即使那是输出中最容易找到的一个。通常,在程序早期的一个错误可能会导致后面一系列的错误,而不修复触发所有错误的错误,试图修复后面的错误是没有意义的。 - Rob Kennedy
3个回答

25

您需要

#include <string>

在你的bankAccount头文件中,将字符串定义为std::string

#ifndef H_bankAccount;
#define H_bankAccount;

#include <string>

class bankAccount
{
public:
    std::string getAcctOwnersName() const;

   ....

一旦它被包含在头文件中,你就不需要在实现文件中再次包含它了。


2
好的,我在头文件代码中添加了std::string到字符串类型之前,但这并没有改变任何东西。 - Eric Oudin
2
此回答无用,未提供与帖子所引用的错误信息相关的任何连接或解释。 - Lisa

1
我发现当一个私有成员变量和一个成员函数拥有相同的名称时,IDE会给我"不兼容"的错误提示,也许这就是你所遇到的问题...

0
有时候出现这个错误是因为它在不同的机器上会有所不同。如果你在一个文件中声明你的类和所有的实现,而不是在另外一个文件中声明类并将其链接到驱动程序文件,那么你的程序将会正常工作。
再说一遍:这是完全依赖于机器的错误。
在Visual Studio 2012中,你会遇到这种类型的错误,因为它不能处理这些文件,而在其他版本的VS中,你不会遇到任何错误类型的异常。
希望对你有所帮助......

这可能是其中一个错误,但是OP所面临的问题已经在其他答案中观察到了。 - Abhishek Dutt

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接