C++ Unresolved Externals because of header [duplicate] - c++

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 8 years ago.
I just started with C++ for some OpenGL applications and wanted to pack some monster functions into a util class so that my code remains clean. This is what I did:
awesomeClass.h :
#pragma once
class AwesomeClass
{
public:
static void do_something_awesome();
};
awesomeClass.cpp :
#include "awesomeClass.h"
void do_something_awesome(){
//...
}
main.cpp :
#include "awesomeClass.h"
int main(int argc, char** argv)
{
AwesomeClass::versuchen();
return 0;
}
Output:
Error 3 error LNK1120: 1 unresolved externals \Visual Studio 2013\Projects\TestEnvironmment\Debug\TestEnvironmment.exe TestEnvironmment
Error 2 error LNK2019: unresolved external symbol "public: static void __cdecl AwesomeClass::do_something_awesome(void)" (?do_something_awesome#AwesomeClass##SAXXZ) referenced in function _SDL_main \Visual Studio 2013\Projects\TestEnvironmment\TestEnvironmment\main.obj TestEnvironmment
What is wrong with that code? I mean it works when I paste everything in one file.

You should write
void AwesomeClass::do_something_awesome(){... }
Instead of
void do_something_awesome(){... }
Otherwise the function you implement does not belong to the class.

You should write this if you want write do_something_awesome in separate cpp file.
void AwesomeClass::do_something_awesome(){
//...
}
Or you can use:
#pragma once
class AwesomeClass
{
public:
static void do_something_awesome(){
//code here
}
};

Related

LNK2019 Error: unresolved external symbol

I am getting an error in Visual Studio when compiling my program.
Error LNK2019 unresolved external symbol "public: __cdecl
Grid::Grid(void)" (??0Grid##QEAA#XZ) referenced in function
main Grid C:\Users\Ryan\Desktop\Dev\Grid\Grid\main.obj 1
Error LNK2019 unresolved external symbol "public: __thiscall
Grid::~Grid(void)" (??1Grid##QAE#XZ) referenced in function
_main Grid C:\Users\Ryan\Desktop\Dev\Grid\Grid\main.obj 1
This project works fine at my university but not on my own computer and I am not sure what is wrong.
My main.cpp:
#include <iostream>
#include "Grid.h"
using namespace std;
int main(int args, char **argv)
{
Grid grid;
// grid.LoadGrid("Grid1.txt");
// grid.SaveGrid("OutGrid.txt");
system("pause");
}
And my header file:
#pragma once
class Grid
{
public:
Grid();
~Grid();
void LoadGrid(const char filename[]);
void SaveGrid(const char filename[]);
private:
int m_grid[9][9];
};
Any help at all is appreciated, thanks.
Issue resolved from advise given on [error LNK2019: unresolved external symbol "public: __thiscall : constructor issue
"First in the library project do rightclick->properties, then under the tab General, Configuration Type should be Static library (.lib)."
Thanks everyone for your answers.
As per my understanding your grid class constructor and destractor implementation are missing. You should check your .cpp file, implemention like this
Grid(){}
~ Grid(){}

Unresolved external symbol Error on public static vector [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Unresolved external symbol on static class members
(6 answers)
Closed 2 years ago.
Error message in question:
LNK2001 unresolved external symbol "public: static class std::vector<struct MakeKey::KeyStruct,class std::allocator<struct MakeKey::KeyStruct> > MakeKey::KeyArray" (?KeyArray#MakeKey##2V?$vector#UKeyStruct#MakeKey##V?$allocator#UKeyStruct#MakeKey###std###std##A)
Hello i'm kind of new to this whole coding thing and apologize if the error is stupid but I am getting a unresolved external symbol error on both of my public static vectors. I have read through multiple posts on stack overflow and can't seem to figure out what I am doing wrong. Anyway here is the minimal code that produces the same errors. Yes it is important that the vectors remain static because multiple other errors appear if it is non-static.
class MakeKey
{
public:
typedef struct KeyStruct {
sf::Image Img;
sf::Texture Tex;
sf::Sprite Sprite;
}NewKey;
static vector <MakeKey::NewKey> KeyArray;
static vector <sf::RenderWindow*> WindowArray;
static void StepWindows()
{
for (int i{ 0 }; i > MakeKey::KeyArray.size(); i++)
{
WindowArray[i]->clear(sf::Color::Transparent);
WindowArray[i]->draw(KeyArray[i].Sprite);
WindowArray[i]->display();
}
}
};
And my main
int main()
{
MakeKey::StepWindows();
}

Unresolved external calling C function from C++ [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
I have a Visual Studio solution which has to call legacy C code from C++ code and I am getting linker errors. Eg:
1>------ Build started: Project: testapp_soln, Configuration: Debug Win32 ------
1>legacy.lib(launchit.obj) : error LNK2019: unresolved external symbol "void __cdecl legacy_task(void)" (?legacy_task##YAXXZ) referenced in function "public: virtual void __thiscall Launchit::DoIt(void)" (?DoIt#Launchit##UAEXXZ)
1>E:\share\code\so\TestApp\testapp_soln\Debug\testapp_soln.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I have a static lib project called legacy with a C++ class, launchit calling the C functions in legacy C code.
Here are details of the legacy project.
legacy.h:
#ifndef LEGACY_H_
#define LEGACY_H_
extern void legacy_task (void);
extern int initialise_thing();
#endif /* LEGACY_H_ */
legacy.c
#include "legacy.h"
void legacy_task (void)
{
/* doing things */
initialise_thing();
}
legacy_init.c
#include <stdio.h>
int initialise_thing()
{
printf("Doing init thing here\n");
return 0;
}
legacy C stuff called from launchit class
launchit.hpp
#ifndef LAUNCHIT_HPP_
#define LAUNCHIT_HPP_
class Launchit
{
public:
Launchit();
virtual ~Launchit();
virtual void DoIt();
};
#endif // LAUNCHIT_HPP_
launchit.cpp
#include "Launchit.hpp"
#include "legacy.h"
Launchit::Launchit()
{
}
Launchit::~Launchit()
{
}
void Launchit::DoIt()
{
legacy_task();
}
Then I build this as a static lib. It build just fine.
I have a console application project like this:
#include "launchit.hpp"
int main() {
Launchit li;
li.DoIt();
}
But this gives me linker error:
legacy.lib(launchit.obj) : error LNK2019: unresolved external symbol "void __cdecl legacy_task(void)" (?legacy_task##YAXXZ) referenced in function "public: virtual void __thiscall Launchit::DoIt(void)" (?DoIt#Launchit##UAEXXZ)
How can I fix this linker error?
Notice that the link error says that the symbol ?legacy_task##YAXXZ was not found. This is a mingled name, derived by the C++ compiler from the function name. For compatibility with C, you need to use "extern "C" in legacy.h:
#ifdef __cplusplus
extern "C" {
#endif
void legacy_task (void);
#ifdef __cplusplus
}
#endif
This way, the compiler does not mangle the funtion's name.
For a detailed discussion of extern "C", see In C++ source, what is the effect of extern "C"?

CPP error LNK2019: unresolved external symbol cpp [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 8 years ago.
Error 1 error LNK2019: unresolved external symbol "public: void __thiscall Sounds::soundBox(void)" (?soundBox#Sounds##QAEXXZ) referenced in function _main
For some reason i get this error, and i rly dont know what i did wrong.
Got wimm.lib added playsound works when called from main()
When i try to call it from class in playsound.cpp it calls error...
playsounds.h
#pragma once
#include <Windows.h>
class Sounds
{
public:
Sounds();
~Sounds();
void soundBox();
};
playsound.cpp
#include "playsound.h"
Sounds::Sounds()
{
}
void soundBox()
{
PlaySound(TEXT("fx/boom1.wav"), NULL, SND_FILENAME);
}
Sounds::~Sounds()
{
}
main.cpp
#include <iostream>
#include <conio.h>
#include "playsound.h"
int main()
{
Sounds newsound;
newsound.soundBox();
_getch();
}
You need to change the function definition in playsound.cpp
void soundBox()
To
void Sounds::soundBox()
This is because the function exists within the scope of the Sounds class, so you have to define it as such. Otherwise it would be a free function, and the version of the function in your Sounds class would be undefined (which is what the error is telling you).

Unresolved Externals when using polymorphism

Hi i'm getting the following errors:
Error 9 error LNK1120: 2 unresolved externals
Error 8 error LNK2019: unresolved external symbol "public: virtual __thiscall physics::~physics(void)" (??1physics##UAE#XZ) referenced in function "public: virtual void * __thiscall physics::`scalar deleting destructor'(unsigned int)" (??_Gphysics##UAEPAXI#Z)
Error 7 error LNK2019: unresolved external symbol "public: virtual __thiscall student::~student(void)" (??1student##UAE#XZ) referenced in function __unwindfunclet$??0physics##QAE#XZ$0
which occur using the following code:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class student{
protected:
string fName,sName;
int id;
vector<string> cname;
vector<int> cmark;
public:
virtual ~student();
virtual void addCourse(string name, int mark)=0;
};
class physics : public student{
public:
physics(){//Initialize default values
fName="blank";
sName="blank";
id=0;
cname.push_back("none");
cmark.push_back(0);
};
~physics();
void addCourse(string name, int mark){
if(cname.size()==1){//override default value for a size of 1
cname[0]=name;
cmark[0]=mark;
}
else{
cname.push_back(name);
cmark.push_back(mark);
}
}
};
The above compiles fine but when i try to initialize an object in main() by using:
int main(){
//Database Storage
vector<student*> DB;
DB.push_back(new physics);
}
That's when i get the errors (removing the push_back line fixes it but i need this for my program). What am i doing wrong?
Turns out adding braces to the end of the destructors fixed it. What difference does that make? (from the comments)
The difference is that in one case you have a declaration which lacks a definition; in the second case you provide a (empty) definition inline.
Trying to invoke a function that is declared but not defined (as in the first case) result in an unresolved reference error raised by the linker - after all, what should it do when a function invocation is found for a function whose implementation is not available?