Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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_Pointers - Fatal编程技术网

C++ 将数组指针传递给函数

C++ 将数组指针传递给函数,c++,arrays,pointers,C++,Arrays,Pointers,我有一个节点*hands[4]并希望将其传递到一个名为Deal的函数中,如Deal(deck,hands,4,“一次一个”,13) 当我使用以下函数时 void Deal(Node *deck, Node *hands[], int num_hands, const std::string &type, int num_cards) 我明白了 /tmp/ccqckP1I.o:main.cpp:(.text+0x82a): undefined reference to `CutDeck(

我有一个
节点*hands[4]并希望将其传递到一个名为Deal的函数中,如
Deal(deck,hands,4,“一次一个”,13)

当我使用以下函数时

void Deal(Node *deck, Node *hands[], int num_hands, const std::string &type, int num_cards)
我明白了

/tmp/ccqckP1I.o:main.cpp:(.text+0x82a): undefined reference to `CutDeck(Node*, Node*&, Node*&, std::string const&)'
/tmp/ccqckP1I.o:main.cpp:(.text+0xb34): undefined reference to `DeleteAllCards(Node*&)'
/tmp/ccqckP1I.o:main.cpp:(.text+0xb3f): undefined reference to `DeleteAllCards(Node*&)'
/tmp/ccqckP1I.o:main.cpp:(.text+0x1030): undefined reference to `DeleteAllCards(Node*&)'
/tmp/ccqckP1I.o:main.cpp:(.text+0x103e): undefined reference to `DeleteAllCards(Node*&)'
/tmp/ccqckP1I.o:main.cpp:(.text+0x104c): undefined reference to `DeleteAllCards(Node*&)'
/tmp/ccqckP1I.o:main.cpp:(.text+0x105a): more undefined references to `DeleteAllCards(Node*&)' follow
/tmp/ccqckP1I.o:main.cpp:(.text+0x1348): undefined reference to `CutDeck(Node*, Node*&, Node*&, std::string const&)'
/tmp/ccqckP1I.o:main.cpp:(.text+0x1492): undefined reference to `CutDeck(Node*, Node*&, Node*&, std::string const&)'
/tmp/ccqckP1I.o:main.cpp:(.text+0x15dc): undefined reference to `CutDeck(Node*, Node*&, Node*&, std::string const&)'
/tmp/ccqckP1I.o:main.cpp:(.text+0x1726): undefined reference to `CutDeck(Node*, Node*&, Node*&, std::string const&)'
/tmp/ccqckP1I.o:main.cpp:(.text+0x1870): undefined reference to `CutDeck(Node*, Node*&, Node*&, std::string const&)'
/tmp/ccqckP1I.o:main.cpp:(.text+0x1c2d): more undefined references to `CutDeck(Node*, Node*&, Node*&, std::string const&)' follow
/tmp/ccqckP1I.o:main.cpp:(.text+0x1f8c): undefined reference to `DeleteAllCards(Node*&)'
/tmp/ccqckP1I.o:main.cpp:(.text+0x1f9a): undefined reference to `DeleteAllCards(Node*&)'
/tmp/ccqckP1I.o:main.cpp:(.text+0x1fa8): undefined reference to `DeleteAllCards(Node*&)'
/tmp/ccqckP1I.o:main.cpp:(.text+0x1fb6): undefined reference to `DeleteAllCards(Node*&)'
/tmp/ccqckP1I.o:main.cpp:(.text+0x1fc4): undefined reference to `DeleteAllCards(Node*&)'
/tmp/ccqckP1I.o:main.cpp:(.text+0x1fd2): more undefined references to `DeleteAllCards(Node*&)' follow
/usr/lib/gcc/i686-pc-cygwin/4.7.3/../../../../i686-pc-cygwin/bin/ld: /tmp/ccqckP1I.o: bad reloc address 0x1b in section `.text$_ZNSt11char_traitsIcE7compareEPKcS2_j[__ZNSt11char_traitsIcE7compareEPKcS2_j]'
collect2: error: ld returned 1 exit status
…应该是

Node* hands[]


解决办法之一是

Deal(Node *deck, Node **hands, int num_hands, const std::string &type, int num_cards)
您可以按示例中的方式调用。

以下原型(您提到的第一个变体)是正确的:

void Deal(Node *deck,
          Node *hands[],
          int num_hands,
          const std::string &type,
          int num_cards);
例如,当这样使用时:

Node n;
Node* deck = &n;
Node* hands[4];
Deal(deck, hands, 4, "one-at-a-time", 13);
请注意,在本例中,
hands
只是一个指针数组,它还没有指向
节点的任何实例。在尝试使用它们之前,请确保它们指向有效对象:

for (int i = 0; i < num_hands; ++i)
    hands[i] = new Node();
for(int i=0;i
您可能只需要一个
std::array
@chris这是一个数据结构作业,我必须使用节点*[]。好吧,那太糟糕了。指针乍一看似乎毫无用处,只是引入了不必要的复杂性,而一个知道其大小的数组并不会让任何事情变得更简单,只是更安全。如前所述,第一个选项是正确语法的一个选项。请不要将问题替换为新问题,这会使现有答案变得毫无意义。我使用了我的第一个选项,出于某种原因,我在尝试编译时得到了一个未定义的方法引用列表。@JonathanWrona:编辑你的问题,复制粘贴编译器给您的错误,并复制粘贴编译器抱怨的行。哦,我如何通过引用传递
Node*hands[4]
,以便直接编辑数据?您已经以调用方可以看到更改的方式传递了数据。在传递指针数组时,函数修改这些指针(元素),而不是数组本身。
Node n;
Node* deck = &n;
Node* hands[4];
Deal(deck, hands, 4, "one-at-a-time", 13);
for (int i = 0; i < num_hands; ++i)
    hands[i] = new Node();