3

我正在尝试编译文件q1.cpp但我不断收到编译错误:

q1.cpp:2:28: fatal error: SavingsAccount.h: No such file or directory
compilation terminated.

头文件和头文件的实现都在与q1.cpp完全相同的目录中。

文件如下:

q1.cpp

#include <iostream>
#include <SavingsAccount.h>
using namespace std;

int main() {
    SavingsAccount s1(2000.00);
    SavingsAccount s2(3000.00);
}

SavingsAccount.cpp

#include <iostream>
#include <SavingsAccount.h>
using namespace std;

//constrauctor
SavingsAccount::SavingsAccount(double initialBalance) {
     savingsBalance = initialBalance;
}
SavingsAccount::calculateMonthlyInterest() {
     return savingsBalance*annualInterestRate/12
}
SavingsAccount::modifyInterestRate(double new_rate) {
     annualInterestRate = new_rate;
}

储蓄账户.h

class SavingsAccount {
    public:
        double annualInterestRate;
        SavingsAccount(double);
        double calculateMonthlyInterest();
        double modifyInterestRate(double);
    private:
        double savingsBalance;
};

我想重申所有文件都在同一个目录中。我正在尝试在 Windows 命令提示符下使用此行进行编译:

 C:\MinGW\bin\g++ q1.cpp -o q1

对此问题的任何输入将不胜感激。

4

1 回答 1

6
 #include <SavingsAccount.h>

应该

#include "SavingsAccount.h"

因为SavingsAccount.h是你定义的头文件,你不应该要求编译器通过使用它来搜索系统头文件<>

同时,当你编译它时,你应该编译两个 cpp 文件:SavingsAccount.cppq1.cpp.

 g++ SavingsAccount.cpp q1.cpp -o q1 

顺便说一句:你错过了;这里:

SavingsAccount::calculateMonthlyInterest() {
    return savingsBalance*annualInterestRate/12;
                                    //^^; cannot miss it
}
于 2013-04-22T02:01:28.583 回答