我已經開始學習C++,並在處理多個文件時陷入困境。爲了實踐基本類,我寫了三個不同的文件,如何從多個C++文件構建二進制文件?
- working.cpp
- word.cpp
- word.h
word.cpp:
#include <iostream>
#include "word.h"
using namespace std;
class word{
public:
char *word;
void createWord(char *str)
{
word = str;
}
void print_word(void)
{
cout<<word<<endl;
}
char * getWord()
{
return word;
}
}
working.cpp
#include <iostream>
#include "word.h"
void printWord(word);
using namespace std;
int main()
{
word one;
one.createWord("one");
printWord(one);
}
void printWord(word a)
{
cout<<a.getWord()<<endl;
}
word.h
class word;
這是三個不同的文件,所以我不知道如何編譯它們。我曾嘗試爲
g++ working.cpp word.cpp
然而,編譯器無法識別的字爲一類,並給了我下面的錯誤
working.cpp: In function 'int main()':
working.cpp:7:7: error: aggregate 'word one' has incomplete type and cannot be defined
working.cpp:7:12: error: aggregate 'word two' has incomplete type and cannot be defined
working.cpp:7:17: error: aggregate 'word three' has incomplete type and cannot be defined
working.cpp: In function 'void printWord(word)':
working.cpp:19:6: error: 'aha' has incomplete type
In file included from working.cpp:2:0:
word.h:2:7: error: forward declaration of 'class word'
word.cpp:25:1: error: expected ';' after class definition
我在做什麼錯在編譯時?
從一本好書開始,代碼有很多問題。 – lekroif