0

So I have checked some of the other posts about this error but I can't seem to track down the issue within my own code. I am working with three separate files. When I call functions in one file, they cause the error. From what I can tell everything is formatted and referred to correctly.

Core Code

#include <iostream>
#include <cstdlib> 
#include <ctime> 
#include <string>

//Task 1: Install Libaries and Get the GUI Running
#include "gui.h"

//Task 4: Implement the game engine
#include "engine.h"

//Include global constants
#include "constants.h"

using namespace std;

int main(int argc, char *argv[]){

//Seed random number generator
srand((unsigned)time(0)); 

//Task 2: Declare and Intialize Player Properties
int playerX = 0;
int playerY = 0;
int playerState = 0;

//Task 3: Declare and Initialize Background Properties'
int elementsX [MAX_NUM_ELEMENTS];
int elementsY [MAX_NUM_ELEMENTS];
int numElements = MAX_NUM_ELEMENTS;

//Initialize the game's data source
string gameFile("./data/background.txt");


//Task 5: Load Data from a file'
//loadBackground(gameFile,numElements,elementsX,elementsY);

//Task 6: Randomize Player Sprite Appearance
randomPlayer(playerX,playerY,playerState,MAX_POS_X,MAX_POS_Y,MAX_PLAYER_STATE);

//Construct GUI
GUI gui;

//Initialize Termination Criteria
bool quit = false;

//While the user hasn't quit
while(quit == false){

    //GUI waits for mouse events
    while(gui.observeEvent()){

        //GUI transmits quit event
        if(gui.quitGame()){
            quit=true;
        }
    }
    //Render Game Data
    gui.displayGameState(playerX,playerY,playerState,numElements,elementsX,elementsY);
}

//Return
return 0;

}

Engine:

#include <fstream>
#include <string>
#include "engine.h"
#include "constants.h"
#include <ctime>

using namespace std;

void loadBackground(string path, int numElements, int posX[], int posY[]){

fstream fin;
fin.open(path,ios::in);
int lineSize = 0;

fin >> numElements;
for(int i=0; i<numElements; i++){
    fin >> posX[i];
    fin >> posY[i];
}
fin.close();
}


void randomPlayer(int posX, int posY, int state, int MAX_POS_X,int MAX_POS_Y,int MAX_PLAYER_STATE){
srand((unsigned)time(0));
posX = rand()%MAX_POS_X;
posY = rand()%MAX_POS_Y;
state = rand()%MAX_PLAYER_STATE-1;

}

This issue occurs when the either of the two functions from Engine are called. I'm working in Visual Studio 2012 on a Surface Pro 3.

Also this is the full build error:

1>------ Build started: Project: Hmwk.1.Assignment, Configuration: Debug Win32 ------
1>hmwk.1.obj : error LNK2019: unresolved external symbol "void __cdecl loadBackground(class     std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,int &,int *     const,int * const)" (?loadBackground@@YAXV?$basic_string@DU?$char_traits@D@std@@V?    $allocator@D@2@@std@@AAHQAH2@Z) referenced in function _SDL_main
1>hmwk.1.obj : error LNK2019: unresolved external symbol "void __cdecl randomPlayer(int &,int     &,int &,int,int,int)" (?randomPlayer@@YAXAAH00HHH@Z) referenced in function _SDL_main
1>C:\Users\tmars_000\Desktop\Fall 2014\Intro to Game     Prog\Hmwk.1.Released\Debug\Hmwk.1.Assignment.exe : fatal error LNK1120: 2 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

engine.h

#ifndef ENGINE_H
#define ENGINE_H

#include <string>

using namespace std;

void loadBackground(string, int &, int[], int[]);
void randomPlayer(int &, int &, int &, int, int, int);

#endif
  • Could you please give us more infos about the error, and especially the error message preceeding LNK1120 as explained here: http://msdn.microsoft.com/en-us/library/z98k84c3.aspx – Christophe Sep 16 '14 at 19:16
  • Was adding it as you typed your comment! – Tanner Marshall Sep 16 '14 at 19:19
  • What is in engine.h? – KC-NH Sep 16 '14 at 19:23
  • 1
    Look closely at the linker error message, pay attention to the argument types. You just didn't write a loadBackground() function that matches the declaration in your .h file. Same problem for randomPlayer(). Careful with int vs int& and int[] vs int* – Hans Passant Sep 16 '14 at 19:25
  • Could you explain a bit more? I added engine.h and it looks like all of my arguments are correct.. – Tanner Marshall Sep 16 '14 at 19:33
  • The function signatures (for the linker error) in the header file and the cpp don't match exactly, they need to for the linker to locate them correctly. – Niall Sep 16 '14 at 19:35
  • Okay, thanks. This is only my second C++ program. I've mostly worked in Java, Python, and Haxe. That just threw me off I suppose. I appreciate it though! – Tanner Marshall Sep 16 '14 at 19:39
  • possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Niall Sep 16 '14 at 19:40

0 Answers0