簡體   English   中英

將數組指針傳遞給函數

[英]Passing an array pointer into a function

我有一個Node* hands[4]; 並希望將其傳遞給諸如Deal之類的Deal(deck,hands,4,"one-at-a-time",13);函數Deal(deck,hands,4,"one-at-a-time",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

以下原型(您提到的第一個變體)是正確的:

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在這種情況下,僅僅是不指向任何情況下指針數組Node呢。 在嘗試使用它們之前,請確保它們指向有效對象:

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

...應該

Node* hands[]

要么

Node** hands

解決方案之一是

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

您可以像在示例中一樣進行呼叫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM