g++找不到我的头文件

g++ is unable to find my header file

本文关键字:文件 我的 找不到 g++      更新时间:2023-10-16

我试图编译文件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;
}

SavingsAccount.h

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

我想重申,所有文件都在同一目录中。我正试图在windows命令提示符下使用以下行进行编译:

 C:MinGWbing++ q1.cpp -o q1

对这个问题的任何投入都将不胜感激。

 #include <SavingsAccount.h>

应该是

#include "SavingsAccount.h"

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

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

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

BTW:你错过了;这里:

SavingsAccount::calculateMonthlyInterest() {
    return savingsBalance*annualInterestRate/12;
                                    //^^; cannot miss it
}
相关文章: