看板 EE_DSnP 關於我們 聯絡資訊
Somethings about the "include" command which is one of the preprocessor directives: 1. Two formats of the #include #include 指令可以讀入你所指定的檔案內容,通常有兩種形式 #include <somefile> (C++) or #include <somefile.h> (C) #include "myfile.h" 如果檔名以(< >)括起來,表示要讀入的檔案是個專案表頭or標準表頭檔, 那麼搜尋路徑會是一個預先特定的目錄;如果檔名以(" ")括起來,表示要 讀入的檔案是程式員提供的表頭檔,那個搜尋路徑應該會從工作目錄or目 前所在的目錄開始搜尋。 2. The difference between including .h and including .cpp 其實問題應該要問include header file(表頭檔)和include source file (程式原始碼檔)的不同,因為表頭檔的副檔名會隨著C++ compiler的不同 而有所不同(這也就是為什麼C++標準表頭檔決定不使用副檔名的原因之一) ,通常會放在表頭檔的東西有extern object宣告、函式宣告和inline函式 定義,凡是必須使用或定義object或函式的程式原始碼檔,都應該含入這個 表頭檔,所以程式原始碼檔通常是用來定義object或函式,當你include程 式原始碼檔就有可能會發生重複定義相同的object或函式的錯誤,但是如果 你include表頭檔只有一些宣告,重複宣告是沒有關係的。 ※ 引述《wintercobra (wintercobra)》之銘言: : 我開了一個專案,main.cpp和HW3-2-upp.cpp都放在同一個資料夾下 : //in main.cpp : #include <cstdlib> : #include <iostream> : #include <time.h> : #include <HW3-2-upp.h> : using namespace std; : int main(int argc, char *argv[]) : { : ......... : } : 編譯main的時候他說HW3-2-upp.h.No such file or directory. : 把.h改成.cpp也還是一樣 : 請問是為什麼呢? : 喔還有include的時候.h和.cpp的差別在哪我還是不太清楚 : 可以麻煩再說明一下嗎? : 謝謝~~ -- 以上是小的淺見...不知道有沒有錯,哈!? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.171.138
wintercobra:謝啦~ 不過,重覆宣告沒有關係? 不是要用#ifndef嗎? 10/22 08:46