C++ lnk error 2019 unresolved external symbol virtual errors because of an interface...(?) - c++

Im having problems with making a good interface and use it...
My setup overview:
An "interface" GraphicsLibrary.H...
virtual void drawPoint(const Point& p, unsigned char r, unsigned char g, unsigned char b, double pointSize);
with an "empty" GraphicsLibrary.ccp! because its an interface, so "OpenGL" is an graphics library... so i have an OpenGL.CPP with:
void GraphicsLibrary::drawPoint(const Point& p, unsigned char r, unsigned char g, unsigned char b, double pointSize)
{
//some code
}
which has ofcourse an "empty" OpenGL.h (since his header file is the GraphicsLibrary.h)
then i have a class with more specific functions that uses OpenGL, and uses those base drawing functions... (OpenGLVis_Enviroment.cpp):
OpenGL ogl;
void drawObstacleUnderConstruction(Obstacle::Type type, const vector<Point>& points)
{
for( //etcetc )
ogl.drawPoint(*it, 255, 255, 255, 3.0);
}
BUT i also have a main that uses some OpenGL functions...
so the main has also:
OpenGL openGL;
openGL.drawText(something);
but now i have a lot of those errors (i have the same with all the other functions):
1>OpenGLVis_Environment.obj : error LNK2019: unresolved external symbol "public: virtual void __thiscall GraphicsLibrary::drawPoint(struct Point const &,unsigned char,unsigned char,unsigned char,double)" (?drawPoint#GraphicsLibrary##UAEXABUPoint##EEEN#Z) referenced in function "void __cdecl DrawingFunctions::drawObstacleUnderConstruction(enum Obstacle::Type,class std::vector<struct Point,class std::allocator<struct Point> > const &)" (?drawObstacleUnderConstruction#DrawingFunctions##YAXW4Type#Obstacle##ABV?$vector#UPoint##V?$allocator#UPoint###std###std###Z)
Is this because i use "GraphicsLibrary::drawPoint..." ? I am searching online for ages, but it's hard to find a lot of examples about interfaces.. and how to work with them...
Thanks in advance guys

The linker complains about DrawingFunctions::drawObstacleUnderConstruction and you defined void drawObstacleUnderConstruction, which is a free function.
Qualify the name when you define the function.
void DrawingFunctions::drawObstacleUnderConstruction(Obstacle::Type type, const vector<Point>& points)
{
for( //etcetc )
ogl.drawPoint(*it, 255, 255, 255, 3.0);
}

Related

Linker Error "unresolved external symbol" when using constructor of other class

I've been struggling to find why my linker gets an unresolved external symbol error. The error looks like this:
Error
LNK2019
unresolved external symbol "public: __thiscall Shader::Shader(char const *)" (??0Shader##QAE#PBD#Z) referenced in function "public: __thiscall GridWorldGPGPU::GridWorldGPGPU(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,unsigned int)" (??0GridWorldGPGPU##QAE#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##I#Z)
DV2556_Project
grid_world_GPGPU.obj
1
As far as I understand it it has something to do with that my linker finds the declaration of the Shader::Shader(char const *)-function but cannot find the definition. I have been staring at this for hours and cannot figure out why the linker becomes sad.
grid_world_GPGPU.h:
#ifndef GRID_WORLD_GPGPU_H
#define GRID_WORLD_GPGPU_H
#include "grid_world.h"
#include <../swift_open_gl.h>
class GridWorldGPGPU : public GridWorld {
private:
Shader* compute_shader_ = nullptr;
public:
GridWorldGPGPU(std::string in_path_shader, unsigned int in_side = 1);
};
#endif // !GRID_WORLD_GPGPU_H
grid_world_GPGPU.cpp:
GridWorldGPGPU::GridWorldGPGPU(std::string in_path_shader, unsigned int in_side)
: GridWorld(in_side) {
this->compute_shader_ = new Shader(in_path_shader.c_str());
}
The Shader-class is defined in the swift_open_gl.h file:
#ifndef SWIFT_OPEN_GL_H
#define SWIFT_OPEN_GL_H
#include <glad/glad.h>
#include <GLFW/glfw3.h>
class Shader {
public:
Shader(const char* cs_path);
};
#endif // !SWIFT_OPEN_GL_H
And swift_open_gl.cpp has this:
#include "..\swift_open_gl.h"
inline Shader::Shader(const char * cs_path) {
//Do stuff
}
I've tried with and without the inline (Visual Studio added it when I tried moving the function definition between the .h-file and a .cpp-file, so I decided to try it) and i have verified that the #include <../swift_open_gl.h> doesn't occur anywhere else in the project than the files listed above.
An extra set of eyes to look over this would be appreciated!
Provide default constructor of the class as well.
class Shader {
public:
Shader(){} // default constructor
Shader(const char* cs_path);
};
Dont use inline in Shader::Shader(const char* cs_path){} definition

c++ optional/default argument

I have defined a method with an optional/defaulted last argument called noAutoResolve as follows:
typedef std::shared_ptr<IMessage> TMessagePtr;
class NetworkService : public IConnectionManagerDelegate, public net::IStreamDelegate
{
public:
void send_message(std::string identity, msg::TMessagePtr msg, QObject* window, std::function<void(int, std::shared_ptr<msg::IMessage> msg)> fn, bool noAutoResolve = false);
}
and later:
void NetworkService::send_message(std::string endpoint, msg::TMessagePtr msg, QObject* window, std::function<void(int res, std::shared_ptr<msg::IMessage> msg)> fn, bool noAutoResolve)
{
}
The linker is now unhappy about unresolved externals in the following line where I intentionally omitted the last argument:
service_->send_message(endpoint_, msg, this, [this](int result, msg::TMessagePtr msg){
// .....
});
Is that not possible in c++?
Error LNK1120 1 unresolved externals QTServer QTServer.exe 1
Error LNK2019 unresolved external symbol "public: void __thiscall NetworkService::send_message(class std::basic_string,class std::allocator >,class std::shared_ptr,class QObject *,class std::function)>)" (?send_message#NetworkService##QAEXV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##V?$shared_ptr#UIMessage#msg###3#PAVQObject##V?$function#$$A6AXHV?$shared_ptr#UIMessage#msg###std###Z#3##Z) referenced in function "public: void __thiscall QTWindow::ExecuteCommand(void)" (?ExecuteCommand#QTWindow##QAEXXZ) QTServer QTWindow.obj 1
The fn parameter of your function is type of std::function<void(int, std::shared_ptr<msg::IMessage> msg)>. However, the lambda you are passing is:
[this](int result, msg::TMessagePtr msg){
// .....
}
This function has the signature of void(int, msg::TMessagePtr), so if there is no conversion from std::shared_ptr<msg::IMessage> to msg::TMessagePtr, the code cannot compile.
Your problem is therefore not about the optional parameter. For a quick fix, if you have access to a C++14 compiler, try getting the lambda parameters as auto:
[this](auto result, auto msg){
// .....
}

Linker error (LNK2019) for two classes located in a single file

I have two classes located in ExtractMaximalStableRegion.h. They are
class ExtractM_StableRegion
{
public:
//! the full constructor
ExtractM_StableRegion(int _delta = 5, int _min_area = 60, int _max_area = 14400,
double _max_variation = 0.25, double _min_diversity = .2,
int _max_evolution = 200, double _area_threshold = 1.01,
double _min_margin = 0.003, int _edge_blur_size = 5);
//! the operator that extracts the MSERs from the image or the specific part of it
void operator()(const Mat& image, CV_OUT vector<vector<Point> >& msers, const Mat& mask = Mat());
//AlgorithmInfo* info() const;
protected:
int delta;
int minArea;
int maxArea;
double maxVariation;
double minDiversity;
int maxEvolution;
double areaThreshold;
double minMargin;
int edgeBlurSize;
};
Another one is
class ExtractM_StableRegion_ParallelProcess{
private:
vector<pair<Mat, Mat>> objs;
vector<vector<Point>> msers;
public:
~ExtractM_StableRegion_ParallelProcess(){
if (!objs.empty())
{
objs.clear();
vector<pair<Mat, Mat>>().swap(objs);//check these Mat is released
}
if (!objs.empty())
{
msers.clear();
vector<vector<Point> >().swap(msers);
}
}
void operator()(const tbb::blocked_range<tbb::size_t>& r) {
for (tbb::size_t i = r.begin(); i != r.end(); ++i)
{
ExtractM_StableRegion()(objs[i].first, msers, objs[i].second);
}
}
ExtractM_StableRegion_ParallelProcess(vector<pair<Mat, Mat>> objs_) : objs(objs_){}
ExtractM_StableRegion_ParallelProcess(ExtractM_StableRegion_ParallelProcess& x, tbb::split) : objs(x.objs), msers(x.msers){}
void join(ExtractM_StableRegion_ParallelProcess& y)
{
for (vector<vector<Point> >::iterator it = y.msers.begin(); it != y.msers.end(); ++it)
msers.push_back(*it);
vector< vector<Point> > local;//check local is released
ExtractM_StableRegion()(objs[0].first, local, objs[0].second);
}
void compareWithSingleThread(){
for (int i = 0; i < objs.size(); ++i)
{
vector< vector<Point> > local;//check local is released
ExtractM_StableRegion()(objs[i].first, local, objs[i].second);
for (vector<vector<Point> >::iterator it = local.begin(); it != local.end(); ++it)
msers.push_back(*it);
}
return;
}
vector<vector<Point> > getMSER_Regions(){
return msers;
}
};
ExtractM_StableRegion_ParallelProcess is to speed up using TBB for ExtractM_StableRegion class.
Then I have another cpp file counting.cpp and run ExtractM_StableRegion_ParallelProcess class as
ExtractM_StableRegion_ParallelProcess *ep = new ExtractM_StableRegion_ParallelProcess(objs);
//tbb::parallel_reduce(blocked_range<size_t>(0, numObj), *ep, auto_partitioner());//simple_partitioner()
ep->compareWithSingleThread();
When I build, I have linker error that I don't understand as
Counting.obj : error LNK2019: unresolved external symbol "public: __cdecl VIDEOANALYTICS_PLATFORM::ExtractM_StableRegion::ExtractM_StableRegion(int,int,int,double,double,int,double,double,int)" (??0ExtractM_StableRegion#VIDEOANALYTICS_PLATFORM##QEAA#HHHNNHNNH#Z) referenced in function "public: void __cdecl VIDEOANALYTICS_PLATFORM::ExtractM_StableRegion_ParallelProcess::compareWithSingleThread(void)" (?compareWithSingleThread#ExtractM_StableRegion_ParallelProcess#VIDEOANALYTICS_PLATFORM##QEAAXXZ)
1>Counting.obj : error LNK2019: unresolved external symbol "public: void __cdecl VIDEOANALYTICS_PLATFORM::ExtractM_StableRegion::operator()(class cv::Mat const &,class std::vector<class std::vector<class cv::Point_<int>,class std::allocator<class cv::Point_<int> > >,class std::allocator<class std::vector<class cv::Point_<int>,class std::allocator<class cv::Point_<int> > > > > &,class cv::Mat const &)" (??RExtractM_StableRegion#VIDEOANALYTICS_PLATFORM##QEAAXAEBVMat#cv##AEAV?$vector#V?$vector#V?$Point_#H#cv##V?$allocator#V?$Point_#H#cv###std###std##V?$allocator#V?$vector#V?$Point_#H#cv##V?$allocator#V?$Point_#H#cv###std###std###2##std##0#Z) referenced in function "public: void __cdecl VIDEOANALYTICS_PLATFORM::ExtractM_StableRegion_ParallelProcess::compareWithSingleThread(void)" (?compareWithSingleThread#ExtractM_StableRegion_ParallelProcess#VIDEOANALYTICS_PLATFORM##QEAAXXZ)
The linker error is happening at method void compareWithSingleThread() for using ExtractM_StableRegion()(objs[i].first, local, objs[i].second);
I also have object ExtractM_StableRegion()(objs[i].first, local, objs[i].second);used in void operator()(const tbb::blocked_range<tbb::size_t>& r) method. Why the one inside compareWithSingleThread() has error and inside operator() doesn't have error.
Thanks
A linker error means that the compiler had terminated without errors but the linker is unable to find the definition (of a function, method, or symbol) for some declaration . The problem is that sometimes you have to guess where this comes from.
In your case I'd do the following: Reduce your example. Comment out every (member) variable and method body except for those mentioned in the error message. In your original post above remove the appropriate lines. This makes it easier for SO people to understand what is going wrong. It may even lead you to the solution.
Move the method bodies from the .cpp file to the .h file. This might give you a hint on what's going wrong. Or rebuild the .cpp file.
Check the respective project properties or makefile or whatever. Somewhere it has to be specified what is linked where and to what and how. This is maybe incomplete.
You may find other hints here at Stackoverflow because there are many, many questions about linker errors, e.g. this one.

Error while instantiating an object without inheritance

So far I was using classes inheriting from other classes, now I had the need to creating a class inheriting from anything. I called it from my client class and I'm getting error that doesn't make sense to me. what am I doing wrong?
MathHelp .h
public:
float addPerc(float whole, float perc);
float subPerc(float whole, float perc);
MathHelp .cpp
float addPerc(float whole, float perc)
{
return 0;
}
float subPerc(float whole, float perc)
{
return 0;
}
calling from client
MathHelp* mathHelp = new MathHelp();
float mathResult = mathHelp->addPerc(100,5);
Error:
error LNK2019: unresolved external symbol "public: float __thiscall MathHelp::addPerc(float,float)" (?addPerc#MathHelp##QAEMMM#Z)
referenced in function "public: virtual void __thiscall EnergyManager::draw(class cocos2d::Renderer *,class cocos2d::Mat4 const &,unsigned int)" (?draw#EnergyManager##UAEXPAVRenderer#cocos2d##ABVMat4#3#I#Z)
Method declarations need to also have the name of the class when you're declaring them outside of the class definition.
float MathHelp::addPerc(float whole, float perc)
{
return 0;
}
float MathHelp::subPerc(float whole, float perc)
{
return 0;
}
With the code that has been provided, if they are taken as they directly appear in the files, you are missing the scope for the methods of the class MathHelp, you'd instead want to try something like this:
float MathHelp::addPerc(float whole, float perc)
{
return 0;
}
float MathHelp::subPerc(float whole, float perc)
{
return 0;
}

Constant reference parameter causing unresolved external symbol

Below is a simplified version of some code I wrote. this code works fine so far
class.h
namespace myNamespace
{
class myClass
{
public:
myClass(unsigned width, unsigned height);
myClass(OtherClass& other, unsigned width, unsigned height);
~myClass(){};
private:
unsigned width;
unsigned height;
};
}
class.cpp
#include "class.h"
namespace myNamespace
{
myClass::myClass(unsigned width, unsigned height)
{
//code
}
myClass::myClass(OtherClass& other, unsigned width, unsigned height) : myClass(width, height)
{
//code
}
}
(OtherClass is defined elsewhere inside the myNamespace and is included)
the constructor that uses OtherClass doesn't alter other so making it const would be appropriate.
But when I change the constructor in both the .cpp and .h to use const OtherClass& it gives me the error:
error LNK2019: unresolved external symbol "public: __thiscall
myNamespace::myClass::myClass(class myNamespace::OtherClass &,unsigned
int,unsigned int)"
(??0CarbonMatrix#molecule##QAE#AAVCarbonString#1#II#Z) referenced in
function _wmain path\main.obj
As far as I know const shouldn't cause this error as long you use them both at the declaration and at the definition.
So my question: What goes wrong and how to fix it?
That error indicates, that you have actually not changed the declaration in the header file or did not recompile all source files after changing it.
Double check that you indeed changed it to const OtherClass& in the header file. Then re-compile the whole project, i.e. not only class.cpp, but also main.cpp.