error LNK2019: unresolved external symbol newbie needs tip - c++

I am just probing some more advanced coding in C++, so imagine my frustration when I can't solve this:
1>Main.obj : error LNK2019: unresolved external symbol "int __cdecl playerAtk(void)" (? playerAtk##YAHXZ) referenced in function _main
1>C:\Users\Hezekiah Detinc\documents\visual studio 2010\Projects\Custom_1\Debug\Custom_1.exe : fatal error LNK1120: 1 unresolved externals
I had tried searching for an answer here, but the solutions I have found were too specific to be of any use for me.
If anyone can answer; What generally causes this problem? What are some solutions to fixing it?
This is my Main.cpp;
//Custom 1
//Include files that may be used. will cut out unused files in release.
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include "EnemyBaseClass.h"
#include "PlayerBaseClass.h"
using namespace std;
int main()
{
Enemy emy;
Player plr;
emy.enemyAtk();
emy.enemyDef();
emy.enemyHp();
plr.playerAtk();
plr.playerDef();
plr.playerHp();
int playerAtk();
cout << playerAtk();
cout << "You're attacked by a Monster!\n";
system(" pause ");
return 0;
}
If I need to post my headers or the other _.cpp files, let me know.

You declare and use playerAtk as a function (with no implementation, thus the undefined reference)
Change
int playerAtk();
...
cout << playerAtk();
to
int playerAtk;
...
cout << playerAtk;
(You probably want to initialize the variable)

The error is simple: You are using a function which you have declared, but you have not written any definition (Implementation). Check your source (.cpp) files.

Related

Error LNK2019 while implementing a constructor

I am having some trouble with the implementation of a constructor and I cannot figure out what is the problem. I have spent a lot of time with this problem: it would be grateful some kind of advice.
The main code is:
#include "stdafx.h"
#include "Rocket.h"
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
Rocket* rocket;
rocket=new Rocket();
//LLA* position=rocket->positionLLA;
return 0;
}
Rocket.h
#pragma once
#include "LLA.h"
class Rocket {
public:
Rocket(); // Default constructor
LLA* positionLLA;
};
Rocket.cpp
#include "stdafx.h"
#include "Rocket.h"
#include <iostream>
using namespace std;
Rocket::Rocket() // Default constructor
{
// Initialise the position
positionLLA=new LLA();
}
The error says:
error LNK2019: unresolved external symbol "public: __thiscall Rocket::Rocket(void)" (??0Rocket##QAE#XZ) referenced in function _main.
I know the error has something to do with not having declared a variable, but I think I have declared all classes and constructors.
PS: I am using Visual Studio 2008 in order to add dependencies.
I am assuming LLA is correctly defined in your .h file
Are you compiling Rocket.cpp into Rocket.o and main.cpp into main.o and then linking the two object files together?
Your error seems to imply that the linker cannot obtain symbol information from Rocket.o, which usually means Rocket.o was not found
Oh, as a detail, since Rocket is using an LLA*, you don't need to include LLA.h in Rocket.h, you can simply forward-declare
class LLA;
You might want to add #ifndef, #define, and #endif to your header files. Having multiple includes of the same thing in different files can lead to complications.

error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup and Error 2 error LNK1120: 1 unresolved externals

I am getting the two errors when I try to compile my code. I have defined win32 consile application. I have not even yet started coding. My configurations are the following - Linker - subsytem - Console; Linker - Advanced - Entry Point - Blank.
error LNK2019: unresolved external symbol _main referenced in function
___tmainCRTStartup
Error 2 error LNK1120: 1 unresolved externals
I don't know what happened. Before everything was working fine but from today it does not. Do you have any idea how I can resolve it that?
My code so far is
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "tbb/blocked_range.h"
#include "tbb/tbb.h"
#include <stdio.h>
#include <math.h>
#include <iostream>
#include "tbb/parallel_for.h"
#include <sstream>
#include <tbb/task_scheduler_init.h>
using namespace std;
#define PI 3.14159265
using namespace tbb;
// Sequential Execution
class Sequential
{
double * m;
double *n;
public:
Sequential(double n[], double m[]): n(n), m(m){}
void Partition()
{
}
int main(int argc, char** argv)
{
//double a = 5;
//double b = 4;
//Sequential g = Sequential(a,b);
return 0;
}
};
Thanks
The main function must be in the global namespace. C++ is not C#/Java where all functions must be inside classes.

VS 2012 Error LNK2019

I'm here so I can find a solution for my problem. I know this is too simple, but somehow I can't figure out where's the error in my code!
Here you have it:
AulaData.h
#ifndef AULADATA_H_
#define AULADATA_H_
#include <string>
using std::string;
class AulaData
{
private:
int dia;
public:
AulaData(int dia);
};
#endif
AulaData.cpp
#include "AulaData.h"
AulaData::AulaData(int dia)
{
}
And finally, my Main.cpp:
#include <vector>
#include "AulaData.h"
using namespace std;
int main(int argc, char* argv[])
{
AulaData a(12);
getchar();
return 0;
}
The error I'm getting is the following(something that never happened to me):
1>ConsoleApplication1.obj : error LNK2019: unresolved external symbol
"public: __thiscall AulaData::AulaData(int)" (??0AulaData##QAE#XZ)
referenced in function _main
Although if I define the class constructor without arguments, it'll work.
I'd appreciate a lot if someone could help me! :) I'm trully getting frustrated because all seems ok.
Thanks in advance!
I think that you must provide a default constructor as well if you create one with arguments.

error LNK2019: unresolved external symbol (simple code)

this is a pretty simple code that just is coming up with an error even though I have it written the same way other people doing the same code have it
1>assigntment5.obj : error LNK2019: unresolved external symbol "class std::basic_string,class std::allocator > __cdecl promptForString(class std::basic_string,class std::allocator >)" (?promptForString##YA?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##V12##Z) referenced in function _main
1>c:\users\aweb\documents\visual studio 2010\Projects\Assignment5\Debug\Assignment5.exe : fatal error LNK1120: 1 unresolved externals
the .cpp file
#include <iostream>
#include <string>
#include "anw65_Library.h"
using namespace std;
string promptForString(string prompt);
int main()
{
string name = promptForString("What is the filename?: ");
system("pause");
return 0;
}
the .h file
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
static string promptFromString(string prompt)
{
string filename;
cout << prompt;
cin >> filename;
return filename;
}
You never define prompt**For**String, you defined prompt**From**String. Spelling matters. Also:
Why are you defining functions in your .h file? Just declare them there and define them in the .cpp file (unless they're templates).
Don't put using namespace <whatever> in a header file. You're just mucking up the global namespace of whatever includes your header.
You don't need to mark that function as static.
This line:
string promptForString(string prompt);
In your .cpp file is causing issues. It's forward delcaring a function with external linkage. However, the function your header is:
static string promptFromString(string prompt)
{
...
The important part here is the static. static means it has internal linkage. Either get rid of the static, or get rid of the forward declaration, because a function can't have both internal and external linkage.
Edit: also, Ed S. made a good find with your typo.
you call promptForString() from your main function while you have promptFromString() defined in .h file.
You might want to change one of the definitions.

Unresolved external symbol in c++

#include <iostream>
#include <cstdlib>
using std::cout;
using std::endl;
using std::rand;
int man(){
int t=rand();
cout<<t<<endl;
return 0;
}
here is code for generate random number in c++ and i have mistake
1>c:\users\david\documents\visual studio 2010\Projects\random_function\Debug\random_function.exe : fatal error LNK1120: 1 unresolved externals
please help
Change man() to main().
Me, I think I'd change "man" to "main" and see what happens...