g ++无法找到我的头文件

Ben*_*Ben 3 c++ compilation g++ header-files

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

q1.cpp:2:28: fatal error: SavingsAccount.h: No such file or directory
compilation terminated.
Run Code Online (Sandbox Code Playgroud)

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

文件如下:

q1.cpp

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

int main() {
    SavingsAccount s1(2000.00);
    SavingsAccount s2(3000.00);
}
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

SavingsAccount.h

class SavingsAccount {
    public:
        double annualInterestRate;
        SavingsAccount(double);
        double calculateMonthlyInterest();
        double modifyInterestRate(double);
    private:
        double savingsBalance;
};
Run Code Online (Sandbox Code Playgroud)

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

 C:\MinGW\bin\g++ q1.cpp -o q1
Run Code Online (Sandbox Code Playgroud)

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

tao*_*ocp 5

 #include <SavingsAccount.h>
Run Code Online (Sandbox Code Playgroud)

应该

#include "SavingsAccount.h"
Run Code Online (Sandbox Code Playgroud)

由于SavingsAccount.h是您定义的头文件,因此不应要求编译器使用<>它来搜索系统头。

同时,编译时,应同时编译两个cpp文件:SavingsAccount.cppq1.cpp

 g++ SavingsAccount.cpp q1.cpp -o q1 
Run Code Online (Sandbox Code Playgroud)

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

SavingsAccount::calculateMonthlyInterest() {
    return savingsBalance*annualInterestRate/12;
                                    //^^; cannot miss it
}
Run Code Online (Sandbox Code Playgroud)