Ben
Ben

Reputation: 321

g++ is unable to find my header file

I'm trying to compile the file q1.cpp but I keep getting the compilation error:

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

The header file and the implementation of the header file are both in the exact same directory as q1.cpp.

The files are as follows:

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;
};

I'd like to reiterate that all files are in the SAME directory. I'm trying to compile by using this line at a windows command prompt:

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

Any input into this problem would be appreciated.

Upvotes: 3

Views: 3361

Answers (1)

taocp
taocp

Reputation: 23624

 #include <SavingsAccount.h>

should be

#include "SavingsAccount.h"

since SavingsAccount.h is the header file you defined, you should not ask the compiler to search for system headers by using <> around it.

Meanwhile, when you compile it, you should compile both cpp files: SavingsAccount.cpp and q1.cpp.

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

BTW: you missed a ; here:

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

Upvotes: 6

Related Questions