hey, i'm writing a piece of code and i got an error that i really don't understand. The code is below, but i had to post it all because i can't even understand the error, so i posted it all.

The Error:

1>main.obj : error LNK2019: unresolved external symbol "int __cdecl combat(void)" (?combat@@YAHXZ) referenced in function "void __cdecl North1(void)" (?North1@@YAXXZ)
1>C:\Users\Matthew\documents\visual studio 2010\Projects\Text_Game\Debug\Text_Game.exe : fatal error LNK1120: 1 unresolved externals

The Code:

#include <iostream>
#include <cmath>
#include <string>
#include <cstdlib>
#include <ctime>
#include <Windows.h>
using namespace std;
string Plrname;
int choiceint;
int choose (string);
bool dragonkilled;
bool plrhasgear;
bool plrkilled;
string Path1;

int combat();
void North1 ();
void South1 ();
void East1 ();
void West1 ();

int main () {

	system("title The Game");
	dragonkilled = 0;
	plrkilled = 0;
	cout << "Welcome to the game." << endl;
	cout << " " << endl;
	cout << "What is your name?" << endl;
	getline(cin,Plrname);
	cout << "Now " << Plrname << ", we shall begin." << endl;
	cin.ignore();
	cin.get();
	system("cls");
	cout << "You wake in a strange forest, with no memories of how you got here, but" << endl;
	cout << "you are in the middle of a crossroads, what path shall you take?" << endl;
	cout << "(North, South, East or West)" << endl;
	getline(cin,Path1);
	choose(Path1);
	if (choiceint == 1) {
		North1();
	} else if (choiceint == 2) {
		South1();
	} else if (choiceint == 3) {
		East1();
	} else if (choiceint == 4) {
		West1();
	} else {
		choose(Path1);
	}

	do {
		choose(Path1);
	} while (dragonkilled == 0, plrkilled == 0);

	if (plrkilled == 1) {
		system("cls");
		cout << "You lose!" << endl;
	} else if (dragonkilled == 1) {
		system("cls");
		cout << "You win!" << endl;
	}

	system("pause");
	return 0;
}

int choose (string choice) {
	if (choice == "north", "North") {
		choiceint = 1;
	} else if (choice == "south", "South") {
		choiceint = 2;
	} else if (choice == "east", "East") {
		choiceint = 3;
	} else if (choice == "west", "West") {
		choiceint = 4;
	} else {
		choiceint = 5;
	}
	return (choiceint);
}

void North1 () {
	if (plrhasgear == 1) {
		cout << "You decide to travel north, and come across a monster!" << endl;
		combat();
		system("cls");
		cout << "Where do you want to go now?" << endl;
		getline(cin,Path1);
	} else {
		cout << "You found some gear!" << endl;
		plrhasgear = 1;
		cin.ignore();
		cin.get();
		cout << "Where do you want to go now?" << endl;
		getline(cin,Path1);
	}
}

void South1 () {
	cout << "You decide to travel south, and come across a monster!" << endl;
	combat();
	system("cls");
	cout << "Where do you want to go now?" << endl;
	getline(cin,Path1);
}

void East1 () {
	cout << "You decide to travel south, and come across a monster!" << endl;
	combat();
	system("cls");
	cout << "Where do you want to go now?" << endl;
	getline(cin,Path1);
}

void West1 () {
	cout << "You decide to travel west, and come across a monster!" << endl;
	combat();
	system("cls");
	cout << "Where do you want to go now?" << endl;
	getline(cin,Path1);
}

int Combat () {
	system ("cls");
	int monsterno;
	int monsterlvl;
	double monsterhp;
	double monsterdmg;
	int monstercrit;
	double plrhp;
	double plrdmg;

	plrhp = 10;
	plrdmg = 5;
	if (plrhasgear == 1) {
		plrhp = plrhp*2.5;
		plrdmg = plrdmg*2;
	}

	srand( int( time( NULL ) ) );

	monsterno = rand() % 4;
	monstercrit = rand() % 5;

	if (monsterno == 0) {
		cout << "You have encountered a panther!" << endl;
		monsterhp = 20;
		monsterlvl = 1;
		monsterdmg = 2;
		if (monstercrit == 0) {
			monsterdmg = monsterdmg * 1.5;
		}
	} else if (monsterno == 1) {
		cout << "You have encountered a troll!" << endl;
		monsterhp = 25;
		monsterlvl = 2;
		monsterdmg = 3;
		if (monstercrit == 0) {
			monsterdmg = monsterdmg * 1.5;
		}
	} else if (monsterno == 2) {
		cout << "You have encountered a necromancer!" << endl;
		monsterhp = 30;
		monsterlvl = 3;
		monsterdmg = 5;
		if (monstercrit == 0,1) {
			monsterdmg = monsterdmg * 1.5;
		}
	} else if (monsterno == 3) {
		cout << "You have encountered the dragon, good luck!" << endl;
		monsterhp = 40;
		monsterlvl = 5;
		monsterdmg = 10;
		if (monstercrit == 0,1,2) {
			monsterdmg = monsterdmg * 2;
		}
		if (monsterhp == 0) {
			dragonkilled = 1;
		}
	}

	do {
		cout << "You have " << plrhp << " health remaining." << endl;
		cout << "The monster has " << monsterhp << " health remaining." << endl;
		cin.ignore();
		cin.get();
		system("cls");
		cout << "Monster hits you for " << monsterdmg << " damage!" << endl;
		plrhp = plrhp-monsterdmg;
		cin.ignore();
		cin.get();
		cout << Plrname << " hits the monster for " << plrdmg << "damage!" << endl;
		cin.ignore();
		cin.get();
	} while (plrhp != 0, monsterhp != 0);
	if (plrhp == 0) {
		plrkilled = 1;
	}
	return (plrkilled);
}

sorry that the code is so long, as i said, i'm not sure were i've gone wrong so i posted it all.

Dani AI

Generated

Good catch — that is the direct cause of the linker error shown in the original post. LNK2019 means the compiler produced code that references a symbol but the linker could not find a matching definition. In C++ the function name and signature (including case) are part of the symbol the linker looks for, so a declaration and an implementation that differ even by letter case will fail to link.

Quick checklist to diagnose LNK2019 in Visual Studio:

  • Make sure the function declaration and the definition match exactly: same name, same parameter types, same calling convention (and same case).
  • Verify the .cpp file that contains the definition is included in the project and not excluded from the build (Solution Explorer → file properties).
  • Clean and then Rebuild Solution so stale .obj files aren’t masking the change.
  • If the symbol comes from a library, confirm the .lib is added to Linker → Input and the Library Directories are correct.
  • For advanced inspection, view the symbol names in object/library files (dumpbin /symbols) to see how the linker is naming/mangling things.

For : after fixing the name-case mismatch, a few other issues in the posted code will still cause bugs at runtime. Initialize boolean globals (e.g., set plrHasGear = false). Avoid the comma operator in conditions — use logical operators instead. Seed the RNG once (in main), not every combat call. Prefer comparisons that handle negatives (use <= 0 for health checks) and loop conditions like while (plrHp > 0 && monsterHp > 0). Example patterns:

bool plrHasGear = false;
if (choice == "north" || choice == "North") { ... }
while (plrHp > 0 && monsterHp > 0) { ... }
srand(unsigned(time(NULL))); // once in main

Once declaration/definition names match and the implementation file is actually compiled into the project, the specific LNK2019 you saw will disappear.

Recommended Answers

All 2 Replies

Case sensitivity!

your declared function

int combat();

does not match your implemented

int Combat () {

function names must match in correct case

commented: Thanks for your help :) +1

god, is my face red :P thanks for pointing that out i can't believe i didn't notice that before :D

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.