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).
Related
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.
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).
I have created a template class that is supposed to store a grid as a 2-dimensional std::vector; however, when I compile, using VC++ (2010 if it matters, but I doubt it) I get the following error:
unable to match function definition to an existing declaration
Even though the two functions it is trying to match are exactly equal.
Here is the code in the header file:
#pragma once
#include "CBlock.h"
template<class T>
class CMyGrid{
public:
long sizeX;
long sizeY;
/*block position on grid*/
std::vector<std::vector<T*>> System;
CMyGrid();
~CMyGrid();
CMyGrid(int sizeXp, int sizeYp){sizeX = sizeXp; sizeY =sizeYp;};
void Set(T *data, int x, int y){System.at(x).at(y) = data;};
int GetSizeX(){return sizeX;}
int GetSizeY(){return sizeY;}
int getxPosition(T *data); /*make easier put in struct*/
int getyPosition(T *data);
/*size*/ /* will hopefully be sizex by sizey*/
};
And here is the cpp file:
#include "stdafx.h"
#include "CMyGrid.h"
#include "CBlock.h"
template <class T>
template <class T>
int CMyGrid<T>::getxPosition(T *data)
{
for (int i = 0; i <System.size(); i++)
{
for (int j = 0; j < System[i].size(); j++)
{
if data == System[i][j];
return j;
else
continue;
}
}
}
template <class T>
int CMyGrid<T>::getyPosition(T *data)
{
for (int i = 0; i <System.size(); i++)
{
for (int j = 0; j < System[i].size(); j++)
{
if data == System[i][j];
return i;
else
continue;
}
}
}
Here's the entire error:
1>c:\users\chris\documents\visual studio 2010\projects\testtest\testtest\cmygrid.cpp(33): error C2244: 'CMyGrid::getxPosition' : unable to match function definition to an existing declaration
1> c:\users\chris\documents\visual studio 2010\projects\testtest\testtest\cmygrid.h(18) : see declaration of 'CMyGrid::getxPosition'
1> definition
1> 'int CMyGrid::getxPosition(T *)'
1> existing declarations
1> 'int CMyGrid::getxPosition(T *)'
I have read several other threads with similar problems, and have gotten as far as changing the error to a linker error which I get if I include the function code for the two getposition functions in the header file alongside the declaration. The linker error is:
1>CBoard.obj : error LNK2019: unresolved external symbol "public: __thiscall CMyGrid::CMyGrid(void)" (??0?$CMyGrid#VCBlock####QAE#XZ) referenced in function "public: __thiscall CBoard::CBoard(void)" (??0CBoard##QAE#XZ)
1>CBoard.obj : error LNK2019: unresolved external symbol "public: __thiscall CMyGrid::~CMyGrid(void)" (??1?$CMyGrid#VCBlock####QAE#XZ) referenced in function "public: __thiscall CBoard::~CBoard(void)" (??1CBoard##QAE#XZ
The problem is that you can't have the implementation of a template in the .cpp.
Try to change your code into the .h
Here you have a big post explaining this.
Storing C++ template function definitions in a .CPP file
I'm having a problem with a vector declaration.
Here's the code:
.h
#ifndef ANIMATEDSPRITE_H_
#define ANIMATEDSPRITE_H_
#include "Sprite.h"
#include <vector>
//using namespace std;
class AnimatedSprite //abstract class to point sprites
{
public:
AnimatedSprite();
~AnimatedSprite();
//gets and sets
Sprite GetMySprite(int _index);
void SetSpriteToList(Sprite _sprite);
int GetState() const;
void SetState(int _state);
//other
private:
std::vector<Sprite> spriteList;
int state; //estado que esse sprite representa (parado esquerda, andando direita, etc)
};
#endif
.cpp
#include "AnimatedSprite.h"
AnimatedSprite::AnimatedSprite()
{
spriteList.clear();
state = NULL;
}
AnimatedSprite::~AnimatedSprite()
{
}
Sprite AnimatedSprite::GetMySprite(int _index)
{
return spriteList[_index];
}
void AnimatedSprite::SetSpriteToList( Sprite _sprite )
{
//Sprite* temp = new Sprite(1,2);
spriteList.push_back(_sprite);
}
int AnimatedSprite::GetState() const
{
return state;
}
void AnimatedSprite::SetState( int _state )
{
state = _state;
}
But I'm getting 2 errors:
Error 1 error LNK2019: unresolved external symbol imp_CrtDbgReportW referenced in function "public: class Sprite & __thiscall std::vector >::operator[](unsigned int)" (??A?$vector#VSprite##V?$allocator#VSprite###std###std##QAEAAVSprite##I#Z) AnimatedSprite.obj
Error 2 fatal error LNK1120: 1 unresolved externals C:\DevProjects\SDLSkeleton\Debug\SDLSkeleton.exe
I've found a solution removing the _DEBUG from the Preprocessor Definitions, but it seems kinda wrong to do that.
Is it the right solution? What's the consequence of removing it?
In the book and documentations I've checked it should be just a common variable declaration, but this errors showed up.
Thanks.
This is because your build is inconsistent: you define _DEBUG macro, but link with release CRT version (/MD). So either remove _DEBUG, or select /MDd option.
I'm getting the following error while compiling this simple program using Visual Studio:
error LNK2019: unresolved external symbol "public: void __thiscall CoList<int>::enqueue(int)" (?enqueue#?$CoList#H##QAEXH#Z) referenced in function _main
error LNK2019: unresolved external symbol "public: virtual __thiscall CoList<int>::~CoList<int>(void)" (??1?$CoList#H##UAE#XZ) referenced in function _main
error LNK2019: unresolved external symbol "public: int __thiscall CoList<int>::dequeue(void)" (?dequeue#?$CoList#H##QAEHXZ) referenced in function _main
error LNK2019: unresolved external symbol "public: int __thiscall CoList<int>::count(void)" (?count#?$CoList#H##QAEHXZ) referenced in function _main
error LNK2019: unresolved external symbol "public: __thiscall CoList<int>::CoList<int>(void)" (??0?$CoList#H##QAE#XZ) referenced in function _main
error LNK1120: 5 unresolved externals
My program is very simple. I don't use external libraries, just the 'iostream' and 'exception' headers... Here is the full code:
CoList.h
#pragma once
#include "CoListItem.h"
template <class T>
class CoList
{
public:
CoList();
virtual ~CoList();
void enqueue(T value);
T dequeue();
T *peek();
int count();
private:
CoListItem<T> *m_root;
int m_count;
};
CoListItem.h
#pragma once
template <class T>
class CoListItem
{
public:
CoListItem();
virtual ~CoListItem();
T value;
CoListItem *next;
};
CoList.cpp
#include "CoList.h"
#include <exception>
template <class T>
CoList<T>::CoList()
{
}
template <class T>
CoList<T>::~CoList()
{
}
template <class T>
void CoList<T>::enqueue(T value)
{
if (this->m_root != NULL) {
this->m_root = new CoListItem<T>();
this->m_root->value = value;
this->m_root->next = NULL;
} else {
CoListItem<T> *tempitem = new CoListItem<T>();
tempitem->value = value;
tempitem->next = this->m_root;
this->m_root = tempitem;
}
this->m_count++;
}
template <class T>
T CoList<T>::dequeue()
{
if (this->m_root == NULL) {
throw std::exception();
} else {
T retval = this->m_root->value;
CoListItem *next = this->m_root->next;
delete this->m_root;
this->m_root = next;
return retval;
}
}
template <class T>
T *CoList<T>::peek()
{
if (this->m_root == NULL) {
return NULL;
} else {
return *this->dequeue();
}
}
template <class T>
int CoList<T>::count()
{
return this->m_count;
}
CoListItem.cpp
#include "CoListItem.h"
template <class T>
CoListItem<T>::CoListItem()
{
}
template <class T>
CoListItem<T>::~CoListItem()
{
}
and finally the main function:
#include <iostream>
#include "CoList.h"
#include "CoListItem.h"
using namespace std;
int main(int argc, char *argv[])
{
CoList<int> list;
for(int i = 0; i < 10; i++)
list.enqueue(i);
cout << "Count: " << list.count() << endl;
for(int i = 0; i < 10; i++)
cout << "Item: " << list.dequeue() << endl;
cout << "Count: " << list.count() << endl;
int wait = 0;
cin >> wait;
}
As you can see it is a very simple Queue implementation using a linked list...
The definitions of function templates(including member functions of class templates) must be in the .h file so that they are present in every cpp file in which they are used. That's how templates work. You cant put the definitions into a cpp file. Technically, there is an export keyword which enables this but since almost no implementation supported it it was removed in the new standard.
Read this: The inclusion model
template definitions should be visible to the code which is using it. For that,
Put all the definitions in ".h" file
Put the definitions in ".cpp" file (for the code separation) and
#include that ".cpp" file
For example, in your case you can #include "CoList.cpp" instead of "CoList.h". And so on.
Consider a template function that takes T and performs modulus (%), or a simple addition (+) for that matter.
template <class T>
T GetT(T t1, T t2)
{
return t1%t2;
}
You see NO ERROR in this code. Alright. When I pass two ints it gets compiled:
GetT(10,20);
But when I pass float/double it WONT compile:
GetT(10.6, 20.5);
Compiler will emit: error C2296: '%' : illegal, left operand has type 'double' and other related errors.
The point is that template code doesn't get compiled until you instantiate it at least once for a particular data type. The template code stays junk - compiler doesn't care what actually inside the code. In your case the CPP is nothing but a text-file the compiler has ignored - All of it.
Having said that, when I use operator +, instead of operator % it would work for all basic data-types, but not for classes that are missing operator +. In that case compiler will compile again the template-stuff for that data-type (the class).
There are cases where compiler and linker work together to reduce final binary code size, when they see some code is duplicate and would be same for all/several data-types. But that's a different case.
This is straight from Nicolai Josutis's legendary book,
C++ Templates: A complete Guide
Templates are compiled twice:
Without instantiation, the template itself is checked for correct syntax, Syntax errors such as semicolon are discovered here.
As the time of instantiation, the templates code is checked to ensure that all calls are valid. Invalid calls are discovered such as unsupported function calls.
This leads to an important problem in the handling of templates practice. When a function template is used in a way that it triggers instantiation, a compiler will (at some point) need to see that template definition. This breaks the usual compile and link distinction for functions, when declaration of function is sufficient to compile the its use.
Thus, For a template the declaration and definition should be kept in the same header file so that they are visible in every cpp which uses them.