LNK2019: unresolved external symbol but I have coded the function - c++

Can somebody help me with this? I'm using Visual Studio 2010
I'm getting this message and I don't know how to solve this.
1> Generating Code...
1>dct.obj : error LNK2019: unresolved external symbol "public:
__thiscall Amostras::Amostras(class std::basic_string,class std::allocator >)"
(??0Amostras##QAE#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z)
referenced in function _main
1>C:\Users\redneck\documents\visual studio
2010\Projects\dct\Debug\dct.exe : fatal error LNK1120: 1 unresolved
externals
Here is some of the *.cpp file:
class Amostras {
public:
int original[10][257];
int idct[10][257];
float dct[10][257];
int grupos;
Amostras::Amostras(void)
{
for (int i=0;i<10;i++)
{
this->original[i][0]=0;
this->dct[i][0]=0.0;
this->idct[i][0]=0;
}
this->grupos=0;
}
Amostras::Amostras(string arquivo)
{
int n_samples=0,linha=0,coluna=0;
int cont;
..
and here is the *.h
class Amostras {
public:
int original[10][257];
int idct[10][257];
float dct[10][257];
int grupos;
Amostras::Amostras();
Amostras::Amostras(string arquivo);
void Amostras::mostra(void);
};
main
int main(void)
{
Amostras *amostra = new Amostras("in.txt");
dct(amostra,0);
show(amostra,0);
amostra->mostra();
return 0;
}
hope it helps, i'm running out of options here :(
Solution:
So what I did was just putting the class just in *.h and then including the *.h in the class *.cpp that only has the methods and functions of that class. It worked!

This linker error usually means that you have prototyped a function but forgotten to define it. Make sure that you have implemented the function
Amostras::Amostras(string arg);
somewhere, and that when you were linking your code that the object file containing that implementation was linked in.
Hope this helps!

you forgot to define the Amostras::Amostras(string arg);
though declared in your *.h file
Amostras::Amostras(string arg)
{
}
copy the above code in your *.cpp file
OR
you can also do this by commenting the line from your *.h file.
//Amostras::Amostras(string arg);
whoa! do you have a *.h file ? if you are working in only *.cpp then lemme know.

Related

How to fix C++ linker error with no explaination

I have a C++ project that won't compile, and the following 2 errors are produced:
Error LNK1120 1 unresolved externals
Error LNK2019 unresolved external symbol "public: virtual __cdecl
StartWindow::~StartWindow(void)" (??1StartWindow##UEAA#XZ) referenced
in function "public: void __cdecl StartWindow::`vbase
destructor'(void)" (??_DStartWindow##QEAAXXZ)
StartWindow is a class I have defined, but currently it is never instantiated or included anywhere in the project. Deleting the class allows the project to compile, but if this class is within the project it won't.
I will include the code for the class in case I am missing something:
.CPP File
#include "StartWindow.h"
StartWindow::StartWindow()
{
setImmediateDrawMode(false);
}
void StartWindow::onDraw()
{
clearScreen(WHITE);
EasyGraphics::onDraw();
}
Header File:
#pragma once
#include "EasyGraphics.h"
class StartWindow : public EasyGraphics
{
public:
StartWindow();
~StartWindow();
private:
virtual void onDraw();
};
Thanks.
You're missing the implementation for the destructor for StartWindow. In your implementation file (.cpp file), append:
StartWindow::~StartWindow(){
//if your destructor is non-trivial, include definition here
}

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(){}

LNK2019 Error when compiling .cpp

First, this is hw and I'm supposed to change only one function. But I'm not here for help on that function, I'm here for an LNK error.
Here's the relevant output on the Developer Command Prompt after I type in cl shortest.cpp (with spaces between errors for easier reading):
shortest.obj : error LNK2019: unresolved external symbol "public: void __thiscall Graph::readFile(char *)" (?readFile#Graph##QAEXPAD#Z) referenced in function _main
shortest.obj : error LNK2019: unresolved external symbol "public: void __thiscall Graph::dijkstra(int)" (?dijkstra#Graph##QAEXH#Z) referenced in function _main
shortest.obj : error LNK2019: unresolved external symbol "public: void __thiscall Graph::printPath(int)" (?printPath#Graph##QAEXH#Z) referenced in function _main
shortest.exe : fatal error LNK1120: 3 unresolved externals
I understand this is supposed to mean I haven't defined those symbols, but they should have been predefined for me in one of two files: graph.cpp or graph.h. Graph.h seems more likely, as it contains the declarations of each of these "unresolved" symbols.
graph.h:
#include <iostream>
#include <fstream>
#include <vector>
#include <limits.h>
#include <cstdlib>
#define INFINITY INT_MAX
using namespace std;
class Graph
{
struct Edge
{
int dest; // destination vertex number
int cost; // edge weight
Edge *next;
};
struct Vertex
{
Edge *adj;
bool known;
int dist;
int path;
};
vector<Vertex *> vertices;
vector<int> PQ;
int PQsize;
void PQinsert (int v);
int PQdeleteMin ();
public:
Graph () { PQsize=0; }
int numVertices () { return vertices.size(); }
int dist (int dest) { return vertices[dest]->dist; }
void readFile (char *name);
void dijkstra (int start);
void printPath (int dest);
};
I won't post graph.cpp or shortest.cpp (which only contains the main), unless requested, as it doesn't seem to me that the content of the related functions is an issue. If I do, I will only post the related methods to keep it shorter. But graph.cpp, graph.h, and shortest.cpp are all in the same folder. And shortest.cpp does include graph.h. I'm only supposed to change the dijkstra method, but if I can add something that will make this compile without changing way the methods or class themselves work (which it shouldn't have to) I don't see a problem.
You have to use make file to build with all the source files. Otherwise you can first compile the sources and then link manually. Search google for individual commands for compiling/linking.

How to solve error LNK2019: unresolved external symbol error in Visual Studio 2012 with C++?

I have a visual c++ console project named "C_test".
Another project is called "dll_project" which is dll project.
in "C_test" project, I have set the location of additional include directory for "dll_project".
I have these simple codes.
in "C_test" project's main.cpp.
#include "SampleClass.h"
int main()
{
SampleClass *_parser = new SampleClass();
return 0;
}
And in "dll_project" SampleClass.h and SampleClass.cpp.
SampleClass.h
#pragma once
class SampleClass
{
public:
SampleClass(void);
~SampleClass(void);
};
SampleClass.cpp
#include "SampleClass.h"
SampleClass::SampleClass(void)
{
}
SampleClass::~SampleClass(void)
{
}
When I try to debug or build, I get this error.
error LNK2019: unresolved external symbol "public: __thiscall SampleClass::SampleClass(void)" (??0SampleClass##QAE#XZ) referenced in function _main C:\C_test\main.obj C_test
What have I done wrong?

C++ LNK2019 error with constructors and destructors in derived classes

I have two classes, one inherited from the other. When I compile, I get the following errors:
Entity.obj : error LNK2019: unresolved external symbol "public: __thiscall Utility::Parsables::Base::Base(void)" (??0Base#Parsables#Utility##QAE#XZ) referenced in function "public: __thiscall Utility::Parsables::Entity::Entity(void)" (??0Entity#Parsables#Utility##QAE#XZ)
Entity.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall Utility::Parsables::Base::~Base(void)" (??1Base#Parsables#Utility##UAE#XZ) referenced in function "public: virtual __thiscall Utility::Parsables::Entity::~Entity(void)" (??1Entity#Parsables#Utility##UAE#XZ)
D:\Programming\Projects\Caffeine\Debug\Caffeine.exe : fatal error LNK1120: 2 unresolved externals
I really can't figure out what's going on.. can anyone see what I'm doing wrong? I'm using Visual C++ Express 2008. Here are the files..
"include/Utility/Parsables/Base.hpp"
#ifndef CAFFEINE_UTILITY_PARSABLES_BASE_HPP
#define CAFFEINE_UTILITY_PARSABLES_BASE_HPP
namespace Utility
{
namespace Parsables
{
class Base
{
public:
Base( void );
virtual ~Base( void );
};
}
}
#endif //CAFFEINE_UTILITY_PARSABLES_BASE_HPP
"src/Utility/Parsables/Base.cpp"
#include "Utility/Parsables/Base.hpp"
namespace Utility
{
namespace Parsables
{
Base::Base( void )
{
}
Base::~Base( void )
{
}
}
}
"include/Utility/Parsables/Entity.hpp"
#ifndef CAFFEINE_UTILITY_PARSABLES_ENTITY_HPP
#define CAFFEINE_UTILITY_PARSABLES_ENTITY_HPP
#include "Utility/Parsables/Base.hpp"
namespace Utility
{
namespace Parsables
{
class Entity : public Base
{
public:
Entity( void );
virtual ~Entity( void );
};
}
}
#endif //CAFFEINE_UTILITY_PARSABLES_ENTITY_HPP
"src/Utility/Parsables/Entity.cpp"
#include "Utility/Parsables/Entity.hpp"
namespace Utility
{
namespace Parsables
{
Entity::Entity( void )
{
}
Entity::~Entity( void )
{
}
}
}
The relevant bit is this:
unresolved external symbol "public: __thiscall Utility::Parsables::Base::Base(void)"
You need to provide a definition for Base::Base and Base::~Base. A declaration is not good enough. Even if you have nothing to do in either function, you need to leave an empty function body, because C++ actually requires the function to exist. C++ puts things like virtual table maintenance inside your constructors and destructors, so they must be defined even if you don't need to do anything there -- C++ has to do things in there.
Are you sure Base.cpp is being included in the build?
Just encountered this exact same error today in Visual Studio 2015. Unfortunately the accepted answer didn't worked (as well as answers from many same questions). The thing that worked for me was right click on the base class cpp file, exclude and then include it again. I think somehow VS got confused while moving file around and renames and it just silently refused to compile it even though it was marked as "Included In project" = true in property editor as well as listed in vcproj file in group. This is horrible error and ended up spending good hour on it.
Either your base.cpp is not being compiled/linked or you have a misspelling in it