2013-04-22 104 views
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; 
} 

SavingsAccount.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 

對此問題的任何輸入將不勝感激。

+3

只有系統內置頭用作'的#include '。除了那些複製到系統包含路徑(不推薦)的路徑之外,你自己的自定義頭文件必須用作'#include「myheader.h」' – phoeagon 2013-04-22 02:15:02

回答

4
#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 
} 
+0

另外,不應該編譯'SavingsAccount.cpp'並將其鏈接以及? – vidit 2013-04-22 02:02:51

+0

@vidit是的,那是另一個點 – taocp 2013-04-22 02:03:21

+0

啊,就是這樣!不能相信我錯過了......謝謝,當我能夠時,我會接受這個答案。 – Ben 2013-04-22 02:04:58