C++ Linker Error LNK2019 - c++

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

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

C++ Direct2D - Linker error 2019 after moving sine files from my laptop

I wanted to move some code that I wrote, on my laptop, to my other PC. I didn't use Github or any other type of source control, I just simply copied the c++ files on to a USB drive and put them on my other PC.
And now I'm getting an error, for some reason, when I try to run the code?
The code is simply a class that uses direct2d to draw on a window.
This is what it looks like:
#pragma once
#include <Windows.h>
#include <d2d1.h>
#include <iostream>
#include "LinkedList.h"
class Graphics
{
private:
ID2D1Factory* pFactory;
ID2D1HwndRenderTarget* pRenderTarget;
ID2D1SolidColorBrush* pBrush;
RECT bounds;
float lineWidth = 5.0f;
LinkedList pointList;
public:
Graphics();
~Graphics();
void BeginDraw() { pRenderTarget->BeginDraw(); };
void EndDraw() { pRenderTarget->EndDraw(); };
void SetBrushColor(float r, float g, float b, float a);
void SetBrushColor(float r, float g, float b);
void SetLineWidth(float width);
RECT GetBounds();
void ClearScreen(float r, float g, float b, float a);
void ClearScreen(float r, float g, float b);
void FillCircle(float x, float y, float radius);
void DrawCircle(float x, float y, float radius);
void FillRect(float x, float y, float w, float h);
void DrawRect(float x, float y, float w, float h);
void MoveTo(float x, float y);
void LineTo(float x, float y);
void tester();
bool Init(HWND* pWindowHandle);
};
And the error I'm getting is this:
Error LNK2019 unresolved external symbol _D2D1CreateFactory#16 referenced in function "long __cdecl D2D1CreateFactory(enum D2D1_FACTORY_TYPE,struct _GUID const &,void * *)" (?D2D1CreateFactory##YAJW4D2D1_FACTORY_TYPE##ABU_GUID##PAPAX#Z) ffff c:\Users\sharkgaming\documents\visual studio 2015\Projects\ffff\ffff\Graphics.obj 1
At first I thought it was because I forgot to link to the d2d1.lib but even after doing that I'm still getting the error?
So, does anyone know why I'm getting the error and how to fix it?
Okay,
after googling around, for an hour or two, I found out that basically adding this:
#pragma comment(lib,"d2d1.lib")
would fix my problem.
But I have absolutely no idea why, since I already went in to the linker settings and added d2d1.lib to the additional library files?
EDIT: OMG I just figured it out.
I added the library to the x64 build settings not the x86. -_-

calling a function template from a second class

I’m trying to call a function template defined inside a class from within another class, but I’m stuck.
I get error:
1>class1.obj : error LNK2019: riferimento al simbolo esterno "public: void __thiscall class2::output<double,long>(double,long,float)" (??$output#NJ#class2##QAEXNJM#Z) non risolto nella funzione "public: void __thiscall class1::x(class class2 &)" (?x#class1##QAEXAAVclass2###Z)
1>class1.obj : error LNK2019: riferimento al simbolo esterno "public: void __thiscall class2::output<float,int>(float,int,float)" (??$output#MH#class2##QAEXMHM#Z) non risolto nella funzione "public: void __thiscall class1::y(class class2 &)" (?y#class1##QAEXAAVclass2###Z)
1>C:\xxx.exe : fatal error LNK1120: 2 esterni non risolti
If I uncomment lines with duplicate functions (the ones without templates) it is ok.
Can you help fix it?
File: “class1.h”
#ifndef CLASS1
#define CLASS1
#include "class2.h"
class class1{
public:
void x(class2& c );
void y(class2& c );
};
#endif
File: “class1.cpp”
#include "class1.h"
void class1::x( class2& c )
{
double img;
long integ;
float y;
c.output(img, integ, y);
}
void class1::y(class2& c )
{
float img;
int integ;
float y;
c.output(img, integ, y);
}
File: “class2.h”
#ifndef CLASS2
#define CLASS2
class class2{
void output2(double img, long integ, float y);
void output2(float img, int integ, float y);
public:
template <typename T1, typename T2>
void output(T1 img, T2 integ, float y);
//void output(double img, long integ, float y);
//void output(float img, int integ, float y);
};
#endif
File: “class2.cpp”
#include "class2.h"
template <typename T1, typename T2>
void class2::output(T1 img, T2 integ, float y)
{output2(img, integ, y);}
//void class2::output(double img, long integ, float y)
//{output2(img, integ, y);}
//void class2::output(float img, int integ, float y)
//{ output2(img, integ, y);}
void class2::output2(double img, long integ, float y){/*...*/}
void class2::output2(float img, int integ, float y){/*...*/}
EDIT
I’m talking about function templates and not class templates. I’ve seen the question I would be duplicating before, but it is not what I’m looking for.
I have two functions inside a class that are identical except for their parameters type.
I only wanted a simple trick to avoid writing and maintaining identical code for two functions inside the same class.
Anyway I’ve found a solution, adding these two lines at the bottom of class2.cpp:
template void class2::output<double, long>(double img, long integ, float y);
template void class2::output<float, int>(float img, int integ, float y);
If you want a specialization you need to implement the "default" template function in the .h file and define which specializations you are implementing. In the .cpp file you simply implement the specializations.
File: "class2.h"
#ifndef CLASS2
#define CLASS2
class class2{
public:
template <typename T1, typename T2>
void class2::output(T1 img, T2 integ, float y)
{ /* .. do stuff.. */}
};
template <>
void class2::output(double img, long integ, float y);
#endif
File: “class2.cpp”
#include "class2.h"
template <>
void class2::output(double img, long integ, float y)
{ /* .. do specialized stuff.. */}

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

Overloading difficulty with an extended sprite class. How is this wrong?

So I've been working on a puzzle game using a sprite system I used fine previously and in my past projects have been fine when extending the class, however my code keeps throwing an error of
error C2511:
'Door:: Door(float,float,float,float,float,float,int,CGame *)' :
overloaded member function not found in 'Door'
and I have no idea why as i've checked all the classes in my previous project and how they interface with the rest of the program and it's IDENTICAL but this new class is still erroring out. and the code in my main to create the object throws "no overloaded function in Door takes 8 arguments" out.
Door Class:
Door.h
#pragma once
#include "sprite.h"
class CGame;
class Door :
public CSprite
{
public:
Door(void);
Door(float, float, float, float,float, float, float, int, CGame * p_Game);
void Update(float dt);
~Door(void);
private:
};
Door.CPP
#include "stdafx.h"
#include "Door.h"
#include "Game.h"
Door::Door(void)
{
}
//So this is where i try to make door class extend sprite. but it keeps saying "overloaded member function not found in Door"
//and the other error is "doesnt take 8 args" and to top it off. It says unexpected end of file.
//Uncomment this block and the code in the door bit of the map gen to see what it is doing wrong
Door::Door(float _x, float _y, float _w, float _h, float _vX, float _vY, int _texID, CGame * p_Game) : CSprite(_x, _y, _w, _h, _vX, _vY, _texID, p_Game)
{
m_iType = 4 //sets the type of sprite that this object is.
}
void Door::Update(float dt)
{
}
Door::~Door(void)
{
}
and this is the sprite class i am extending (just the relevant parts)
Sprite.h
#pragma once
class CGame;
class CSprite
{
public:
float m_fX; //the position of the centre of the sprite
float m_fY;
float m_fW; //width of the sprite in arbitrary units
float m_fH; //height of the sprite in arbitrary units
float m_fvX;
float m_fvY;
int m_iTextureID; //which of the preloaded textures to use
float m_fR; //red component between 0 and 1
float m_fG; //green component between 0 and 1
float m_fB; //blue component between 0 and 1
float m_fA; //alpha value 0-1
int m_iType;
CGame * m_pGame;
public:
CSprite();
CSprite(float x, float y, float w, float h,float vX,float vY, int textureID ,CGame * p_Game);
bool bIsCollidingWith( CSprite * othersprite_);
bool markedForDelete;
//This new constructor is added to the Csprite.h header file.
float getX() { return m_fX; }
float getY() { return m_fY; }
virtual bool TagForDeletion();
virtual int GetSpriteType();
virtual ~CSprite();
virtual void Render();
virtual void Update(float dt);
};
Sprite.cpp
#include "StdAfx.h"
#include <gl.h>
#include <glut.h>
#include <glaux.h>
#include "main.h"
#include "sprite.h"
#include "Game.h"
CSprite::CSprite( )
{
m_fX=0.0f;
m_fY=0.0f;
m_fW=1.0f;
m_fH=1.0f;
m_fvX=0.0f;
m_fvY=0.0f;
markedForDelete=false;
m_fR=m_fG=m_fB=m_fA=1.0;
m_iTextureID=0;
}
CSprite::CSprite(float x_, float y_, float w_, float h_,float vX_,float vY_, int textureID_, CGame * p_Game)
{
m_iType = 1;
m_fX=x_;
m_fY=y_;
m_fW=w_;
m_fH=h_;
m_fvX=vX_;
m_fvY=vY_;
m_fR=m_fG=m_fB=m_fA=1.0;
m_iTextureID=textureID_;
m_pGame = p_Game;
markedForDelete=false;
}
CSprite::~CSprite(void)
{
}
Implementation in the game class uses the parameters extended from Sprite to create the object
p_Door[m_iSpritesLoaded++]=new Door(uiRow,4.58 -uiCol,1,1,0,0,4,this);
You miscounted your floats.
Declaration
Door(float, float, float, float,float, float, float, int, CGame * p_Game);
// 1 2 3 4 5 6 7
Definition
Door::Door(float _x, float _y, float _w, float _h, float _vX, float _vY, int _texID, CGame * p_Game)
// 1 2 3 4 5 6
: CSprite(_x, _y, _w, _h, _vX, _vY, _texID, p_Game)
Usage
p_Door[m_iSpritesLoaded++]=new Door(uiRow,4.58 -uiCol,1,1,0,0,4,this);
// 1 2 3 4 5 6
(Did you really need Stack Overflow for this?!)
Looks like your Door constructor has one extra float parameter. The declaration has 7 floats whereas the definition in Door.cpp has 6.
In Door.h the constructor has 9 arguments
Door(float, float, float, float,float, float, float, int, CGame * p_Game);
while in the Door.cpp the definition of the constructor has only 8 elements, that is one float is missing.
Solution:
add one argument to the definition of the constructor
CSprite::CSprite(float x_, float y_, float w_, float h_,float vX_,float vY_, int textureID_, CGame * p_Game)