2013-05-08 44 views
5

头文件不兼容:宣言是

#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声明,该声明是“<错误 - 不兼容键入> bankAccount :: getAcctOwnersName()const“。

+1

如前所述,代码不应该编译,因为头文件不包含''。我认为这个问题可能是头文件比'std :: string'提取了'string'的不同含义。尝试将'#include '放入标题,并在其中使用'std :: string'而不是普通的'string'。看看是否有帮助。 – Angew 2013-05-08 12:29:35

+2

除非这是编译器显示的* first *错误,否则最好忽略它。始终处理从上到下的错误列表;不要从打印的最后一张开始,尽管这是在输出中最容易找到的。通常,一个程序的早期错误可能会导致后来出现一连串的错误,如果没有解决首先触发它们的错误,那么尝试修复后面的错误并不是一件好事。 – 2013-05-08 12:32:09

回答

14

您需要

#include <string> 

bankAccount头文件,并参考字符串作为std::string

#ifndef H_bankAccount; 
#define H_bankAccount; 

#include <string> 

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

    .... 

一旦它被包含在标题中,就不再需要将它包含在实现文件中。

+0

好吧我添加std :: string到我的头文件代码之前的字符串类型,它并没有改变任何 – 2013-05-08 12:30:49

+2

好吧,永远不要感谢现在很多工作现在 – 2013-05-08 12:32:43