在C++中,友元类可以从友元类创建对象吗
Can a friend class create objects from its friend classes in C++?
这是我的两个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;
}
};
在第二个类中,我想从第一个类中创建一个对象并对其进行操作,首先在创建的对象中填充哈希表,然后删除所有经过排序的重复单词(如果Levenstein距离接近某个比例(。
#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"没有命名类型(如果可能的话,你能告诉我如何或一些来源让我了解它吗?如果不可能,那么一些提示将是一件很棒的事情,谢谢。
("我使用GCC 4.3.2"(
这看起来像一个循环include。
- "Hash_class.h"包括"remove_duplicates.h">
- "remove_duplicates.h"包括"Hash_class.h">
要修复它,请从"Hash_class.h"中删除此行:
#include "remove_duplicates.h"
这应该起作用,因为线
friend class remove_duplicates;
不需要关于如何实现CCD_ 1的信息。
编辑:为了避免进一步的错误,请将两个头文件都包装在包含保护中。
相关文章:
- 如何使用单独文件中的派生类访问友元函数对象
- 为什么创建友元类的实例会导致"undefined reference to"错误?
- 在C++中,友元类可以从友元类创建对象吗
- 为什么在定义类之前声明类的对象会在友元类中给出错误,而在友元函数中不会出错
- 声明一个模板函数,将模板类友元的两个对象(仅)带到这两个专用化
- 友元类对象是否可以在其成员函数中访问派生类对象的基类私有成员?
- Scott Meyers关于首选非成员非友元方法的建议是否适用于对象构造?
- 友元函数是否必须将常量对象作为其参数
- C++我们如何在不使用友元函数的情况下通过对象调用私有函数
- 为具有许多友元函数c++的模板类(2d动态)创建析构函数
- 如何通过友元函数将一个对象(类型a)传递给另一个对象(类型b)的私有对象(类型a)
- 模板对象的模板友元函数和命名空间
- C++:用友元类限制对象实例化
- 为三维阵列中的特定图元创建对象
- 如何将此对象传递给另一个对象并使用友元类 (c++) 访问其变量
- 使用指针数组中的对象调用重载的友元 ostream 插入运算符将返回内存地址
- C++:特定于友元的对象(嵌套类)
- 在模板类中创建重载友元运算符的模板
- 重载运算符<<在不使用友元函数的情况下输出对象成员
- 将动态数组对象传递给友元函数不会累积计数器