Problem

This is a discussion on Problem within the C++ Programming forums, part of the General Programming Boards category; source.obj : error LNK2001: unresolved external symbol "bool __cdecl safe(int)" (?safe@@YA_NH@Z) Would anyone know why I get this message when ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    73

    Problem

    source.obj : error LNK2001: unresolved external symbol "bool __cdecl safe(int)" (?safe@@YA_NH@Z)


    Would anyone know why I get this message when ever i try to run my program.


    Code:
    #include <iostream>
    
    using namespace std;
    
    
    
    int num_of_suc;
    int nth_next_move(int n, int GameState[9]);
    void nth_next_game(int n, int GameState[9], int NextGame[9]);
    int number_of_moves(int GameState[9]);
    void not_in_state(int GameState[], int Return[]);
    bool member(int e, int GameState[9]);
    bool safe(int);
    bool game=false;
    void drawboard();
    char board[9]={0,1,2,3,4,5,6,7,8};
    void moves(int);
    void turn(int, int);
    
    
    
    
    
    
    
    	void nth_next_game(int n, int GameState[9], int NextGame[9])
    	{
    		for(int i=0;i<9;i++)
    		{
    			if(i<number_of_moves(GameState))
    				NextGame[i]=GameState[i];
    			else if (i == number_of_moves(GameState))
    				NextGame[i]=nth_next_move(n,GameState);
    			else
    				NextGame[i]=-1;
    		}
    		
    	}
    
    
    
    	int number_of_moves(int GameState[9])
    	{
    		int i=0;
    		while(GameState[i] != -1)
    			i++;
    
    		return i;
    	}
    
    	int nth_next_move(int n, int GameState[9])
    	{
    		int NotIn[9];
    		not_in_state(GameState,NotIn);
    		return NotIn[n-1];
    	} 
    
    
    
    
    	void not_in_state(int GameState[], int ret[])
    	{
    		int Counter=0;
    
    		for(int i=1;i<=9;i++)
    		if(!member(i,GameState))
    		{
    			ret[Counter]=i;
    			Counter++;
    		}
    
    		for(i=Counter;i<9;i++)
    			ret[i] = -1;
    
    	}
    
    
    	bool member(int e, int GameState[9])
    	{
    		bool flag=false;
    
    		for(int i=0;i<9;i++)
    			if(GameState[i]==e)
    				flag =true;
    
    		return flag;
    	}
    
    	bool safe (int gamestate[9])
    	{
    		//o's move only!!!!!!
    
    		bool safeflag=false;
    		num_of_suc=9-number_of_moves(gamestate);
    
    		int nextgame[9];
    
    		if (number_of_moves(gamestate)%2==1)
    		{
    			for (int i=1; i<=num_of_suc; i++)
    			{
    				nth_next_game(i, gamestate, nextgame);
    				if (safe(nextgame))
    					safeflag=true;
    			}
    		}
    		else // x's move only!!!!!!
    		{
    			bool safeflag=true;
    
    			if (number_of_moves(gamestate)%2==1)
    			{
    				for (int i=1; i<=num_of_suc; i++)
    				{
    					nth_next_game(i, gamestate, nextgame);
    					if (!safe(nextgame))
    						safeflag=false;
    				}
    			}
    		}
    		return 0;
    	}
    
    	void moves(int m)
    	{
    		cin >> m;
    
    		switch(m)
    		{
    			case 1:	if ((m==1)&&(board[0]!='x')&&(board[0]!='o'))
    						board[0]='x';
    					break;
    			case 2:	if ((m==2)&&(board[1]!='x')&&(board[1]!='o'))
    						board[1]='x';
    					break;
    			case 3:	if ((m==3)&&(board[2]!='x')&&(board[2]!='o'))
    						board[2]='x';
    					break;
    			case 4:	if ((m==4)&&(board[3]!='x')&&(board[3]!='o'))
    						board[3]='x';
    					break;
    			case 5:	if ((m==5)&&(board[4]!='x')&&(board[4]!='o'))
    						board[4]='x';
    					break;
    			case 6:	if ((m==6)&&(board[5]!='x')&&(board[5]!='o'))
    						board[5]='x';
    					break;
    			case 7:	if ((m==7)&&(board[6]!='x')&&(board[6]!='o'))
    						board[6]='x';
    					break;
    			case 8:	if ((m==8)&&(board[7]!='x')&&(board[7]!='o'))
    						board[7]='x';
    					break;
    			case 9:	if ((m==9)&&(board[8]!='x')&&(board[8]!='o'))
    						board[8]='x';
    			default: cout << "Cannot move there!" << endl;
    		}
    
    	}
    
    
    	////////////////////////////////////////////////////
    	//////////  Main Funciton  /////////////////////////
    	////////////////////////////////////////////////////
    
    	void main()
    	{
    		board[0]=' ';
    		board[1]=' ';
    		board[2]=' ';
    		board[3]=' ';
    		board[4]=' ';
    		board[5]=' ';
    		board[6]=' ';
    		board[7]=' ';
    		board[8]=' ';
    
    		int Game[9]={3,5,7,8,1,-1,-1,-1,-1};
    		int Next[9];
    
    		char x;
    
    		cout << "Would you like to play a game of Tic Tac Toe (Y/N)?" << endl;
    
    		cin >> x;
    
    		if (x=='y')
    			game=false;
    		else
    			if (x=='n')
    				game=true;
    
    		while (!game)
    		{
    			for (int count=0; count<9; count++)
    			{
    			
    				cout << "Choose a number between 1 and 9 to move" << endl;
    				drawboard();
    
    				if ((count==0)||(count==2)||(count==4)||(count==6)||(count==8))
    				{
    					moves(Game[count]);
    				}else
    				{
    					if (safe(Game[count]))
    					{
    						cout << "Game is Safe!" << endl;
    					}
    				}
    
    			}
    
    			if (count==9)
    			{
    				game=true;
    			}
    		}
    
    	}
    
    	void drawboard()
    	{
    		cout << board[0] << "|" << board[1] << "|" << board[2] << endl;
    		cout << board[3] << "|" << board[4] << "|" << board[5] << endl;
    		cout << board[6] << "|" << board[7] << "|" << board[8] << endl;
    	}
    There is my code..................I believe that the error is comeing form my use of the safe function (safe(Game[count]));

  2. #2
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    GŲteborg, Sweden
    Posts
    2,072
    Look at the declaration of safe.
    Then look at the definition of safe.

    See any differences?
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  3. #3
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    By the way.
    I don't know why people keep asking about the same problem.
    unresolved external symbol"...."
    And it's always the same answer.

    My question is:
    Is there any other cause for this error?

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    26

    Talking

    Well all i can tell you is that look at your functions because i see that you are colling some functions but as i know in C++ when you build a function you build it at at the header files and then the main pary you do at .cpp hope this helps
    for example this bool member(int e, int GameState[9]);
    you save as member.h and then the main part you do it in member.cpp this will get rid of it the LINK ERROR OK and you will have to do all for the functions that you colled
    C++ Is cool

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    73
    Umm safe is declared with an int argument. Then later on I use add safe(int gamestate[9]) that is what you are supposed to do. Now if I am not supposed to use the line if (safe(Game[9])) like that then how am I supposed to use it?

  6. #6
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,138
    You have to figure out how your function should look like. Then, write the function. Your forward declaration must look exactly the same as your implementation. It's basically the first line copied and with a semicolon. Compare your declaration with your implementation, they don't match.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    73
    Well i guess im not seeing the differance between

    Code:
    bool safe(int);
    
          bool safe(int gamestate[9])
          {
          }
    
    safe(Next[9]);
    All three statements have one argument that is of type int. So whats the deal?

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    73
    Ok i changed my implimentation of safe(blah blah) to bool safe (blah blah). Is that what yall where talking about?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21