C++ compile error LNK2019: unresolved external symbol error [duplicate] - c++

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 3 years ago.
This problem has been solved before, but I've been looking all over, and none of those explains how to fix this, the situation i'm in. Most of them is about external libraries.
I'm trying to test my code. I've made a test class and that class is trying to access another class by including that class's header file. But when I'm trying to call its function it just gives me an unresolved external symbol error.
This is my current attempt. Here I'm trying to access the other classes's header file to call it's functions.
CardTest.cpp
#include <iostream>
#include "../Header Files/Hand.h"
#include "../Header Files/HandValueCalculator.h"
using namespace std;
HandValueCalculator handValueCalculator;
Hand hand;
void Test() {
bool value = handValueCalculator.DoesHandHaveAce(&hand.cards);
cout << value << endl;
}
HandValueCalculator.h
#ifndef HANDVALUECALCULATOR_H_INCLUDED
#define HANDVALUECALCULATOR_H_INCLUDED
#include <vector>
#include "../Header Files/Card.h"
class HandValueCalculator {
public:
HandValueCalculator();
bool DoesHandHaveAce(std::vector<Card>* cards);
int GetValueWithoutAce(std::vector<Card>* cards);
int GetValueWithAce(std::vector<Card>* cards);
};
#endif // HANDVALUECALCULATOR_H_INCLUDED
HandValueCalculator.cpp
#include "../Header Files/HandValueCalculator.h"
HandValueCalculator::HandValueCalculator() {
}
bool HandValueCalculator::DoesHandHaveAce(std::vector<Card>* cards) {
int i;
for (i = 0; i < cards.size(); i++) {
if (cards.at(i).GetValue() == 11) {
return true;
}
}
return false;
}
int HandValueCalculator::GetValueWithoutAce(std::vector<Card>* cards) {
for (i = 0; i < cards.size(); i++) {
int cardValue = cards.at(i).GetValue()
totalValue = totalValue + cardValue;
}
return 0;
}
int HandValueCalculator::GetValueWithAce(std::vector<Card>* cards) {
return 0;
}
This is the error I'm getting, and I don't think the compiler recognizes that the functions have a body, and because it can't find a body for the declared functions it returns an error like this.
C:\Users\fagel\Documents\Blackjack\Blackjack\CardTest.obj : error LNK2019: unresolved external symbol "public: void __thiscall HandValueCalculator::a(void)" (?a#HandValueCalculator##QAEXXZ) referenced in function "void __cdecl Test(void)" (?Test##YAXXZ)

Your HandValueCalculator does not have a void a(); implementation available to the linker. If the a function is defined, make sure you link with the object file containing the definition.
However, you're most likely the victim of the most vexing parse and think you've declared a to be a variable (somewhere not shown), but you've instead declared a function (without definition).

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

C++ Unresolved External on Class in Project [duplicate]

This question already has answers here:
Why do I get "unresolved external symbol" errors when using templates? [duplicate]
(3 answers)
Closed 3 years ago.
So I'm new to C++ and Visual Studio and I'm trying to implement a hash table using templates. I have four files: main.cpp, HashNode.h, HashTable.h, and HashTable.cpp.
main calls the HashTable constructor with a paramenter (the definition is in HashNode.h, with the implementation in the cpp file), but this throws 2 unresolved external errors: one for the called constructor, and one for what I assume to be the default constructor.
However, main also calls the HashNode constructor with no problems. HashNode has its implementation and declaration all in the HashNode.h file, but moving HashTable's implementation to its .h file didn't clear the error. So I'm very confused lol.
I'm running Visual Studio 2019, fresh install, and using the default build button to build it. It does compile and run other things (like hello world), just not this.
I've also tried adding random garbage into HashTable.cpp to see if the compiler just didn't see that it existed, but that's not the case. It also throws a compilation error then.
HashTable.h:
#pragma once
#include "HashNode.h"
template <typename T>
class HashTable
{
public:
void AddItem(int key, T item);
T* GetItem(int key);
HashTable(int buckets);
~HashTable();
int print();
private:
HashNode<T>** elements;
int buckets;
};
HashTable.cpp:
#include "HashTable.h"
#include "HashNode.h"
#include <stdexcept>
template<typename T>
HashTable<T>::HashTable(int buckets)
{
elements = new HashNode<T> * [buckets];
for (int i = 0; i < buckets; i++)
{
elements[i] = nullptr;
}
HashTable::buckets = buckets;
}
... //other methods defined below
HashNode.h
#pragma once
template <typename V>
class HashNode
{
public:
HashNode(int key, const V value) : k(key), v(value), next(nullptr) {}
int getKey () const { return k; }
V getValue() const { return v; }
HashNode* getNext() const { return next; }
void setNext(HashNode* next) { HashNode::next = next; }
void appendToChain(HashNode* last)
{
HashNode* curr = this;
while (curr->getNext() != nullptr)
{
curr = curr->getNext();
}
curr.setNext(last);
}
private:
int k;
V v;
HashNode* next;
};
Main.cpp:
#include <iostream>
#include "HashTable.h"
#include "HashNode.h"
int main()
{
std::cout << "Hello World!\n";
HashNode<int> node(1,1); //works fine
std::cout << node.getValue() << std::endl; //prints fine
HashTable<int> table(5); //throws error on compilation
}
It's probably just something stupid or that I'm blind, but here's the errors:
Error LNK1120 2 unresolved externals HashTable D:\C++\HashTable\Debug\HashTable.exe 1
Error LNK2019 unresolved external symbol "public: __thiscall HashTable<int>::HashTable<int>(int)" (??0?$HashTable#H##QAE#H#Z) referenced in function _main HashTable D:\C++\HashTable\HashTable\Main.obj 1
Error LNK2019 unresolved external symbol "public: __thiscall HashTable<int>::~HashTable<int>(void)" (??1?$HashTable#H##QAE#XZ) referenced in function _main HashTable D:\C++\HashTable\HashTable\Main.obj 1
Also, please don't hesitate to give me pointers if my code's bad. I've never really programmed anything in C++ before so any help is welcome!
You need to move the template function definitions into the header file.
A longer answer can be found here.

error using static variable unresolved external symbol / undefined reference [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 3 years ago.
I am using static variable. After referring to Unresolved external symbol on static class members, I modified the program with Abc::ct
#include <iostream>
class Abc
{
private:
static unsigned int ct;
public:
void f1()
{
for (int i = 0; i < 5; ++i)
f2();
}
void f2() {
Abc::ct = 0;
if (Abc::ct == 0)
std::cout << "Zero iteration\n";
std::cout << Abc::ct << "\t";
++Abc::ct;
}
};
int main()
{
Abc obj;
obj.f1();
}
but getting error as error LNK2001: unresolved external symbol "private: static unsigned int Abc::ct" in MSVC or undefined reference to Abc::ct in g++. How can I define static variable in class Abc?
You declared your static variable, but you did not define and initialize it. Above main(), but outside of your class, add the following line:
unsigned int Abc::ct = 0;
or, if you are using C++17, you can change your:
static unsigned int ct;
to:
static inline unsigned int ct = 0;
You have to define it:
unsigned int Abc::ct = 0;
Demo

Unresolved external symbol (singleton class C++) [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 7 years ago.
I already looked up some answers in Stackoverflow for this type of problem, none of which are helping me out. This question describes how to resolve this error, and that I should provide a definition and not just a declaration. I've done that, but I'm still getting the following error:
Error 13 error LNK2019: unresolved external symbol "private: __thiscall NetworkManager::NetworkManager(void)" (??0NetworkManager##AAE#XZ) referenced in function "public: static class NetworkManager * __cdecl NetworkManager::Instance(void)" (?Instance#NetworkManager##SAPAV1#XZ) C:\Users\HIDDEN\Documents\AGK Projects\C++ Libraries\apps\template_windows_vs2013\NetworkManager.obj Template
Here's the code:
NetworkManager.h
#ifndef _H_NETWORKMANAGER_
#define _H_NETWORKMANAGER_
#include<iostream>
#include<vector>
class NetworkManager
{
private:
NetworkManager();
static NetworkManager * netManager;
public:
int networkID;
static NetworkManager * Instance();
int HostNetwork(std::string netName, std::string hostName, int port);
int JoinNetwork(std::string netName, std::string clientName);
bool IsNetworkActive(int netID);
};
#endif
NetworkManager.cpp
#include<iostream>
#include "NetworkManager.h"
#include "template.h"
NetworkManager * NetworkManager::netManager = NULL;
NetworkManager * NetworkManager::Instance()
{
if (!netManager)
netManager = new NetworkManager;
return netManager;
}
int NetworkManager::HostNetwork(std::string netName, std::string hostName, int port)
{
int networdID__;
const char * netName__ = netName.c_str();
const char * hostName__ = hostName.c_str();
networdID__ = agk::HostNetwork(netName__, hostName__, port);
return networdID__;
}
int NetworkManager::JoinNetwork(std::string netName, std::string clientName)
{
int networdID__;
const char * netName__ = netName.c_str();
const char * clientName__ = clientName.c_str();
networdID__ = agk::JoinNetwork(netName__, clientName__);
return networdID__;
}
bool NetworkManager::IsNetworkActive(int netID)
{
switch (agk::IsNetworkActive(netID))
{
case 0: return false; break;
case 1: return true; break;
}
}
You declared NetworkManager::NetworkManager() in the header file but there is no implementation of it in the source file.

Multiple class in a list c++ [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why can templates only be implemented in the header file?
Main class
int main() {
initCarList();
}
void initCarList() {
List<Car> carList;
Car c1 = Car("Toyota", "Bettle", 5);
carList.add(c1);
Car c2 = Car("Mercedes", "Bettle", 7);
carList.add(c2);
Car c3 = Car("FireTruck", "Large Van", 20);
carList.add(c3);
Car c4 = Car("Puma", "Saloon Car", 10);
carList.add(c4);
}
List class
#include "List.h"
#include <iostream>
using namespace std;
template <typename ItemType>
class List {
private:
ItemType itemList[10];
int size;
public:
List();
void add(ItemType);
void del(int index);
bool isEmpty();
ItemType get(int);
int length();
};
template<typename ItemType>
List<ItemType>::List() {
size = 0;
}
template<typename ItemType>
void List<ItemType>::add(ItemType item) {
if(size < MAX_SIZE) {
itemList[size] = item;
size++;
} else {
cout << typename << " list is full.\n";
}
}
I got errors like these
Error 3 error LNK2019: unresolved external symbol "public: void
__thiscall List::add(class Car)" (?add#?$List#VCar####QAEXVCar###Z) referenced in function "void
__cdecl initCarList(void)" (?initCarList##YAXXZ) C:\Users\USER\Desktop\New
folder\DSA_Assignment\main.obj DSA_Assignment
Did I do anything wrongly in my code? NEED HELP THANKS!
There is a syntax error (cout << typename ) in your code. I don't know how you got the linker error. May be its not being compiled at all.
otherwise its okay http://ideone.com/PGWGZu
Clearly you did as it doesn't work! Flippancy aside, let's take a look at the error message bit by bit:
Error 3 error LNK2019: unresolved external symbol
So this is a linkage error. The linker is trying to put together the units that were individually compiled together, but in this case it can't find an external symbol - usually a function or variable name.
"public: void __thiscall List::add(class Worker)" (?add#?$List#VWorker####QAEXVWorker###Z)
This is the full signature of the function that you're missing. It's name manged unfortunately but with your context knowledge of the code that you're writing, you should be able to tell that it's:
void List::add(Worker)
The next bit ...
referenced in function "void __cdecl initWorkerList(void)" (?initWorkerList##YAXXZ) C:\Users\USER\Desktop\New folder\DSA_Assignment\main.obj DSA_Assignment
... is telling you where the problem is happening, i.e where in the code it's trying to link, there is a reference to the missing function. Again, after demangling it's in:
void initWorkerList()
As you can see, with a bit of graft, you can determine exactly what you've done wrong here. Hope this helps.