1

I have a Node* hands[4]; and want to pass it into a function called Deal like 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
4
  • 1
    You probably just want a std::array<Node, 4>. Commented Oct 4, 2013 at 20:44
  • 1
    @chris This is for a Data Structures homework assignment and I have to use the Node* []. Commented Oct 4, 2013 at 20:45
  • Well, that sort of sucks then. The pointer looks useless at first glance, just introducing unneeded complexity, and an array that knows its size doesn't really make anything easier, just safer. As mentioned, the first is one option for correct syntax. Commented Oct 4, 2013 at 20:47
  • 2
    Please don't replace a question with a new one, it makes existing answers pointless. Commented Oct 4, 2013 at 21:02

3 Answers 3

2

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. 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();
Sign up to request clarification or add additional context in comments.

4 Comments

I used my first option, and for some reason I get a list of undefined references to methods when I try to compile.
@JonathanWrona: Edit your question, copy-paste the errors your compiler gives you and copy-paste the lines that compiler complains about.
Oh, how could I pass Node* hands[4] by reference so I can directly edit the data?
You are already passing it in a way that changes are visible to the caller. You are passing an array of pointers, your function modifies these pointers (elements), not the array itself.
0
Node [] *hands

...should be

Node* hands[]

or

Node** hands

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.