简体   繁体   English

将数组指针传递给函数

[英]Passing an array pointer into a function

I have a Node* hands[4]; 我有一个Node* hands[4]; and want to pass it into a function called Deal like Deal(deck,hands,4,"one-at-a-time",13); 并希望将其传递给诸如Deal之类的Deal(deck,hands,4,"one-at-a-time",13);函数Deal(deck,hands,4,"one-at-a-time",13);

When I use the following function... 当我使用以下功能时...

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

I get this... 我明白了...

/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

The following prototype (the first variant you mentioned) is correct: 以下原型(您提到的第一个变体)是正确的:

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

when used for example like this: 例如这样使用时:

Node n;
Node* deck = &n;
Node* hands[4];
Deal(deck, hands, 4, "one-at-a-time", 13);

just note that hands in this case is just an array of pointers that don't point to any instances of Node yet. 只是注意, hands在这种情况下,仅仅是不指向任何情况下指针数组Node呢。 Make sure they point to valid objects before you try to use them: 在尝试使用它们之前,请确保它们指向有效对象:

for (int i = 0; i < num_hands; ++i)
    hands[i] = new Node();
Node [] *hands

...should be ...应该

Node* hands[]

or 要么

Node** hands

One of the solutions is 解决方案之一是

Deal(Node *deck, Node **hands, int num_hands, const std::string &type, int num_cards)

You can call as you have in your example. 您可以像在示例中一样进行呼叫。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM