There are a number of similar questions, in fact I composed the following code from several other posts. Unfortunately I still have one error I can't seem to crack - and although I did a lot of c++ development that was 15 years ago.
I want to make a simple static look-up table using a map.
Here is the code so far (the code css seems to not render it very well):
enum RegionCodeEnum
{
One,
Two,
Three
};
enum DeviceCodeEnum
{
AAA,
BBB,
CCC
};
class LookupTable
{
friend class constructor;
struct constructor
{
constructor()
{
table[One] = AAA;
table[Two] = AAA;
table[Three] = CCC;
}
};
static constructor cons;
public:
LookupTable(void);
static DeviceCodeEnum GetDeviceFromRegion(RegionCodeEnum RegionCode);
private:
static map<RegionCodeEnum, DeviceCodeEnum> table;
};
LookupTable::constructor LookupTable::cons;
LookupTable::LookupTable(void)
{
}
DeviceCodeEnum LookupTable::GetDeviceFromRegion(RegionCodeEnum RegionCode)
{
return table[RegionCode];
}
From else where in the code I have this code:
DeviceCodeEnum code= LookupTable::GetDeviceFromRegion(One);
The compile error I get is:
error LNK2001: unresolved external symbol "private: static class std::map<enum RegionCodeEnum,enum DeviceCodeEnum,struct std::less<enum DeviceCodeEnum>,class std::allocator<struct std::pair<enum RegionCodeEnum const ,enum DeviceCodeEnum> > > LookupTable::table" (?table#LookupTable##0V?$map#W4RegionCodeEnum##W41#U?$less#W4DeviceCodeEnum###std##V?$allocator#U?$pair#$$CBW4DeviceCodeEnum##W41##std###3##std##A) C:\_dev\temp\test\main.obj Refactor01
Any thoughts?
You are missing the definition for table. Somewhere in your code it should say:
map<RegionCodeEnum, DeviceCodeEnum> LookupTable::table;
just as you did for constructor cons.
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.
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.
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.
I've written this beauty:
#include <iostream>
struct something {
static const char ref[];
};
const char something::ref[] = "";
template<int N, const char(&t_ref)[N], typename to> struct to_literal {
private:
static to hidden[N];
public:
to_literal()
: ref(hidden) {
for(int i = 0; i < N; i++)
hidden[i] = t_ref[i];
}
const to(&ref)[N];
};
template<int N, const char(&ref)[N], typename to> const to* make_literal() {
return to_literal<N, ref, to>().ref;
}
int main() {
const wchar_t* lit = make_literal<sizeof(something::ref), something::ref, wchar_t>();
}
It somewhat cleanly converts between string literal types. But when I compile it, MSVC says that the make_literal function is an undefined external function- which is clearly untrue as it's defined right there.
Edit: I've managed to reduce the problem down without all of the template gunk.
struct some {
friend int main();
private:
static wchar_t hidden[40];
public:
some()
{
}
};
int main() {
std::cout << some::hidden;
//const wchar_t* lit = make_literal<sizeof(something::ref), something::ref, wchar_t>();
}
main.obj : error LNK2001: unresolved external symbol "private: static wchar_t * some::hidden" (?hidden#some##0PA_WA)
It's just a static array. Does life hate me?
The issue is that is that to_literal::hidden is declared but never defined. Take another look:
struct something {
static const char ref[]; // declaration of something::ref
};
const char something::ref[] = ""; // definition of something::ref
template<int N, const char(&t_ref)[N], typename to> struct to_literal {
private:
static to hidden[N]; // declaration of to_literal::hidden (but there's no
// definition anywhere)
public:
to_literal()
: ref(hidden) {
for(int i = 0; i < N; i++)
hidden[i] = t_ref[i];
}
const to(&ref)[N];
};
To fix this, add a proper definition of to_literal::hidden:
template<int N, const char(&t_ref)[N], typename to>
to to_literal<N, t_ref, to>::hidden[N]; // definition of to_literal::hidden
When you define static members, a declaration does not suffice. You must provide a definition outside the class. I.e. add
wchar_t some::hidden[40];
outside the class, and it'll be defined.
Otherwise, if C++ allowed this, it'd cause the same problem as defining a global variable in a header -- every .cpp file that includes it will come with a duplicate definition, and at link time you'd get a multiply-defined symbol error.
You're declaring but not defining the static member. Add something like...
template<int N, const char(&t_ref)[N], typename to>
to to_literal<N, t_ref, to>::hidden[N];
I tried to check in MSVC for you too, but with VS2005 I get another stupid error...
template<int N, const char(&t_ref)[N], typename to>
to to_literal<N, t_ref, to>::hidden[N];
...compiler complains of...
error C3860: template argument list following class template name must list parameters in the order used in template parameter list
Looks like when they fix one bug, there's another one behind it ;-/.
When I built this with VC 2008, that wasn't the error I got. The error was:
Error 1 error LNK2001: unresolved
external symbol "private: static
wchar_t * to_literal<1,&public: static
char const * const
something::ref,wchar_t>::hidden"
(?hidden#?$to_literal#$00$1?ref#something##2QBDB_W##0PA_WA) main.obj Enabler
Removing static from the to hidden[N]; member resolved the issue.
Are you sure you got the error message correct?
My goal is to create a system wherein I can provide the string name of an class at run time and have it return an instance of that class in turn.
Searching stackoverflow, I came across an example that seems to do exactly what I am trying to accomplish, although I am currently unable to have it compile properly. The following is based on that code:
//LevelObject.h
#pragma once
#include <map>
#include <string>
class LevelObject
{
protected:
int ID;
public:
template<class T> static LevelObject* createT(void)
{
return new T(0);
}
LevelObject(void);
~LevelObject(void);
};
struct BaseFactory
{
typedef std::map<std::string, LevelObject*(*)()> map_type;
static LevelObject* createInstance(const std::string& s)
{
map_type::iterator it = getMap()->find(s);
if(it == getMap()->end())
{
return 0;
}
return it->second();
}
private:
static map_type* objectMap;
protected:
static map_type* getMap()
{
if(!objectMap)
{
objectMap= new map_type;
}
return objectMap;
}
};
template<class T>
struct DerivedRegister : BaseFactory
{
DerivedRegister(const std::string& s)
{
getMap()->insert(std::make_pair( s, &LevelObject::createT<T> ));
}
};
//Item.h
#pragma once
#include "LevelObject.h"
class Item :
public LevelObject
{
int ID;
static DerivedRegister<Item> reg;
public:
Item(int id);
~Item(void);
};
//Item.cpp
#include "Item.h"
Item::Item(int id)
{
ID = id;
}
Item::~Item(void)
{
}
DerivedRegister<Item> Item::reg("item");
The logic is that the derived objects, i.e. Item, will register a string and reference to a function that returns an instance of itself. On calling createInstance, it will take in a user inputted string and use the map to determine the object to return.
Unfortunately, this code is not compiling correctly, and gives me the following errors:
Error 1 error C2752:
'std::tr1::_Remove_reference<_Ty>' :
more than one partial specialization
matches the template argument list
Error 2 error C2528: 'abstract
declarator' : pointer to reference is
illegal c:\program files\microsoft
visual studio
10.0\vc\include\type_traits 965
Error 3 error C2528: 'type' : pointer
to reference is illegal c:\program
files\microsoft visual studio
10.0\vc\include\type_traits 349
If someone can help smooth out these errors, I would greatly appreciate it.
Or perhaps I am going about this entirely wrong in the first place, so if someone instead feels that I should be going in a different direction entirely please let me know.
Thanks in advance.
It's been a long time since this question was posted, but since there's no answer and I stumbled here too, I figured I'd add one. I copied the same factory code you did (from the StackOverflow answer here) and had the same problem. I found the solution at this StackOverflow answer.
It turns out Visual Studio 2010 (which I'm assuming you're using) has a problem with std::make_pair. Just use std::pair<std::string,LevelObject*(*)()> instead and you'll be good to go. At least that resolved this exact same problem for me.
I added empty bodies to the LevelObject class constructor and destructor:
LevelObject(void) { }
~LevelObject(void) { }
Then declared the static map member variable of the BaeFactory class:
BaseFactory::map_type* BaseFactory::map;
and the code compiled without errors in both GCC and Visual Studio.