将数组指针传递给函数

时间:2013-10-04 20:41:29

标签: c++ arrays pointers

我有一个Node* hands[4];,并希望将其传递给名为“像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

3 个答案:

答案 0 :(得分:2)

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

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();

答案 1 :(得分:0)

Node [] *hands

......应该是

Node* hands[]

Node** hands

答案 2 :(得分:0)

其中一个解决方案是

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

你可以在你的例子中打电话。