-
October 13th, 2011, 08:39 PM
#1
Linker error: fatal error LNK1120: 1 unresolved externals
Hi,
I'm coding simple Tic Tac Toe game, however I get a build error and I'm not sure how to resolve it:
Code:
1>------ Build started: Project: TicTacToe, Configuration: Debug Win32 ------
1>LINK : error LNK2001: unresolved external symbol _mainCRTStartup
1>C:\Users\Documents\Projects\C++\TicTacToe\Debug\TicTacToe.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
From what I have researched , it is a linker error , but I am not sure what this entails.
Here's the Code:
Code:
#include <iostream>
using namespace std;
void printBoard()
{
cout << cell1 <<"|"<< cell2 << "|" << cell3 << endl;
cout <<"-+-+-"<< endl;
cout << cell4 <<"|"<< cell5 << "|" << cell6 << endl;
cout <<"-+-+-"<< endl;
cout << cell7 <<"|"<< cell8 << "|" << cell9 << endl;
cout <<"-+-+-"<< endl;
}
int main()
{
char cell1 = 1;
char cell2 = 2;
char cell3 = 3;
char cell4 = 4;
char cell5 = 5;
char cell6 = 6;
char cell7 = 7;
char cell8 = 8;
char cell9 = 9;
char player = 1;
char mark;
do
{
cout << cell1 <<"|"<< cell2 << "|" << cell3 << endl;
cout <<"-+-+-"<< endl;
cout << cell4 <<"|"<< cell5 << "|" << cell6 << endl;
cout <<"-+-+-"<< endl;
cout << cell7 <<"|"<< cell8 << "|" << cell9 << endl;
cout <<"-+-+-"<< endl;
printBoard();
//Handle player marker
if (player = 1)
{
mark = 'X';
}
else
{
mark = 'O';
}
do
{
cout << "Player "<<player<< ": /n";
char move;
cin >> move;
bool invalid = false;
//Validate move
if (move == 1 && cell1 == 1)
{
cell1 = mark;
}
else if (move == 2 && cell2 == 2)
{
cell2 = mark;
}
else if (move == 3 && cell3 == 3)
{
cell3 = mark;
}
else if (move == 4 && cell4 == 4)
{
cell4 = mark;
}
else if (move == 5 && cell5 == 5)
{
cell5 = mark;
}
else if (move == 6 && cell6 == 6)
{
cell6 = mark;
}
else if (move == 7 && cell7 == 7)
{
cell7 = mark;
}
else if (move == 8 && cell8 == 8)
{
cell8 = mark;
}
else if (move == 9 && cell9 == 9)
{
cell9 = mark;
}
else
{
cout <<" Inalid Move";
invalid = true;
}
}
while (invalid = true);
//Check for winning & draw conditions
bool gameover = false;
bool draw = false;
if (cell1 != 1)
{
if (cell1 == cell2 && cell1 == cell3)
{
gameover = true;
}
else if (cell1 == cell4 && cell1 == cell7)
{
gameover = true;
}
}
if (cell9 != 9)
{
if (cell9 == cell3 && cell9 == cell6)
{
gameover = true;
}
else if (cell9 == cell8 && cell9 == cell7)
{
gameover = true;
}
}
if (cell5 =! 5)
{
if (cell5 == cell2 && cell5 == cell8)
{
gameover = true;
}
else if (cell5 == cell4 && cell5 == cell6)
{
gameover = true;
}
else if (cell5 == cell1 && cell5 == cell9)
{
gameover = true;
}
else if (cell5 == cell3 && cell5 == cell7)
{
gameover = true;
}
}
// Check for draw conditions
if ( cell1 != 1 && cell2 != 2 && cell3 =! 3 &&
cell4 != 4 && cell5 != 5 && cell6 != 6 &&
cell7 != 7 && cell8 != 8 && cell9 != 9)
{
cout <<" Its a draw"
gameover = true;
draw = true;
}
if (gameover)
{
if (draw = false)
{
cout << "Player "<< player << " wins!"
}
printBoard();
cout<< "Play again? (y/n)"<< endl;
char playAgain;
cin >> playAgain;
//Reset board if user wants to play again
if (playAgain == y)
{
gameover = false;
cell1 = 1;
cell2 = 2;
cell3 = 3;
cell4 = 4;
cell5 = 5;
cell6 = 6;
cell7 = 7;
cell8 = 8;
cell9 = 9;
player = 1;
}
}
else
{
//Alternate players
if (player == 1)
{
player = 2;
}
else
{
player = 1;
}
}
}
while (gameover == false);
return 0;
}
Thanks in advance.
-
October 13th, 2011, 09:23 PM
#2
Re: Linker error: fatal error LNK1120: 1 unresolved externals
Usually that means you don't have a main() function. However, you do. Therefore, the other possibility is that the linker is expecting a different main function. This may indicate you have the wrong project type selected. For instance, if your project type is "Windows Application" rather than "Console Application", it may be looking for WinMain(). Change it to Console Application or create a new project file.
-
October 17th, 2011, 10:30 AM
#3
Re: Linker error: fatal error LNK1120: 1 unresolved externals
Originally Posted by Lindley
Usually that means you don't have a main() function. However, you do. Therefore, the other possibility is that the linker is expecting a different main function. This may indicate you have the wrong project type selected. For instance, if your project type is "Windows Application" rather than "Console Application", it may be looking for WinMain(). Change it to Console Application or create a new project file.
I can confirm that I have the right project selected - Win32 console project. Are there any any other suggestions to solve this?
Thanks.
-
October 17th, 2011, 10:40 AM
#4
Re: Linker error: fatal error LNK1120: 1 unresolved externals
Didn't even come close to compiling for me. 16 errors. Post the actual code generating the error.
-
October 17th, 2011, 11:30 AM
#5
Re: Linker error: fatal error LNK1120: 1 unresolved externals
Originally Posted by GCDEF
Didn't even come close to compiling for me. 16 errors. Post the actual code generating the error.
Thanks GCDEF. I created a new project using the same code and it came up with error messages that did not show up in the original project. The only error message seen in the original project was the one I stated in my first post. In this new project, it found a few errors (mainly stupid syntax errors made) and I have resolved most of them apart from 2 of them.
Here are the error messages:
Code:
1>------ Build started: Project: TicTacToe1, Configuration: Debug Win32 ------
1> TicTacToe1.cpp
1>c:\users\fdama\documents\projects\c++\tictactoe1\tictactoe1\tictactoe1.cpp(111): error C2065: 'invalid' : undeclared identifier
1>c:\users\fdama\documents\projects\c++\tictactoe1\tictactoe1\tictactoe1.cpp(229): error C2065: 'gameover' : undeclared identifier
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
And here is my code (I have marked where the errors are in the code with " \\ERROR HERE " :
Code:
#include <iostream>
using namespace std;
//void printBoard()
//{
// cout << cell1 <<"|"<< cell2 << "|" << cell3 << endl;
// cout <<"-+-+-"<< endl;
// cout << cell4 <<"|"<< cell5 << "|" << cell6 << endl;
// cout <<"-+-+-"<< endl;
// cout << cell7 <<"|"<< cell8 << "|" << cell9 << endl;
// cout <<"-+-+-"<< endl;
//}
int main()
{
char cell1 = 1;
char cell2 = 2;
char cell3 = 3;
char cell4 = 4;
char cell5 = 5;
char cell6 = 6;
char cell7 = 7;
char cell8 = 8;
char cell9 = 9;
char player = 1;
char mark;
do
{
cout << cell1 <<"|"<< cell2 << "|" << cell3 << endl;
cout <<"-+-+-"<< endl;
cout << cell4 <<"|"<< cell5 << "|" << cell6 << endl;
cout <<"-+-+-"<< endl;
cout << cell7 <<"|"<< cell8 << "|" << cell9 << endl;
cout <<"-+-+-"<< endl;
//printBoard();
//Handle player marker
if (player = 1)
{
mark = 'X';
}
else
{
mark = 'O';
}
do
{
cout << "Player "<<player<< ": /n";
char move;
cin >> move;
bool invalid = false;
//Validate move
if (move == 1 && cell1 == 1)
{
cell1 = mark;
}
else if (move == 2 && cell2 == 2)
{
cell2 = mark;
}
else if (move == 3 && cell3 == 3)
{
cell3 = mark;
}
else if (move == 4 && cell4 == 4)
{
cell4 = mark;
}
else if (move == 5 && cell5 == 5)
{
cell5 = mark;
}
else if (move == 6 && cell6 == 6)
{
cell6 = mark;
}
else if (move == 7 && cell7 == 7)
{
cell7 = mark;
}
else if (move == 8 && cell8 == 8)
{
cell8 = mark;
}
else if (move == 9 && cell9 == 9)
{
cell9 = mark;
}
else
{
cout <<" Invalid Move";
invalid = true;
}
}
while (invalid); // ERROR HERE
//Check for winning & draw conditions
bool gameover = false;
bool draw = false;
if (cell1 != 1)
{
if (cell1 == cell2 && cell1 == cell3)
{
gameover = true;
}
else if (cell1 == cell4 && cell1 == cell7)
{
gameover = true;
}
}
if (cell9 != 9)
{
if (cell9 == cell3 && cell9 == cell6)
{
gameover = true;
}
else if (cell9 == cell8 && cell9 == cell7)
{
gameover = true;
}
}
if (cell5 =! 5)
{
if (cell5 == cell2 && cell5 == cell8)
{
gameover = true;
}
else if (cell5 == cell4 && cell5 == cell6)
{
gameover = true;
}
else if (cell5 == cell1 && cell5 == cell9)
{
gameover = true;
}
else if (cell5 == cell3 && cell5 == cell7)
{
gameover = true;
}
}
// Check for draw conditions
if ( cell1 != 1 && cell2 != 2 && cell3 != 3 &&
cell4 != 4 && cell5 != 5 && cell6 != 6 &&
cell7 != 7 && cell8 != 8 && cell9 != 9)
{
cout <<" Its a draw";
gameover = true;
draw = true;
}
if (gameover)
{
if (draw = false)
{
cout << "Player "<< player << " wins!";
}
//printBoard();
cout << cell1 <<"|"<< cell2 << "|" << cell3 << endl;
cout <<"-+-+-"<< endl;
cout << cell4 <<"|"<< cell5 << "|" << cell6 << endl;
cout <<"-+-+-"<< endl;
cout << cell7 <<"|"<< cell8 << "|" << cell9 << endl;
cout <<"-+-+-"<< endl;
cout<< "Play again? (y/n)"<< endl;
char playAgain;
cin >> playAgain;
//Reset board if user wants to play again
if (playAgain == 'y')
{
gameover = false;
cell1 = 1;
cell2 = 2;
cell3 = 3;
cell4 = 4;
cell5 = 5;
cell6 = 6;
cell7 = 7;
cell8 = 8;
cell9 = 9;
player = 1;
}
}
else
{
//Alternate players
if (player == 1)
{
player = 2;
}
else
{
player = 1;
}
}
}
while (gameover == false); //ERROR HERE
return 0;
}
-
October 17th, 2011, 12:17 PM
#6
Re: Linker error: fatal error LNK1120: 1 unresolved externals
You have declared "invalid" within the do-while loop. You therefore can't use it in the while condition----its scope ends at the }. Just move the declaration outside the loop. (You can still set it to false within the loop if you like, but that doesn't appear necessary to me.)
A similar problem exists for gameover.
-
October 17th, 2011, 10:11 PM
#7
Re: Linker error: fatal error LNK1120: 1 unresolved externals
Originally Posted by Lindley
You have declared "invalid" within the do-while loop. You therefore can't use it in the while condition----its scope ends at the }. Just move the declaration outside the loop. (You can still set it to false within the loop if you like, but that doesn't appear necessary to me.)
A similar problem exists for gameover.
Thanks Lindley. I always learn something new here.
-
October 17th, 2011, 10:26 PM
#8
Re: Linker error: fatal error LNK1120: 1 unresolved externals
The program now runs however something strange happens. Firstly the PC 'beeps' when the console app launches. In the 9 cells of the game board which should show the numbers 1-9, It instead shows icons from playing cards such as aces, hearts and spades.
Any Idea as to why this happens?
Thanks
The code:
Code:
#include <iostream>
using namespace std;
//void printBoard()
//{
// cout << cell1 <<"|"<< cell2 << "|" << cell3 << endl;
// cout <<"-+-+-"<< endl;
// cout << cell4 <<"|"<< cell5 << "|" << cell6 << endl;
// cout <<"-+-+-"<< endl;
// cout << cell7 <<"|"<< cell8 << "|" << cell9 << endl;
// cout <<"-+-+-"<< endl;
//}
int main()
{
char cell1 = 1;
char cell2 = 2;
char cell3 = 3;
char cell4 = 4;
char cell5 = 5;
char cell6 = 6;
char cell7 = 7;
char cell8 = 8;
char cell9 = 9;
char player = 1;
char mark;
bool gameover = false;
do
{
cout << cell1 <<"|"<< cell2 << "|" << cell3 << endl;
cout <<"-+-+-"<< endl;
cout << cell4 <<"|"<< cell5 << "|" << cell6 << endl;
cout <<"-+-+-"<< endl;
cout << cell7 <<"|"<< cell8 << "|" << cell9 << endl;
cout <<"-+-+-"<< endl;
//printBoard();
//Handle player marker
if (player = 1)
{
mark = 'X';
}
else
{
mark = 'O';
}
bool invalid = false;
do
{
cout << "Player "<<player<< ": /n";
char move;
cin >> move;
//Validate move
if (move == 1 && cell1 == 1)
{
cell1 = mark;
}
else if (move == 2 && cell2 == 2)
{
cell2 = mark;
}
else if (move == 3 && cell3 == 3)
{
cell3 = mark;
}
else if (move == 4 && cell4 == 4)
{
cell4 = mark;
}
else if (move == 5 && cell5 == 5)
{
cell5 = mark;
}
else if (move == 6 && cell6 == 6)
{
cell6 = mark;
}
else if (move == 7 && cell7 == 7)
{
cell7 = mark;
}
else if (move == 8 && cell8 == 8)
{
cell8 = mark;
}
else if (move == 9 && cell9 == 9)
{
cell9 = mark;
}
else
{
cout <<" Invalid Move";
invalid = true;
}
}
while (invalid);
//Check for winning & draw conditions
bool draw = false;
if (cell1 != 1)
{
if (cell1 == cell2 && cell1 == cell3)
{
gameover = true;
}
else if (cell1 == cell4 && cell1 == cell7)
{
gameover = true;
}
}
if (cell9 != 9)
{
if (cell9 == cell3 && cell9 == cell6)
{
gameover = true;
}
else if (cell9 == cell8 && cell9 == cell7)
{
gameover = true;
}
}
if (cell5 =! 5)
{
if (cell5 == cell2 && cell5 == cell8)
{
gameover = true;
}
else if (cell5 == cell4 && cell5 == cell6)
{
gameover = true;
}
else if (cell5 == cell1 && cell5 == cell9)
{
gameover = true;
}
else if (cell5 == cell3 && cell5 == cell7)
{
gameover = true;
}
}
// Check for draw conditions
if ( cell1 != 1 && cell2 != 2 && cell3 != 3 &&
cell4 != 4 && cell5 != 5 && cell6 != 6 &&
cell7 != 7 && cell8 != 8 && cell9 != 9)
{
cout <<" Its a draw";
gameover = true;
draw = true;
}
if (gameover)
{
if (draw = false)
{
cout << "Player "<< player << " wins!";
}
//printBoard();
cout << cell1 <<"|"<< cell2 << "|" << cell3 << endl;
cout <<"-+-+-"<< endl;
cout << cell4 <<"|"<< cell5 << "|" << cell6 << endl;
cout <<"-+-+-"<< endl;
cout << cell7 <<"|"<< cell8 << "|" << cell9 << endl;
cout <<"-+-+-"<< endl;
cout<< "Play again? (y/n)"<< endl;
char playAgain;
cin >> playAgain;
//Reset board if user wants to play again
if (playAgain == 'y')
{
gameover = false;
cell1 = 1;
cell2 = 2;
cell3 = 3;
cell4 = 4;
cell5 = 5;
cell6 = 6;
cell7 = 7;
cell8 = 8;
cell9 = 9;
player = 1;
}
}
else
{
//Alternate players
if (player == 1)
{
player = 2;
}
else
{
player = 1;
}
}
}
while (gameover == false);
return 0;
}
Last edited by fsdama; October 17th, 2011 at 10:28 PM.
Reason: added code
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|