Hi guys, im trying to call a function with in another and for some reason i am getting


In function `int bDisplay()':
`move' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)
In function `int move()':
`int move()' used prior to declaration

but the problem is that i have that function declared =(. What going on? Im making a memory tic tac toe game with AI and im fairly new into coding. So please don't flame my syntax =)

Also if it would be more clear to you guys if i posted everything i have right now please ask. Its not that long right now so it wouldnt take up that much space.

int bDisplay()
{
    cout<<"\n\n\n\n\n";
    cout<<"\t\t\t\t"<<sOne<<" | "<<sTwo<<" | "<<sThree<<endl;
    cout<<"\t\t\t\t----------"<<endl;
    cout<<"\t\t\t\t"<<sFour<<" | "<<sFive<<" | "<<sSix<<endl;
    cout<<"\t\t\t\t----------"<<endl;
    cout<<"\t\t\t\t"<<sSeven<<" | "<<sEight<<" | "<<sNine<<endl;
    Sleep(2000);
    system("cls");
    move();    
}

int winCond()
{
    if (sOne == 'x' && sTwo == 'x' && sThree == 'x'){win();}
    if (sOne == 'x' && sFour == 'x' && sSeven == 'x'){win();}
    if (sOne == 'x' && sFive == 'x' && sNine == 'x'){win();}
    if (sTwo == 'x' && sFive == 'x' && sEight == 'x'){win();}
    if (sThree == 'x' && sFive == 'x' && sSeven == 'x'){win();}
    if (sThree == 'x' && sSix == 'x' && sNine == 'x'){win();}
    if (sFour == 'x' && sFive == 'x' && sSix == 'x'){win();}
    if (sSeven == 'x' && sEight == 'x' && sNine == 'x'){win();}
}


int move()
{
    bool repeat;
    char choice;
    winCond();
    
    do {
        
        repeat = false;
        cout<<"Please enter a selection 1-9:";
        cin>>choice;
        
        switch (choice)
        {
               case 1:
etc....

Recommended Answers

All 8 Replies

without the function call aswell it is hard to see what the problem is.

Chris

Use a 2d array to represent the tic tac toe grid, not a seperate variable for each place >.< . And also...post the whole code. I don't understand why your calling wincond() at the start of main. It does literally nothing there. Also, never forget to initialize variables! Also, you are inputting a choice of type char, and are checking it against integers. While char and ints are very similar, the compiler does an ascii conversion for you in cases like this. If you want to check a char against an int, use single quotes '1' around it. If you don't, it will check against the ascii values of the char, whcih for 1 is 49 decimal, or 31 in hex. Ascii Table

You do have function prototypes, and the prototype for move( ) agrees with your implementation?

Seeing your full program would help.

#include <iostream>
#include <windows.h>

using namespace std;

int sOne = 1;
int sTwo = 2;
int sThree = 3;
int sFour = 4;
int sFive = 5;
int sSix = 6;
int sSeven = 7;
int sEight = 8;
int sNine = 9;

int win()
{
    cout<<"gratz, you win the game!";
    return (0);
}

int bDisplay()
{
    cout<<"\n\n\n\n\n";
    cout<<"\t\t\t\t"<<sOne<<" | "<<sTwo<<" | "<<sThree<<endl;
    cout<<"\t\t\t\t----------"<<endl;
    cout<<"\t\t\t\t"<<sFour<<" | "<<sFive<<" | "<<sSix<<endl;
    cout<<"\t\t\t\t----------"<<endl;
    cout<<"\t\t\t\t"<<sSeven<<" | "<<sEight<<" | "<<sNine<<endl;
    Sleep(2000);
    system("cls");
    move();    
}

int winCond()
{
    if (sOne == 'x' && sTwo == 'x' && sThree == 'x'){win();}
    if (sOne == 'x' && sFour == 'x' && sSeven == 'x'){win();}
    if (sOne == 'x' && sFive == 'x' && sNine == 'x'){win();}
    if (sTwo == 'x' && sFive == 'x' && sEight == 'x'){win();}
    if (sThree == 'x' && sFive == 'x' && sSeven == 'x'){win();}
    if (sThree == 'x' && sSix == 'x' && sNine == 'x'){win();}
    if (sFour == 'x' && sFive == 'x' && sSix == 'x'){win();}
    if (sSeven == 'x' && sEight == 'x' && sNine == 'x'){win();}
}


int move()
{
    bool repeat;
    char choice;
    winCond();
    
    do {
        
        repeat = false;
        cout<<"Please enter a selection 1-9:";
        cin>>choice;
        
        switch (choice)
        {
               case 1:
                    
                    sOne = 'x';
                    break;
                    
               case 2:
                    
                    sTwo = 'x';
                    break;
               
               case 3:
                    
                    sThree = 'x';
                    break;
                    
               case 4:
                    
                    sFour = 'x';
                    break;
                    
               case 5:
                    
                    sFive = 'x';
                    break;
                    
               case 6:
                    
                    sSix = 'x';
                    break;
                    
               case 7:
                    
                    sSeven = 'x';
                    break;
                    
               case 8:
                    
                    sEight = 'x';
                    break;
                    
               case 9:
                    
                    sNine = 'x';
                    break;
                    
               default:
                    
                    cout<<"Sorry, bad input, please choose again.";
                    repeat = true;
                    }
                    }
               while (repeat);
               ai();
               }

Theres the full code right now. I didn't use arrays because of the simplicity of assigning new values to the int variables. But ya, their is no main() right now because im building the components needed first, before I attach them all to eachother. Again, i have not been programming for long, only a couple weeks, so take it easy on me =( Thanks!

You will need a main before you can properly compile your program.

Because as of right now I have no idea of what your program is supposed to do. Also you need to have return statements for your functions since they are all declared int functionName If you do not want to return anything remove the int and make it void functionName .

As I guessed, no function prototypes. The compiler sees your call of the move( ) function, but has no idea what that is, not having gottne that far in the code yet. Got back to your textbook or here for an explanation of what you need to do.

As kenji points out, you have at present only a collection of functions and some global variables (ooohh, you should be slapped on the wrist for that, too), but no actual program. Until you write function main( ), nothing will actually run.

#include <iostream>
#include <windows.h>

using namespace std;

int sOne = 1;
int sTwo = 2;
int sThree = 3;
int sFour = 4;
int sFive = 5;
int sSix = 6;
int sSeven = 7;
int sEight = 8;
int sNine = 9;

int win()
{
    cout<<"gratz, you win the game!";
    return (0);
}

int bDisplay()
{
    cout<<"\n\n\n\n\n";
    cout<<"\t\t\t\t"<<sOne<<" | "<<sTwo<<" | "<<sThree<<endl;
    cout<<"\t\t\t\t----------"<<endl;
    cout<<"\t\t\t\t"<<sFour<<" | "<<sFive<<" | "<<sSix<<endl;
    cout<<"\t\t\t\t----------"<<endl;
    cout<<"\t\t\t\t"<<sSeven<<" | "<<sEight<<" | "<<sNine<<endl;
    Sleep(2000);
    system("cls");
    move();    
}

int winCond()
{
    if (sOne == 'x' && sTwo == 'x' && sThree == 'x'){win();}
    if (sOne == 'x' && sFour == 'x' && sSeven == 'x'){win();}
    if (sOne == 'x' && sFive == 'x' && sNine == 'x'){win();}
    if (sTwo == 'x' && sFive == 'x' && sEight == 'x'){win();}
    if (sThree == 'x' && sFive == 'x' && sSeven == 'x'){win();}
    if (sThree == 'x' && sSix == 'x' && sNine == 'x'){win();}
    if (sFour == 'x' && sFive == 'x' && sSix == 'x'){win();}
    if (sSeven == 'x' && sEight == 'x' && sNine == 'x'){win();}
}


int move()
{
    bool repeat;
    char choice;
    winCond();
    
    do {
        
        repeat = false;
        cout<<"Please enter a selection 1-9:";
        cin>>choice;
        
        switch (choice)
        {
               case 1:
                    
                    sOne = 'x';
                    break;
                    
               case 2:
                    
                    sTwo = 'x';
                    break;
               
               case 3:
                    
                    sThree = 'x';
                    break;
                    
               case 4:
                    
                    sFour = 'x';
                    break;
                    
               case 5:
                    
                    sFive = 'x';
                    break;
                    
               case 6:
                    
                    sSix = 'x';
                    break;
                    
               case 7:
                    
                    sSeven = 'x';
                    break;
                    
               case 8:
                    
                    sEight = 'x';
                    break;
                    
               case 9:
                    
                    sNine = 'x';
                    break;
                    
               default:
                    
                    cout<<"Sorry, bad input, please choose again.";
                    repeat = true;
                    }
                    }
               while (repeat);
               ai();
               }

Theres the full code right now. I didn't use arrays because of the simplicity of assigning new values to the int variables. But ya, their is no main() right now because im building the components needed first, before I attach them all to eachother. Again, i have not been programming for long, only a couple weeks, so take it easy on me =( Thanks!

Hi,

Please write the body of move function before bDisplay() function. Your mistake is that
You are calling the move() function before, but you are defining its body after your calling function. Write your code in parent child top to bottom hierarchy. ok It will now compile and run successfully.okay

--
Regards,
Asif

ok, that makes some sense because i know ive dinked around calling functions before. thanks. Im also going to do some research on the prototype stuff.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.