Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从多个c++;文件夹? 我开始学习C++,在使用多个文件时,卡死了。为了练习基础课,我写了三个不同的文件 working.cpp word.cpp word.h_C++ - Fatal编程技术网

如何从多个c++;文件夹? 我开始学习C++,在使用多个文件时,卡死了。为了练习基础课,我写了三个不同的文件 working.cpp word.cpp word.h

如何从多个c++;文件夹? 我开始学习C++,在使用多个文件时,卡死了。为了练习基础课,我写了三个不同的文件 working.cpp word.cpp word.h,c++,C++,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 wo

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;
}

这是三个不同的文件,所以我不知道如何编译它们。我试过的是
g++working.cpp word.cpp

但是,编译器不将word识别为类,并给出以下错误

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

编译时我做错了什么

仅仅在头文件中提到类名(所谓的前向声明)是不够的;您需要一个完整的类声明(声明类的所有字段和函数):


word.h

word.h:2:7: error: forward declaration of 'class word'
我建议你读Bjarne Stroustrup的精彩著作《C++程序设计语言》,开始。

  • 您需要在头文件中包含更多的
    word
    定义。大概是这样的:

    class word
    {
    public:
        char *word;
        void createWord(char *str);
        void print_word(void);
        char * getWord();
    };
    
  • 然后,将
    word.cpp
    更改为只包含以下实现:

    void word::createWord(char *str)
    {
        word = str;
    }
    
    void word::print_word(void)
    {
        cout<<word<<endl;
    }
    
    char * word::getWord()
    {
        return word;
    }
    
    void word::createWord(char*str)
    {
    word=str;
    }
    void word::打印单词(void)
    {
    
    coutStart有一本好书,代码有很多问题。谢谢,我试过了,效果很好。我有点习惯于用更高级的语言定义类,所以在编写类时让我感到困惑。
    word.h:2:7: error: forward declaration of 'class word'
    
    class word
    {
    public:
        char *word;
        void createWord(char *str);
        void print_word(void);
        char * getWord();
    };
    
    void word::createWord(char *str)
    {
        word = str;
    }
    
    void word::print_word(void)
    {
        cout<<word<<endl;
    }
    
    char * word::getWord()
    {
        return word;
    }