我已经开始学习 C++,并且在处理多个文件时遇到了困难。为了练习基础课,我写了三个不同的文件,
- 工作.cpp
- 单词.cpp
- 单词.h
字.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;
}
}
工作.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;
}
单词.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
我在编译时做错了什么?