Unresolved External Symbol calling function - c++

I'm working on a assignment for class so this is only part of the code, but whenever I try to compile it I'm getting this error.
1>pass9.obj : error LNK2019: unresolved external symbol "void __cdecl bestbuy(double,double,double)" (?bestbuy##YAXNNN#Z) referenced in function _main
1>C:\Users\YoggieBear\Desktop\school\pass9\Debug\pass9.exe : fatal error LNK1120: 1 unresolved externals
Code is below
#include <iostream>
using namespace std;
void bestbuy(double, double, double);
void discountresults (double, double);
void howmany(double, double);
int price1, price2, price3;
int main ()
{
cout<<"Please enter 3 prices.\n";//This tests function bestbuy.
cin>>price1>>price2>>price3;
bestbuy(price1,price2,price3);
cout<<"Your lowest price entered was "<<price1<<" and it was the "<<price2<<" number you entered.\n";
system ("PAUSE");
return 0;
}
void bestbuy(double &val1,double &val2, double val3)
{
if (val1 < val2 && val1 < val3)
val2 = 1;
else if (val2 < val1 && val2 < val3)
{val1 = val2;
val2 = 2;}
else
{val1 = val3;
val2 = 3;}
}

Your function declaration does not match your function definition.
Update it as below:
void bestbuy(double&, double&, double);

Your forward declaration is
void bestbuy(double, double, double);
And your attempt at definition is
void bestbuy(double &val1,double &val2, double val3)
They are clearly referring to two different functions. Both signatures have to match, either drop the references or add them to the forward declaration as well. What you are doing right now is forward declaring a function and defining an overload for that non defined function.

You're declaring the function as taking values, but define it as taking references. Change the declaration to void bestbuy(double&, double&, double);.

Related

pointer to function methods within the class not working

I'm trying to to write a code that will call a function that receive a pointer to other function within the same class, and call get_num method from main.
but when doing include to TestClass.h from main, I'm getting linkage errors
class TestClass{
public:
void get_num(int num);
void foo(int num, void(TestClass::*function)(int));
void boo(int num);
};
void TestClass::boo(int num)
{
std::cout << "number: " << num << std::endl;
}
void TestClass::foo(int num, void(TestClass::*function)(int))
{
(this->*function)(num);
}
void TestClass::get_num(int num)
{
foo(num, &TestClass::boo);
}
Following is the main code:
#include "TestClass.h"
int main()
{
TestClass tc1;
tc1.get_num(5);
system("pause");
return 1;
}
The following errors appears:
1>main.cpp
1>TestClass.obj : error LNK2005: "public: void __thiscall TestClass::boo(int)" (?boo#TestClass##QAEXH#Z) already defined in main.obj
1>TestClass.obj : error LNK2005: "public: void __thiscall TestClass::foo(int,void (__thiscall TestClass::*)(int))" (?foo#TestClass##QAEXHP81#AEXH#Z#Z) already defined in main.obj
1>TestClass.obj : error LNK2005: "public: void __thiscall TestClass::get_num(int)" (?get_num#TestClass##QAEXH#Z) already defined in main.obj
1>c:\Proj4.exe : fatal error LNK1169: one or more multiply defined symbols found
Pointer to a method is not the same as a pointer to a simple function. If you only want to be able to accept a pointer to the method of the same class you can rewrite your method like this:
void TestClass::foo(int num, void(TestClass::*function)(int))
{
(this->*function)(num);
}
void TestClass::get_num(int num)
{
foo(num, &TestClass::boo);
}
This allows foo() to accept a pointer to any method in TestClass but not to any method of any other class nor to a simple function. It also calls the received method on the same object (note: this in this->*function).

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

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

Unresolved external symbol [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 9 years ago.
Error 1 error LNK2019: unresolved external symbol "bool __cdecl
prefix(unsigned int,unsigned int)" (?prefix##YA_NII#Z) referenced in
function _main c:\Users\Work\documents\visual studio
2012\Projects\Book\Project5\Project5\Source.obj Project5
Error 2 error LNK1120: 1 unresolved
externals c:\users\work\documents\visual studio
2012\Projects\Book\Project5\Debug\Project5.exe 1 1 Project5
I just..I dont even know what I have to ask you guys. How do I fix this?
This is the code:
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
using namespace std;
void citire(vector<unsigned int> myVector,int &nrElem);
bool prefix(unsigned int nr1,unsigned int nr2);
int main(){
int nrElem={0};
vector<unsigned int> myVector;
//citire(myVector,nrElem);
cout << prefix(123,1234);
system("pause");
return 0;
}
void citire(vector<unsigned int> myVector,int &nrElem){
cout << "NumarElemente=" ;
cin >> nrElem ;
for(int i=0;i<nrElem;i++){
unsigned int nrCitit;
cout << "Elem #" << i+1 <<"=";
cin >> nrCitit;
myVector.push_back(nrCitit);
};
for(int i=0;i<nrElem;i++){
cout << myVector.at(i);
};
}
bool prefix(unsigned int &nr1,unsigned int &nr2){
unsigned int nr1copy=nr1;
unsigned int nr2copy=nr2;
int digitsNr1 = 0; while (nr1copy != 0) { nr1copy /= 10; digitsNr1++; }
int digitsNr2 = 0; while (nr2copy != 0) { nr2copy /= 10; digitsNr1++; }
if ( nr2/_Pow_int(10,digitsNr2-digitsNr1)==nr1) {return true;}
else return false;
}
bool prefix(unsigned int nr1,unsigned int nr2);
is not same as
bool prefix(unsigned int& nr1,unsigned int &nr2);
In forward forward declaration, you are taking the parameters by value but in the definition it is by reference. Keep the argument types same in the declaration and the definition.
unresolved external symbol "bool __cdecl prefix(unsigned int,unsigned int)"
Usually when you see these kind of linker errors, the first thing you need to check is if the function's declaration and definition signatures match or not. In this case, it is clearly not.
Declaration:
bool prefix(unsigned int nr1,unsigned int nr2);
definition:
bool prefix(unsigned int &nr1,unsigned int &nr2){ ... }
See the difference? Both should be the same. Looking at your code, it looks like you should keep the version in the declaration.
You have a prototype for the prefix() function:
bool prefix(unsigned int nr1,unsigned int nr2);
which has a signature that differs from the implementation given below:
bool prefix(unsigned int &nr1,unsigned int &nr2) {
^^^ ^^^
....
}
Note that in the prototype, the nr1 and nr2 parameters are passed by value; instead, in the implementation signature, they are passed by reference (note the &).
Both prototype and implementation signatures should match. Fix the wrong one.
(Note: since you can't pass literals like the 123 in main() as non-const reference, I think the wrong one is the implementation signature, i.e. drop the & in the implementation signature).