Unresolved External Symbol - c++

I have encountered an error which has stumped me for many days now. A quick google hasn't given me an answer. The code, to my eyes, has no errors however when I run the program I get 9 Unresolved External Symbol(LNK2019) error. After trying to decipher one of my errors, I believe it is happening in a function named createMortgage. Here is my calling of the function.
customers is a Vector.
for (unsigned int i = 0; i < customers.size(); i++)
{
Customer tempcust = customers.at(i);
if (tempcust.getId() == id)
{
customers.at(i).createMortgage();
}
}
Here is the function itself.
void createMortgage(){
int amount;
cout << "Amount?";
cin >> amount;
Mortgage mort(amount);
mortgages.push_back(mort);
}
And here, in all it's glory, is the error.
Error 4 error LNK2019: unresolved external symbol "public: __thiscall Mortgage::Mortgage(double)" (??0Mortgage##QAE#N#Z) referenced in function "public: void __thiscall Customer::createMortgage(void)" (?createMortgage#Customer##QAEXXZ) F:\C++ assignment (Real)\C++ assignment (Real)\Driver.obj C++ assignment (Real)
Here is my mortgage .h file.
#pragma once
//#include <iostream>
//#include <String>
class Mortgage
{
private:
int id;
double amount;
public:
Mortgage(double amount);
double getAmount();
int getId();
};
And here is my mortgage .cpp file.
#pragma once
extern int idcreation;
class Mortgage
{
int id;
double amount;
Mortgage(double amount)
{
this -> amount = amount;
this -> id = idcreation;
idcreation++;
}
int getId(){
return id;
}
double getAmount(){
return amount;
}

Change this:
class Mortgage
{
int id;
double amount;
Mortgage(double amount)
{
this -> amount = amount;
this -> id = idcreation;
idcreation++;
}
int getId(){
return id;
}
double getAmount(){
return amount;
}
To this:
#include "mortgage.h"
Mortgage::Mortgage(double amount)
{
this -> amount = amount;
this -> id = idcreation;
idcreation++;
}
int Mortgage::getId(){
return id;
}
double Mortgage::getAmount(){
return amount;
}
I see you don't really get how to use headers and source files to make classes, this tutorial will get you on track: http://thenewboston.org/watch.php?cat=16&number=15.

1) You aren't linking (and probably not compiling) your mortgage.cpp file. Check your IDE project configuration to ensure that it includes mortgage.cpp as a source file.
2) You must not reproduce the class definition in your cpp file. Rather, structure it like this:
#include "mortgage.h"
Mortage::Mortgage(double d) { ... }

You have issues with basic C++ syntax.
#pragma once is Visual Studio specific and is a replacement for header guards. It should never appear in a .cpp file
You are providing two different definitions of class Mortage one is in the header, the second one is in the .cpp file
The correct syntax for defining class is the following:
The header file:
/* something.h */
#ifndef SOMETHING_H_
#define SOMETHING_H_
class Something
{
public:
Something();
void some_method();
};
#endif
The .cpp file:
/* something.cpp */
#include "something.h"
Something::Something() { /* implementation */ }
void Something::some_method() { /* implementation */ }

Related

Unresolved external symbol but the function is defined and implemented

I have a header file, defining the chunk class:
#pragma once
#include <vector>
#include "Tile.h"
#include "Numerics.h"
namespace boch {
class chunk {
public:
chunk();
static const uint defsize_x = 16;
static const uint defsize_y = 16;
std::vector<std::vector<tile*>> tilespace;
tile* getat(vint coords);
void fillc(tile t);
};
}
Then, I defined the implementation of the class in Chunk.cpp file:
#include "Chunk.h"
boch::chunk::chunk() {
tilespace = std::vector<std::vector<tile*>>(defsize_x);
for (int x = 0; x < defsize_x; x++) {
std::vector<tile*> temp = std::vector<tile*>(defsize_y);
tilespace[x] = temp;
}
}
void boch::chunk::fillc(tile t) {
for (int x = 0; x < defsize_x; x++) {
for (int y = 0; y < defsize_y; y++) {
tilespace[x][y] = new tile(t);
}
}
}
boch::tile* boch::chunk::getat(vint coords) {
return tilespace[coords.x][coords.y];
}
(vint is a typedef of boch::vector<int> which is the custom X,Y vector, if that helps)
Then, I use it in the main function in BochGrounds.cpp file:
#include <iostream>
#include "Layer.h"
#include "Gamegrid.h"
int main()
{
boch::layer newlayer = boch::layer(boch::vuint(16, 16));
boch::chunk newchunk = boch::chunk();
boch::gamegrid newgrid = boch::gamegrid();
newchunk.fillc(boch::tile());
newgrid.addchunk(boch::cv_zero, &newchunk);
newgrid.drawtolayer(&newlayer);
newlayer.draw(std::cout);
}
Tile class defines the gamegrid class, chunk includes tile class, gamegrid includes chunk & entity (which includes tile as well). Layer class includes only tile. All header files have #pragma once directive. When trying to compile, I'm getting the following error:
LNK2019 unresolved external symbol "public: __cdecl boch::chunk::chunk(void)" (??0chunk#boch##QEAA#XZ) referenced in function main
LNK2019 unresolved external symbol "public: void __cdecl boch::chunk::fillc(class boch::tile)" (?fillc#chunk#boch##QEAAXVtile#2##Z) referenced in function main
and as the result:
LNK1120 2 unresolved externals
Other StackOverflow answers suggest that the linker cannot see implementations of both fillc() and chunk constructor functions, but I cannot see why if it is even the problem here. Please help. (Linker settings haven't been changed, and are default for MVSC 2019)
Thanks sugar for the answer. I deleted both header and .cpp files and readded them, and it worked like a charm. I suppose I have added either header or .cpp file just by directly adding a new file to the header/source folder instead of adding it to the project (RMB click on the project > add new item).

error C2011: 'SelectorBox' : 'class' type redefinition

I have a problem with a following code:
#ifndef HEADER_H_
class SelectorBox{
public:
string selectorName;
map < string, string > attributeMap;
void setSelectorName(string name);
void setAttribute(string key, string value);
};
string trimTheString(string str); //trimming a string
#endif
//include libraries
#include "Header.h"
using namespace std;
int main()
{
vector <SelectorBox> vectorSelectBox;
SelectorBox *selectorBox;
//code
return 0;
}
#include "Header.h"
#include "main.cpp"
void SelectorBox::setSelectorName(string name) //setter
{
name = trimTheString(name);
selectorName = name;
}
void SelectorBox::setAttribute(string key, string value) //setter
{
key = trimTheString(key);
value = trimTheString(value);
attributeMap[key] = value;
}
When I compile a program, it shows many errors (specifically errors 4430 and 2061), but I believe that the main error is:
error C2011: 'SelectorBox' : 'class' type redefinition.
You must define HEADER_H_. You missed second line from below code.
#ifndef HEADER_H_
#define HEADER_H_
...
#endif
You are including the same header twice, first before main second after main, this causes a problem if you don't use proper include guards.
Your include guard is incomplete, so multiple includes of the same header will cause this error. The trick is to check if a header-specific preprocessor name is defined: If not, then define it, else skip the whole header.
The pattern to use is like this
#ifndef GUARD
#define GUARD
/// the actual header contents
#endif
but simply copying it into every header will cause another problem, because its lack of header specificity. The simplest way to find a good name is to derive it from the file name itself. In your case I'd name the header after the class SelectorBox defined in it, so SelectorBox.h would be a good name, and the include guards would look like this:
#ifndef SELECTORBOX_H
#define SELECTORBOX_H
class SelectorBox {
/// etc.
};
#endif
Using multiple headers with the same include guard is much worse than using no include guards at all.
I guess, you have 3 files:
Header.h
main.cpp
FileX.cpp (you didn't disclose the real name of mister X so far)
In main.cpp you are including Header.h, in FileX.cpp you are including Header.h and main.cpp. Let me show a simplified version of what happens here:
The contents of main.cpp gets transformed into
//include libraries
class SelectorBox{
public:
string selectorName;
map < string, string > attributeMap;
void setSelectorName(string name);
void setAttribute(string key, string value);
};
string trimTheString(string str); //trimming a string
using namespace std;
int main()
{
vector <SelectorBox> vectorSelectBox;
SelectorBox *selectorBox;
//code
return 0;
}
Assuming you included string and map and there is another using namespace std; somewhere before your class definition, this could compile without errors.
But now let's see what happens in FileX.cpp. Its contents gets transformed into the following, and I hope you'll see now what the compilers sees: there is more than one definition of the class SelectorBox:
class SelectorBox{
public:
string selectorName;
map < string, string > attributeMap;
void setSelectorName(string name);
void setAttribute(string key, string value);
};
string trimTheString(string str); //trimming a string
//include libraries
class SelectorBox{
public:
string selectorName;
map < string, string > attributeMap;
void setSelectorName(string name);
void setAttribute(string key, string value);
};
string trimTheString(string str); //trimming a string
using namespace std;
int main()
{
vector <SelectorBox> vectorSelectBox;
SelectorBox *selectorBox;
//code
return 0;
}
void SelectorBox::setSelectorName(string name) //setter
{
name = trimTheString(name);
selectorName = name;
}
void SelectorBox::setAttribute(string key, string value) //setter
{
key = trimTheString(key);
value = trimTheString(value);
attributeMap[key] = value;
}
... compilers use to call that a "redefinition".

Unresolved external symbol "public: int myclass::get_a (void)" How do I resolve this code? Noobie Q

Noobie programmer here learning C++ for the first time. The following is excerpted code from Teach Yourself C++ 3rd Edition.
I'm dying help me, I'm learning about classes, but I can't get this code to compile on visual studio or on Code::Blocks. :(
//#include "stdafx.h"
#include <iostream>
//I understand this. Headers, etc.
using namespace std;
//and this, name traffic management system
class myclass {
//private to myclass
int a;
public:
void set_a(int num);
int get_a();
};
/*I understand int a is private/inaccessible from the rest of the code
and void set_a(int num) is the dummy function.*/
void myclass::set_a(int num)
//not sure what this is
{
a = num;
}
/*self explanatory*/
int _tmain(int argc, _TCHAR* argv[])
{
myclass ob1, ob2;
ob1.set_a(10);
ob2.set_a(99);
cout << ob1.get_a() << "\n";
cout << ob2.get_a() << "\n";
return -5;
}
/*This is just supposed to output the number 10 and 99 right?? So why isn't it?*/
On Visual Studio the full error description is:
Error 1 error LNK2019: unresolved external symbol "public: int __thiscall myclass::get_a(void)" (?get_a#myclass##QAEHXZ) referenced in function _wmain c:\Users\bernardo pliego\documents\visual studio 2013\Projects\Chapter 1.5\Chapter 1.5\Chapter 1.5.obj Chapter 1.5
On Code::Blocks I receive the following error:
In function 'main':
undefined reference to 'my_class::get_a()'
I am in dire need of help, can someone explain this to me?
Because you don't define get_a, you only declare it. Add a definition like this:
int myclass::get_a() { return a; }
Or just define it inline:
class myclass {
//private to myclass
int a;
public:
void set_a(int num);
int get_a() { return a };
};
You don't ever define int get_a(); so you get an error at link-time.
Include this just above your (rather hubristic) /*self explanatory*/ comment
int myclass::get_a()
{
return a;
}

C++ - Error C2511: overloaded member function not found in 'BMI'

Got an error on my C++ program. It is likely to be something simple as I have only just started programming.
The error is:
Error 1 error C2511: 'void BMI::getWeight(double)' : overloaded member function not found in 'BMI' c:\users\**********\documents\visual studio 2012\projects\project2\project2\bmi.cpp 40 1 Project2
bmi.h:
#include <iostream>
#include <string>
using namespace std;
#ifndef BMI_H
#define BMI_H
class BMI {
public:
//Defualt Constructor
BMI();
//Overload Constructor
BMI(string, int, double);
//Destructor
~BMI();
//Accessor Functions
string getName() const;
// getName - returns name of paitent
int getHeight() const;
//getHeight - returns height of paitent
double getWeight() const;
//getWeight returns weight of paitent
private:
//Member Variables
string newName;
int newHeight;
double newWeight;
};
#endif
bmi.cpp:
// Function Definitions
#include "BMI.h"
BMI::BMI() {
newHeight = 0;
newWeight = 0.0;
}
BMI::BMI(string name, int height, double weight) {
newName = name;
newHeight = height;
newWeight = weight;
}
BMI::~BMI() {
}
string BMI::getName() const {
return newName;
}
int BMI::getHeight() const {
return newHeight;
}
double BMI::getWeight() const {
return newWeight;
}
void BMI::setName(string name) {
newName = name;
}
void BMI::setHeight(int height) {
newHeight = height;
}
void BMI::setWeight(double weight) {
newWeight = weight;
}
Ok, when I try to compile the code I see a couple of problems:
The setName(string) function in the .cpp doesn't match anything in the header.
The setHeight(int) function in the .cpp doesn't match anything in the header.
The setWeight(double) function in the .cpp doesn't match anything in the header.
I would try to solve the compilation errors in the order they occur, and then see if you still have a problem with getWeight. I'm assuming that you are seeing the same problems with the undeclared functions that I'm seeing.
The error appears to be telling you that you are trying to call BMI::getWeight() somewhere and you are passing in it a parameter with a double type. This error is a bit perplexing as no such function that matches void BMI::getWeight(double) defined in either the BMI class in the header file or the cpp file. If you have changed the code since you posted it up then please do update and post ALL of the compiler messages. I suspect that you have not posted all of the compiler messages because SetName,setHeight and setWeight are all missing from the BMI class definition. So make sure you add all of those into the BMI class.
Also I think that it's good practice to initialize your data members differently. So instead of:
BMI::BMI(string name, int height, double weight) {
newName = name;
newHeight = height;
newWeight = weight;
}
you should prefer:
BMI::BMI(string name, int height, double weight):
newName(name),
newHeight(height),
newWeight(weight)
{ }

C++ defining constructor from header

class_one.h:
#ifndef CLASS_ONE
#define CLASS_ONE
#include <string>
namespace ones{
typedef enum{BLACK, WHITE, RED} b_color;
typedef char b_letter;
const b_letter letters[4] = {'A', 'B', 'C', 'D'};
class one{
b_color color;
b_letter letter;
public:
one(b_color, b_letter);
std::string combo();
b_color getColor();
b_letter getLetter();
};
}
#endif
Given this header file, how should I go about creating the .cpp file, and how then instantiate this class in another file, main.cpp?
I would think something like this:
class_one.cpp
#include <iostream>
#include "class_one.h"
using namespace ones;
class one
{
b_color color;
b_letter letter;
public:
one(b_color c, b_letter l) //Not sure about this one..
{
color = c;
letter = l;
}
std::string combo()
{
return "blahblah temporary. letter: " + letter; //not finished
}
b_color getColor()
{
return color;
}
b_letter getLetter()
{
return letter;
}
};
and then to instantiate it, I would do something like this:
main.cpp
#include "class_one.h"
int main()
{
ones::one test(ones::BLACK, ones::letters[0]);
//cout<<test.name()<<endl;
return 0;
}
Everything is extracted from a larger cluster of files, but this is the essentials of my question.. The header file should be correct, but I'm not sure how to instantiate the 'one' class, and not with that constructor. I think the constructor I defined in the .cpp is wrong. I'm used to Java, so I've never seen a constructor like the one in the header file, if it's even a constructor. To me it looks like method(int, int) instead of what I'm used to: method(int a, int b)
When running this I get this error:
main.obj : error LNK2019: unresolved external symbol "public: __thiscall ones::one::one(enum ones::b_color, char)" (??0one#ones##QAE#W4b_color#1#D#Z) referenced in function _main
<path>/project.exe : fatal error LNK1120: 1 unresolved externals
Sorry for the incredibly stupid naming I have here, but it does make sense for the purpose. May be some typing errors in the question codes as I've written most of this by hand right now.
Any help appreciated..
Your cpp file should look like this:
#include "class_one.h"
ones::one::one(ones::one::b_color c, ones::one::b_color l)
{
//code here
}
std::string ones::one::combo()
{
// code here
}
// additional functions...
And so on. You don't redefine the class with a class block, you just specify the individual function definitions like I showed here. The function definition format should be something like this:
[return type] [namespace]::[class]::[function]([parameters])
{
// code here
}
It looks like you're good on instantiation. You also don't have to redeclare the member variables.