Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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++,我困惑了为什么我需要一个对象数组(比如节点结构)的双指针。下面是一个简单的代码片段来解释我的情况: struct HashNode{ HashNode* next; int data; int key; int hashCode; HashNode::HashNode( HashNode* next, const int& data, const int& key, const int& hashCode ) : next(next), data(data), key(key), hashCode(hashCode) {} }; class HashMap{ public: HashMap(); HashMap(int tableSize); ~HashMap(); private: //Here is the double pointer HashNode** table; }; HashMap::HashMap(){ //Here is the array initialization table = new HashNode*[100]; }_C++_Pointers_Double Pointer - Fatal编程技术网

为什么一个对象数组需要双指针?C++; 我正在练习一些C++,我困惑了为什么我需要一个对象数组(比如节点结构)的双指针。下面是一个简单的代码片段来解释我的情况: struct HashNode{ HashNode* next; int data; int key; int hashCode; HashNode::HashNode( HashNode* next, const int& data, const int& key, const int& hashCode ) : next(next), data(data), key(key), hashCode(hashCode) {} }; class HashMap{ public: HashMap(); HashMap(int tableSize); ~HashMap(); private: //Here is the double pointer HashNode** table; }; HashMap::HashMap(){ //Here is the array initialization table = new HashNode*[100]; }

为什么一个对象数组需要双指针?C++; 我正在练习一些C++,我困惑了为什么我需要一个对象数组(比如节点结构)的双指针。下面是一个简单的代码片段来解释我的情况: struct HashNode{ HashNode* next; int data; int key; int hashCode; HashNode::HashNode( HashNode* next, const int& data, const int& key, const int& hashCode ) : next(next), data(data), key(key), hashCode(hashCode) {} }; class HashMap{ public: HashMap(); HashMap(int tableSize); ~HashMap(); private: //Here is the double pointer HashNode** table; }; HashMap::HashMap(){ //Here is the array initialization table = new HashNode*[100]; },c++,pointers,double-pointer,C++,Pointers,Double Pointer,我已经删除了该问题不需要的代码 如果删除双指针,如下所示: HashNode* table; 及 我得到以下错误 hashmap.cpp: In method `HashMap::HashMap()': hashmap.cpp:87: no matching function for call to `HashNode::HashNode ()' hashmap.cpp:61: candidates are: HashNode::HashNode(const HashNode &) h

我已经删除了该问题不需要的代码

如果删除双指针,如下所示:

HashNode* table;

我得到以下错误

hashmap.cpp: In method `HashMap::HashMap()':
hashmap.cpp:87: no matching function for call to `HashNode::HashNode ()'
hashmap.cpp:61: candidates are: HashNode::HashNode(const HashNode &)
hashmap.cpp:58:                 HashNode::HashNode(HashNode *, const int &, cons
t int &, const int &)
这表明HashNode尝试运行构造函数

如果我只将数组的初始化更改为
table=newhashnode*[100]同时保留
HashNode*表然后我得到以下错误

hashmap.cpp: In method `HashMap::HashMap()':
hashmap.cpp:87: assignment to `HashNode *' from `HashNode **'
我的假设是,当我创建一个对象数组时,我需要对象的生命周期也适用于程序的持续时间。这要求我为对象和数组使用指针。因此,我需要为数组设置双指针,因为它指向指针,并且我需要为对象设置指针

然而,我仍然不确定,我无法在网上找到任何好的解释。有人能解释一下这种情况吗?

此实现用于管理哈希冲突。因此,
table
是指向
HashNode
的指针数组,这意味着它需要两个星号:

  • 一个星号来自数组元素的类型,即
    HashNode*
  • 另一个星号来自于创建一个
    HashNode*
这也是为什么在
new
表达式中有星号的原因:

table = new HashNode*[100];
//                  ^

看来你对C++指针很陌生。 您当前正在做的是创建100个指针的数组。所以编译器不会给你们任何错误,因为实际的对象不是用这一行创建的。 HashNode**table=newhashnode*[100]

但是当你使用 HashNode*table=newhashnode[100] 然后尝试为HashNode创建100个对象; 但您没有默认构造函数,所以编译器会给您上述错误

我已附上以下工作代码。看看吧

    #include <iostream>
    using namespace std;

    struct HashNode{

        HashNode* next;
        int data;
        int key;
        int hashCode;

        HashNode(){}

        HashNode(
            HashNode* next, 
            const int& data, 
            const int& key, 
            const int& hashCode
            ) : next(next), data(data), key(key), hashCode(hashCode)
            {}

    };

    class HashMap{
        public:
            HashMap();

        private:
            //Here is the double pointer
            HashNode* table;
    };

    HashMap::HashMap(){
        //Here is the array initialization 
        table = new HashNode[100];
    }

    int main() {
        // your code goes here

        HashMap ob;
        std::cout << "him" <<  std::endl;
        return 0;
    }
#包括
使用名称空间std;
结构哈希节点{
HashNode*next;
int数据;
int键;
int哈希码;
HashNode(){}
哈希节点(
HashNode*下一步,
常量int和数据,
const int&key,
常量int和hashCode
):下一个(下一个)、数据(数据)、键(键)、哈希代码(哈希代码)
{}
};
类哈希映射{
公众:
HashMap();
私人:
//这是双指针
HashNode*表;
};
HashMap::HashMap(){
//下面是数组初始化
table=新的HashNode[100];
}
int main(){
//你的密码在这里
HashMap-ob;

std::cout这里您要声明指针数组

HashNode**表


这是一个名为table的数组,指针类型为hashNode。

你需要阅读一篇文章。这是一个不好的问题吗?我认为这是一个很好的例子,我还没有掌握这个概念。我会看看书的。谢谢!问题格式不错,只是它的琐碎之处。有一个特殊的要求没有满足:放入一个It’在询问之前,他尽可能多地进行研究
    #include <iostream>
    using namespace std;

    struct HashNode{

        HashNode* next;
        int data;
        int key;
        int hashCode;

        HashNode(){}

        HashNode(
            HashNode* next, 
            const int& data, 
            const int& key, 
            const int& hashCode
            ) : next(next), data(data), key(key), hashCode(hashCode)
            {}

    };

    class HashMap{
        public:
            HashMap();

        private:
            //Here is the double pointer
            HashNode* table;
    };

    HashMap::HashMap(){
        //Here is the array initialization 
        table = new HashNode[100];
    }

    int main() {
        // your code goes here

        HashMap ob;
        std::cout << "him" <<  std::endl;
        return 0;
    }