2013-02-01 83 views
2

我有三個文件,如下所示:collect2:LD返回1退出狀態錯誤

Movie.h

struct Movie{ 
    char title[30];   // the hour of the current time 
    char director[30];   // the minute of the current time 
    int length; 
} ; 

void printMovieInfo(Movie *s); 

Movie.cpp

#include <iostream> 
#include <cstdlib> 
#include <cstring> 
#include <cmath> 

#include "Movie.h" 

using namespace std; 

void printMovieInfo(Movie *s){ 

    cout << "Hi"; 

} 

和與主文件

#include "Movie.cpp" 
using namespace std; 

int main() 
{ 
    struct Movie *m; 
    printMovieInfo(m); 
} 

當我運行程序時,我得到以下錯誤:

collect2 ld returned 1 exit status 

和警告:

/tmp/ccIe4dlt.o In function `printMovieInfo()': 
Movie.cpp (.text+0x0): multiple definition of `printMovieInfo()' 
/tmp/cc91xrNB.o HelloWorld.cpp:(.text+0x0): first defined here 

我只是想調用一個函數來打印「你好」,但我米不知道爲什麼我得到這個錯誤

+1

你必須包含頭文件,而不是'.cpp'文件 – soon

+2

並且爲頭文件添加包含守護程序總是一個好主意。 – chris

+0

在main.cpp中包含「Movie.h」,而不是「Movie.cpp」。重複的符號意味着你已經正確地建立了+連接Movie.cpp,所以只改變'#include'應該解決傾注問題。 – WhozCraig

回答

2

不要使用#include "movie.cpp"你想要#include "movie.h"

包含「.cpp」文件是非常罕見的 - 它們被編譯爲單獨的單元,然後通過鏈接器(在此稱爲collect2)鏈接在一起。

相關問題