4

这些是我的两个 C++ 头文件中的代码,我在其中声明了两个类,一个是另一个类的朋友:

==>第一个类创建一个哈希表并用给定文件中的单词填充它。

#include "remove_duplicates.h"
class Hash_class{

protected:
    list<string> *hashTable;
    string input_file;
    string output_file;
    int input_file_size;
    friend class remove_duplicates;
    //void file_size();

public:

    /*load the words in the hash Table;
    by calculating the hash Code of each string*/
    Hash_class(string input_file,string output_file="output_file.txt");

    ~Hash_class(){
        fclose(input_file);
        fclose(ouput_file);
    }

    int hashCode( const string input_word);

    void loadInHash();

    void write_in_output();

    int get_input_file_size(){
        return input_file_size;
    }

    string get_input_file(){
        return input_file;
    }

    string get_output_file(){
        return output_file;
    }
};

在第二个类中,我想从第一个类创建一个对象并对其进行操作,首先填充创建的对象内的哈希表,然后删除所有被认为重复的单词(如果 Levenshtein 距离接近某个比例) .

#include "Hash_class.h"
class remove_duplicates{
protected:
    Hash_class hash_object;
public:

    remove_duplicates(Hash_class & hash_object);

    int Levenshtein_distance(string s1,string s2);

    void set_purge();

};

问题是当我编译代码时出现错误:([Error] 'Hash_class' does not name a type)如果可能的话,请告诉我如何或一些我可以了解它的来源。如果不可能,那么一些提示将是一件好事,谢谢。

(“我使用 GCC 4.3.2”)

4

1 回答 1

1

这看起来像一个循环包含。

  • “Hash_class.h”包括“remove_duplicates.h”
  • “remove_duplicates.h”包括“Hash_class.h”

要修复它,请从“Hash_class.h”中删除这一行:

#include "remove_duplicates.h"

这应该有效,因为这条线

friend class remove_duplicates;

不需要有关如何class remove_duplicates实施的信息。

编辑:为避免进一步的错误,请将两个头文件都包含在include guards中。

于 2018-07-25T16:04:54.340 回答