Thread: Passing pointers

  1. #1
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124

    Passing pointers

    Let me walk you through my problem. I have class Test. In a function I have:
    Code:
    Test thing;
    Now I call another function and pass thing to it as &thing. That function recieves it as *ttt which is obviously a pointer. Now my problem comes in when I need to pass *ttt to another function. At the moment I have the following:
    Code:
    compAI( &row, &column, ttt );
    The function prototype looks as follows:
    Code:
    void compAI( int *, int *, TicTacToe * );
    The program compiles, but as it's being linked I get the following error:
    main.o(.text+0x113b):main.cpp: undefined reference to `compAI(int*, int*, TicTacToe*)'
    make.exe: *** [tictactoe-game.exe] Error 1
    I don't know how to pass a pointer from one function to another and am guessing that I've done it incorrectly. Assistance required please.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Less explanation, more code please. I'll assume that by class Test you mean class TicTacToe.

    >undefined reference to `compAI(int*, int*, TicTacToe*)'
    Have you given compAI a body?
    My best code is written with the delete key.

  3. #3
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124
    Snippets of code..

    Code:
    void onePlayer() {
    
       TicTacToe game;
    
       playSingleGame( player1, player2, count, difficulty, &game );
    
    }
    
    void playSingleGame( int p, int p2, int number, int diff, TicTacToe *ttt ) {
    
       compAI( &row, &column, ttt );
    
    }
    
    void CompAI( int *x, int *y, TicTacToe *ttt ) {
    
       int b1;
    
       b1->ttt.getBoard( 0, 0 );
    
    }
    There is obviously much more code in each of the functions, it's just that the compiler won't compile my program successfully. However if I comment out the call to compAI in function playSingleGame the program will compile.


    Building Makefile: "C:\Documents and Settings\Dylan v.d Merwe\My Documents\Coding\C++\Work\Complete Projects\TicTacToe\Makefile.win"
    Executing make...
    make.exe -f "C:\Documents and Settings\Dylan v.d Merwe\My Documents\Coding\C++\Work\Complete Projects\TicTacToe\Makefile.win" all
    g++.exe main.o tictactoe.o -o "tictactoe-game.exe" -L"C:/Program Files/Dev-Cpp/lib"

    main.o(.text+0x118d):main.cpp: undefined reference to `compAI(int*, int*, TicTacToe*)'

    make.exe: *** [tictactoe-game.exe] Error 1

    Execution terminated

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    If it's all in the same file (main.cpp) make sure there is a prototype of compAI(int*, int*, TicTacToe*) before you call it inside playSingleGame. You could also just define the function compAI before the function playSingleGame.

  5. #5
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124
    lol @ jlou. Dude, believe me, I know about prototypes. Look at the red part on my previous program, that is what I think is the problem.

    Here are my prototypes for the above mentioned functions:
    Code:
    void compAI( int *, int *, TicTacToe * );
    void playSingleGame( int, int, int, int, TicTacToe * );
    void onePlayer();

  6. #6
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124
    Oh, you must be joking!! I just made a supid typo!!

    CompAI instead of compAI. Thanks anyway peeps!

  7. #7
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    @cyberClown for laughing at jlou for mentioning prototypes when cyberClown didn't check the spelling first!

    Knowing your code was correct the whole time sure makes it easier to deal with stupid typos, though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing a string array to a function using pointers
    By asofaihp in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2009, 11:31 AM
  2. Passing Pointers by reference
    By Bladactania in forum C Programming
    Replies: 10
    Last Post: 02-13-2009, 10:14 AM
  3. Passing Structure Pointers to Functions
    By samus250 in forum C Programming
    Replies: 15
    Last Post: 03-20-2008, 03:13 PM
  4. passing pointers outside of functions
    By thomas41546 in forum C Programming
    Replies: 6
    Last Post: 01-26-2008, 06:00 PM
  5. Pointers and reference passing
    By Denis Itchy in forum C++ Programming
    Replies: 4
    Last Post: 12-13-2002, 01:36 AM