Error while instantiating an object without inheritance - c++

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;
}

Related

How can I pass the value from void function in one cpp file to an other void function in an other cpp file?

I am very new to programming but i have to do this for my project.
In Visual C++ 6.0, I am trying to send the calculated value from one function to the other in a different cpp file.
however, when I try to compile I get the following error :
Creating library Debug/usradd.lib and object Debug/usradd.exp
addrxn.obj : error LNK2001: unresolved external symbol "float * Gamma"
(?Gamma##3PAMA) ..\debug\usradd.dll : fatal error LNK1120: 1
unresolved externals
How can I fix this problem? Thank you.
This is simple version of code that I tried.
I like to pass Gamma matrix from addk.cpp to addrxn.cpp as shown below.
Thank you.
//callccx.h
//declare "Gamma" array
extern Gamma[23];
//addk.cpp
void addk(float *y, float *x, double t, double p, float *xkv)
{
float Gamma[1] = 2000*t;
.
.
.
float Gamma[23] = 2300*t;
for(int i=0;int<23;i++)
{
xkv[i] = 200*t/p*Gamma[i];
}
return;
}
//addrxn.cpp
int addrxn0(const int nUopID, const int nRxnID, const int nComponents,
const double fTemperature, const double fPressure, const double fRPM,
const double fBetaFac, const double fFreqFac, const double fExpActE,
const double *C, const double *Pi, const double *fStoich,
const double *fExponent, const double *fAdsFac, const double *fAdsE,
const double *fAdsExp, double *pRateForm)
{
//Arhenius
double rate=fExpActE*fFreqFac*Gamma[1]/Gamma[2];
int iComp;
for(iComp=0;iComp<nComponents;iComp++)
{
if(fStoich[iComp] < 0)
{ //This is a reactant.
if(fExponent[iComp] == 0.0)
rate*=pow(C[iComp], -fStoich[iComp]); //Use stoich as exponent
else
rate*=pow(C[iComp], fExponent[iComp]); //Use exponent as given
}
}
//Final rate of formation
(*pRateForm)=rate*
return 0;
}
The extern declaration requires a type:
extern float Gamma[23];
Your sample code:
float Gamma[1] = 2000*t;
makes no sense. Are you declaring a float array of ONE element here? You probably meant:
Gamma[1] = 2000 * t;
Anyway, your link error tells you that Gamma needs to be defined somewhere, probably in another file. Something like
float Gamma[23];

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.

Possible Class/Destructor setup issue, receiving error message 'unresolved external symbol'

EDIT: I believe my issue is because I'm calling my FacePoint class from an .exe project, and FacePoint is actually located in a .dll project, both within the same solution. Any idea to combat this?
I have a class called FacePoint which gets and sets X and Y values of a face feature.
This is FacePoint.h:
#pragma once
class FacePoint
{
public:
FacePoint(void);
FacePoint(int X, int Y);
void FacePoint::SetX(int X);
void FacePoint::SetY(int Y);
void FacePoint::SetXY(int X, int Y);
int FacePoint::GetX();
int FacePoint::GetY();
~FacePoint(void);
private:
int coordX;
int coordY;
};
...and FacePoint.cpp:
#include "StdAfx.h"
#include "FacePoint.h"
FacePoint::FacePoint(void)
{
coordX = 0;
coordY = 0;
}
FacePoint::FacePoint(int X, int Y)
{
coordX = X;
coordY = Y;
}
void FacePoint::SetX(int X){ coordX = X; }
void FacePoint::SetY(int Y){ coordY = Y; }
void FacePoint::SetXY(int X, int Y)
{
coordX = X;
coordY = Y;
}
int FacePoint::GetX(){ return coordX; }
int FacePoint::GetY(){ return coordY; }
FacePoint::~FacePoint(){}
This seems OK to me, however when I try to implement a vector of FacePoint objects, as follows:
std::vector<FacePoint> point;
result = GetFeaturePoints(pHandle, &point);
int x = 0;
int y = 0;
for(int p = 0; p < point.size(); p++)
{
x = point[p].GetX();
y = point[p].GetY();
}
I get the following compile-time errors:
Error 37 error LNK1120: 3 unresolved externals
Error 36 error LNK2001: unresolved external symbol "public: __thiscall
FacePoint::~FacePoint(void)" (??1FacePoint##QAE#XZ)
Error 35 error LNK2001: unresolved external symbol "public: int
__thiscall FacePoint::GetX(void)" (?GetX#FacePoint##QAEHXZ)
Error 34 error LNK2001: unresolved external symbol "public: int
__thiscall FacePoint::GetY(void)" (?GetY#FacePoint##QAEHXZ)
I'm not sure why this is happening, but from the error messages, I suspect it's something to do with my destructor?...
I searched around and most of these issues were around missing a constructor/destructor definition. To my knowledge, my constructor seems fine, and the error message seems to hint at the destructor. The only thing I can think of is I'm missing a desctructor which destroys int X, int Y, but I didn't think primitives needed that?...
Any help is appreciated. Thank you!!
EDIT: So I forgot to #include "FacePoint.h" in my main (where I am creating the vector of FacePoint). I added that in, but am receiving this compile-time error message:
Error 4 error LNK2019: unresolved external symbol "public: __thiscall FacePoint::~FacePoint(void)" (??1FacePoint##QAE#XZ) referenced in function "public: void * __thiscall FacePoint::`scalar deleting destructor'(unsigned int)" (??_GFacePoint##QAEPAXI#Z)
Any idea, guys? Thanks!
It looks like FacePoint.cpp is not part of your project. That would mean that the linker can't find the body of code that implements those functions.
Solution by OP.
Solved by replacing the class with a struct.

Passing 2D array into function

I'd like to pass my 2D Array of class Menza into function..
class Menza
{
public:
string PrintLunch() const {return Lunch;};
unsigned int PrintID() const {return ID;};
double PrintPrice() const {return Price;};
double PrinteValue() const {return eValue;};
string PrintDescription() const {return Description;};
void ChangeLunch(string Change) {Lunch = Change;};
void ChangePrice(double Change) {Price = Change;};
void ChangeID(int Change) {ID = Change;};
void ChangeeValue(double Change) {eValue = Change;};
void ChangeDescription(string Change) {Description = Change;};
private:
string Lunch;
double Price;
unsigned int ID;
string Description;
double eValue;
};
const int Lunches = 5;
void LoadFile(bool FileChoice,Menza (*InputFromFile)[Lunches]);
void CustomerSelection(Menza CustomerSelect[],Menza (*InputFromFile)[Lunches]);
int main()
{
Menza InputFromFile[Lunches][Lunches];
Menza CustomerSelect[Lunches];
bool FileChoice = false;
LoadFile(FileChoice,InputFromFile);
CustomerSelection(CustomerSelect,InputFromFile);
}
Once I compile this, it shows me:
Semestralka.obj : error LNK2019: unresolved external symbol "void __cdecl LoadFile(bool,class Menza (*)[5])" (?LoadFile##YAX_NPAY04VMenza###Z) referenced in function _main
1>E:\My VSB\ZP projekty\Semestralka\Debug\Semestralka.exe : fatal error LNK1120: 1 unresolved externals
Can someone explain me whats wrong in this function call?
Thanks
You don't have definition of LoadFile function, only declaration. Therefore compiler have no way to understand what this function should do.
You must define it or link a library where it is defined (and include a header from this library). (Same is true for CustomerSelection too).
Read more about difference between definition and declaration here: declare_vs_define

C++ Linker Error LNK2019

I'm still kind of new to C++ and don't know why I'm getting these linker errors while trying trying to call these functions in another class.
The errors are:
error LNK2019: unresolved external symbol "public: float __thiscall Star::getMass(void)" (?getMass#Star##QAEMXZ) referenced in function "public: void __thiscall Projectile::Update(class Star * const,int)" (?Update#Projectile##QAEXQAVStar##H#Z)
error LNK2019: unresolved external symbol "public: float __thiscall Star::getX(void)" (?getX#Star##QAEMXZ) referenced in function "public: void __thiscall Projectile::Update(class Star * const,int)" (?Update#Projectile##QAEXQAVStar##H#Z)
error LNK2019: unresolved external symbol "public: float __thiscall Star::getY(void)" (?getY#Star##QAEMXZ) referenced in function "public: void __thiscall Projectile::Update(class Star * const,int)" (?Update#Projectile##QAEXQAVStar##H#Z)
Projectile.cpp:
#include <hge.h>
#include "Projectile.h"
#include "Physics.h"
#include "Star.h"
#include <math.h>
Projectile::Projectile(float xV, float yV, float x, float y, float m, HTEXTURE tex)
{
xVel = xV;
yVel = yV;
xPos = x;
yPos = y;
mass = m;
quad.tex = tex;
}
void Projectile::Update(Star stars[], int length)
{
for(int i = 0; i<length; ++i)
{
float force = Physics::calcGravityForce(mass, stars[i].getMass(), Physics::calcDist(xPos, yPos, stars[i].getX(), stars[i].getY()));
Accelerate(force, stars[i].getX() - xPos, stars[i].getY() - yPos);
}
}
void Projectile::Accelerate(float force, float x, float y)
{
float c = sqrt((x * x) + (y * y));
xVel += x/c;
yVel += y/c;
}
Star is defined in Star.h here:
#ifndef STAR_H
#define STAR_H
#include <hge.h>
class Star
{
private:
float mass, radius, x, y;
hgeQuad quad;
public:
Star(float m, float r, float X, float Y, HTEXTURE);
float getMass();
float getRadius();
float getX();
float getY();
Star() {}
};
#endif
You have several functions declared in the Star class:
Star(float m, float r, float X, float Y, HTEXTURE);
float getMass();
float getRadius();
float getX();
float getY();
And you are trying to use some of them without providing a definition, that is to say, the body of the function, which is why you're getting those linker errors.
Add a new .cpp file to your project named Star.cpp (the name doesn't matter though) and add the definitions of the functions for the Star class, like you've done for the Projectile class. (You could just add them to any .cpp file in your project, like Projectile.cpp, but if you have a separate header file, it's good to have a seperate .cpp file too.)
Or if you don't want to have another cpp file in your project, you can put the bodies of the functions inside the class itself:
class Star
{
private:
float mass, radius, x, y;
hgeQuad quad;
public:
Star(float m, float r, float X, float Y, HTEXTURE);
float getMass() { return mass; }
float getRadius() { return radius; }
float getX() { return x; }
float getY() { return y; }
Star() {}
};
That style is common for small "getter" functions like getMass, getRadius, etc. which just return a member variable.
Though it's not directly related to your question, I should point out a few things:
Make all your "getter" functions (like getMass etc) const (so that they can be used on const Star objects) by putting the word const after the parameters (the () in this case) like this: float getMass() const { return mass; }
Because you have member variables in the Star class, you should set them to some sensible default value in the constructor which takes no parameters,
like this:
Star() : mass(0), radius(0), x(0), y(0) {}
Which will set mass, radius, x and y to 0. (This unusual syntax is called an initialiser list. You can read about them here.)
You can even do this without a seperate constructor by using default arguments:
Star(float m = 0, float r = 0, float X = 0, float Y = 0, HTEXTURE = 0);