Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.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++_Arrays_Class - Fatal编程技术网

C++ 在类中声明数组并使用它

C++ 在类中声明数组并使用它,c++,arrays,class,C++,Arrays,Class,我想在类hash中创建一个数组,并在其方法中使用它。我尝试在public中声明它,但其他方法无法访问它。 这是我的班级宣言: class hash { public: string hash_table[size]; //size is global variable; void initialize(int,string*,int); //I pass `hash_table` pointer and two other `int` type //argument

我想在类
hash
中创建一个数组,并在其方法中使用它。我尝试在
public
中声明它,但其他方法无法访问它。 这是我的班级宣言:

class hash
{   
    public:
    string hash_table[size]; //size is global variable;
    void initialize(int,string*,int); //I pass `hash_table` pointer and two other `int` type //arguments.
    int hash_function(string, int); 
    void quad_prob(int, int, string* , string); //`hash_table` array is used in this method also.
    void print(string*); //to print `hash_table` array

};
我只能使用数组。 另外,当我使用hash h时,请解释一下在
int main()
中,发生了什么?
以下是完整的代码(不使用类结构):

看起来您正在尝试将现有代码转换为类,是吗?在类中保持您的
字符串哈希表[]
私有。然后从代码板上的每个函数中删除该参数。因此,
initialize
,正如人们所指出的,成为构造函数
hash(int n,int size)
,并初始化
hash\u table
hash\u table=newstring[size]
或诸如此类)。类似地,
print
变成了普通的
print()
,并且可以直接引用
hash\u表
,因为它是
hash
类的成员函数。

首先我建议您使用向量

其次,一旦在类中定义了它,您就可以通过名称访问它

class foo
{
public: 
    foo(): vecfoo(5)
    {}
    void DoSomeThing()
    {
         for_each(begin(vecfoo),end(vecfoo),[](const string &){...});
    }
private:
    vector<string> vecfoo;


};
class-foo
{
公众:
foo():vecfoo(5)
{}
无效剂量测定法()
{
对于每个(begin(vecfoo),end(vecfoo),[](const string&){…});
}
私人:
向量向量;
};

创建新哈希对象时,数组将被初始化。在使用对象的任何代码之前,都要使用hash h。创建新对象时,构造函数会设置对象,以便以后在代码中使用。从那里,您应该可以使用类中的代码。

是的,但这里通过初始化意味着我在使用其他方法之前对数组执行一些操作。这就是构造函数jobOK。我将把
initialize
的代码复制到构造函数。你说“不能访问它”是什么意思?请发布您问题的具体情况?
size
不能是全局变量-它必须是常量,因为
hash
类型的大小必须在编译时已知。“首先,我建议您使用向量。”他不能。这是他的家庭作业。。。