4 Replies - 726 Views - Last Post: 08 October 2012 - 07:47 PM Rate Topic: -----

#1 shadok  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 36
  • Joined: 08-October 12

Receiving: fatal error LNK1120 & error LNK2019

Posted 08 October 2012 - 07:09 PM

I'm new to C++ and for a homework assignment I'm trying to create a program which allows the user to play 'Rock, Paper, Scissors' against the computer. Right Now I'm stuck with the following 2 errors:

D:\C++ Files\Lab05-03RockPaperScissorsSol\Debug\Lab05-03RockPaperScissorsProj.exe : fatal error LNK1120: 1 unresolved externals

and

Main.obj : error LNK2019: unresolved external symbol "int __cdecl getComputerChoice(int)" (?getComputerChoice@@YAHH@Z) referenced in function _main

Any help with either of these would be greatly appreciated. I've tried to recreate my project, put different parts of the code into comments to check if any were causing the problem specifically, made sure I was in win32 format, and so on to no avail. I first tested the getUserChoice() function (line 41) and had no problems. The problems started arising after I created the second function, getComputerChoice() (line 54). Thanks in advance for any help. Here's my code so far:

 #include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

const int ROCK = 1;
const int PAPER = 2;
const int SCISSORS = 3;

int getUserChoice(int userChoice);
int getComputerChoice(int computerChoice);
void determineWinner (int, int);
void displayChoice(int);

int main()
{
	int userChoice;
	int computerChoice;

	unsigned seed = time(0); 
	srand(seed);
	
	computerChoice = getComputerChoice(computerChoice);
	userChoice = getUserChoice(userChoice);

	cout << computerChoice << endl << userChoice;
	
	while (userChoice != 4)
	{
		determineWinner(userChoice, computerChoice);

		computerChoice = getComputerChoice();
		userChoice = getUserChoice();
	}
	
	system("pause");
	return 0;
}


int getUserChoice(int)
{
	int userChoice;

	cout << " Game Menu \n" << "---------- \n";
	cout << "1) Rock" << endl << "2) Paper" << endl << "3) Scissors" << endl << "4) Quit" << endl;

	cout << "Enter your choice: ";
	cin >> userChoice;

	return userChoice;
}

int getComputerChoice()
{
	int computerChoice;
	
	unsigned seed = time(0); 
	srand(seed);

	computerChoice = 1 + rand() % 3;

	return computerChoice;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Receiving: fatal error LNK1120 & error LNK2019

#2 NathanMullenax  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 83
  • View blog
  • Posts: 176
  • Joined: 23-September 12

Re: Receiving: fatal error LNK1120 & error LNK2019

Posted 08 October 2012 - 07:22 PM

Hello:

There are is happening because your declaration (line 11) and definition (line 54) of getComputerChoice don't agree. One takes an integer parameter and the other one doesn't. If you edit line 11 so that it's just:

int getComputerChoice();



It should compile. Just a 'random' note: seeding random number generators can be a slow operation. You only need to do it once per program run, so you could remove it from the getComputerChoice() function since it's already happening in main(). Hope this helps.

Edit: also change line 23, so you aren't passing in a parameter to getComputerChoice()... getUserChoice() isn't using its parameter either, so you could nix that too.

This post has been edited by NathanMullenax: 08 October 2012 - 07:26 PM

Was This Post Helpful? 0
  • +
  • -

#3 shadok  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 36
  • Joined: 08-October 12

Re: Receiving: fatal error LNK1120 & error LNK2019

Posted 08 October 2012 - 07:30 PM

Thanks for the fast reply. When I change line 11 and try to build I get the error message d:\c++ files\lab05-03rockpaperscissorssol\lab05-03rockpaperscissorsproj\main.cpp(36): error C2660: 'getComputerChoice' : function does not take 1 arguments. When I try to run, I get the message Unable to start program 'D:\C++Files\Lab05-03RockPaperScissorsSol\Debug\Lab05-03RockPaperScissorsProj.exe'. The system cannot find the file specified. All of my projects are kept in the same file, yet I haven't had this issue before now.
Was This Post Helpful? 0
  • +
  • -

#4 NathanMullenax  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 83
  • View blog
  • Posts: 176
  • Joined: 23-September 12

Re: Receiving: fatal error LNK1120 & error LNK2019

Posted 08 October 2012 - 07:41 PM

Sorry, my answer was a bit scattershot:

The declarations for the two function should be:

int getUserChoice();     // note: no arguments
int getComputerChoice(); // note: no arguments



The definitions should agree with those declarations:

int getUserChoice()
{
    // as before
}
int getComputerChoice()
{
    // as before
}



Additionally, all of the calls to those functions should agree with their signatures (that is, take no arguments):

    computerChoice = getComputerChoice(); // no arguments
    userChoice = getUserChoice();         // no arguments
    cout << computerChoice << endl << userChoice;
    while (userChoice != 4)
    {
        determineWinner(userChoice, computerChoice);
        computerChoice = getComputerChoice(); 
        userChoice = getUserChoice();
    }


Was This Post Helpful? 2
  • +
  • -

#5 shadok  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 36
  • Joined: 08-October 12

Re: Receiving: fatal error LNK1120 & error LNK2019

Posted 08 October 2012 - 07:47 PM

Awesome, that did it. Thank you very much!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1