I got this C++ code:
Client.h:
#include <unordered_map>
using namespace std;
class Client{
static unordered_map<int, Client*>* clients;
public:
static void initializeClients();
}
Client.cpp
#include "Client.h"
#include <unordered_map>
using namespace std;
void Client::initializeClients(){
clients = new unordered_map<int, Client*>();
}
But the linker gives me a LNK2001 unresolved external symbol for the unordered_map. I have no idea of what I am doing wrong, but it seems that I am missing something. I am using Visual Studio 2013.
Any idea? Thank you in advice!
you need to declare clients in your cpp file :
#include "Client.h"
#include <unordered_map>
using namespace std;
unordered_map* Client::clients;
void Client::initializeClients(){
clients = new unordered_map<int, Client*>();
}
explenation:
header file only tells the linker what kinds of symbols some cpp file(s) has. declaring a variable in the header file does not makes this variable appear by it self. here you need to instantiate the static map pointer in the cpp file by declaring it , and make other files know about him by specifing it in the header file
summery :
static member variables and global variables need to be declared also in some cpp file in order to get instantiated
Related
I have multiple C++ classes in a Pacman Game project (Maze, Food, Pacman, ...). I made a namespace 'Pacman_AI' so that the classes can be seen every where in the project. However, I got an error for 'GameObject' class: "name followed by '::' must be a class or namespace name".
Here is my "GameObject.cpp" in which I get the error above:
namespace Pacman_AI{
vector <pair <int, int> > GameObject::getPoints(){
return points;
}
string GameObject::getType(){
return type;
}
}
I already defined my "GameObject" class in a different file "GameObject.h".
Your help is highly appreciated.
The simplest thing is to use namespace in your header files
// Foo.h
namespace Pacman_AI{
// STUFF HERE
};
and then using namespace in your .cpp files
//Foo.cpp
#include "Foo.h"
using namespace Pacman_AI;
// define your functions here
I am having some trouble with the implementation of a constructor and I cannot figure out what is the problem. I have spent a lot of time with this problem: it would be grateful some kind of advice.
The main code is:
#include "stdafx.h"
#include "Rocket.h"
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
Rocket* rocket;
rocket=new Rocket();
//LLA* position=rocket->positionLLA;
return 0;
}
Rocket.h
#pragma once
#include "LLA.h"
class Rocket {
public:
Rocket(); // Default constructor
LLA* positionLLA;
};
Rocket.cpp
#include "stdafx.h"
#include "Rocket.h"
#include <iostream>
using namespace std;
Rocket::Rocket() // Default constructor
{
// Initialise the position
positionLLA=new LLA();
}
The error says:
error LNK2019: unresolved external symbol "public: __thiscall Rocket::Rocket(void)" (??0Rocket##QAE#XZ) referenced in function _main.
I know the error has something to do with not having declared a variable, but I think I have declared all classes and constructors.
PS: I am using Visual Studio 2008 in order to add dependencies.
I am assuming LLA is correctly defined in your .h file
Are you compiling Rocket.cpp into Rocket.o and main.cpp into main.o and then linking the two object files together?
Your error seems to imply that the linker cannot obtain symbol information from Rocket.o, which usually means Rocket.o was not found
Oh, as a detail, since Rocket is using an LLA*, you don't need to include LLA.h in Rocket.h, you can simply forward-declare
class LLA;
You might want to add #ifndef, #define, and #endif to your header files. Having multiple includes of the same thing in different files can lead to complications.
I am new to C++. I had an unresolved external symbol error while using vectors and didn't know what was going wrong so I've replicated it here.
I am using MS Visual Studio 2011. The exact error is:
error LNK2001: unresolved external symbol "class std::vector > abc"
(?abc##3V?$vector#VmyClass##V?$allocator#VmyClass###std###std##A)
I have my class declaration in myClass.h:
#ifndef __MYCLASS__
#define __MYCLASS__
class myClass{
public:
int var;
myClass(void);
myClass (int k);
};
#endif
and my class definition in myClass.cpp:
#include"myClass.h"
myClass::myClass(void){
var=0;
}
myClass::myClass (int k){
var=k;
}
header.h :
ifndef __HEADER__
#define __HEADER__
#include<iostream>
#include<vector>
#include"myClass.h"
using namespace std;
extern std::vector<myClass> abc;
#endif
main.cpp :
#include <iostream>
#include <vector>
#include "myClass.h"
#include "header.h"
using namespace std;
int main(){
abc.push_back(myClass(5));
return 1;
}
This is where I get the unresolved external symbol error. Now I tried putting all of these in a single file and it compiled alright.
THE FOLLOWING FILE IS NOT INCLUDED IN THE ABOVE PROJECT.
#include<iostream>
#include<vector>
#include"myClass.h"
using namespace std;
class myClass{
public:
int var;
myClass(void){
var=0;
}
myClass (int k){
var=k;
}
};
int main(){
std::vector<myClass> abc;
abc.push_back(myClass(5));
return 1;
}
The solution has been given at What is an undefined reference/unresolved external symbol error and how do I fix it?
but I can't figure out how to implement it.
You do not have a definition for this vector:
extern std::vector<myClass> abc;
An extern declaration only tells the compiler that the object exists and it is defined somewhere. But you haven't defined it anywhere.
Add this at global namespace scope in one (and only one!) of your .cpp files:
std::vector<myClass> abc;
Actually, considering that you are not using abc from different translation units (i.e. .cpp files) you do not need the extern declaration at all. Just place your vector in main.cpp, since that is the only place where you are using it.
Also, avoid using directives, especially at namespace scope (since it easily leads to nasty name clashes with entities from the Standard Library):
using namespace std; // THIS IS BAD, DON'T DO IT
Considering that you are qualifying the names of entities from the std namespace already, you don't really need the above.
You declared abc as extern but you never provided definition for it.
Try add definition inside main.cpp:
#include <iostream>
#include <vector>
#include "myClass.h"
#include "header.h"
using namespace std;
std::vector<myClass> abc; //^^add this line
int main(){
abc.push_back(myClass(5));
return 1;
}
However, IMHO using extern here in your code seems useless. Meanwhile, I don't think it is good to name a header file as header.h.
You have abc as a local variable. It should be a global variable. Only then extern would work.
But if you only want to access it as a local variable from within main() and not from another compiled CPP file/object, then it is pointless to use an extern. The extern is only needed if the variable is global and to be accessed from another compiled CPP/object.
This has been stumping me for a while. I am trying to create a function that takes a hash table, and returns said hash table. However I am getting this error in the header file,
error: ‘string’ was not declared in this scope.
error: template argument 1 is invalid
Here is the header file itself:
#ifndef NAME_SPAWN_H
#define NAME_SPAWN_H
#include <QString>
#include <QHash>
#include <string>
class Name_Spawn
{
public:
Name_Spawn();
void initalize();
private:
QString int_2_str(int);
void seed();
QHash<string,QString> setTable(QHash<string,QString>);
};
#endif // NAME_SPAWN_H
As you can see, string has been declared. Any ideas? I am at my wits end.
The real name of string is std::string. Try using that instead.
(You can leave off the std:: qualifier only if there's a using namespace std; directive in scope. But it's a good habit not to put using namespace std; in header files.)
I was doing a project for computer course on programming concepts. This project was to be completed in C++ using Object Oriented designs we learned throughout the course. Anyhow, I have two files symboltable.h and symboltable.cpp. I want to use a map as the data structure so I define it in the private section of the header file. I #include <map> in the cpp file before I #include "symboltable.h".
I get several errors from the compiler (MS VS 2008 Pro) when I go to debug/run the program the first of which is:
Error 1 error C2146: syntax error : missing ';' before identifier 'table' c:\users\jsmith\documents\visual studio 2008\projects\project2\project2\symboltable.h 22 Project2
To fix this I had to #include <map> in the header file, which to me seems strange.
Here are the relevant code files:
// symboltable.h
#include <map>
class SymbolTable {
public:
SymbolTable() {}
void insert(string variable, double value);
double lookUp(string variable);
void init(); // Added as part of the spec given in the conference area.
private:
map<string, double> table; // Our container for variables and their values.
};
and
// symboltable.cpp
#include <map>
#include <string>
#include <iostream>
using namespace std;
#include "symboltable.h"
void SymbolTable::insert(string variable, double value) {
table[variable] = value; // Creates a new map entry, if variable name already exist it overwrites last value.
}
double SymbolTable::lookUp(string variable) {
if(table.find(variable) == table.end()) // Search for the variable, find() returns a position, if thats the end then we didnt find it.
throw exception("Error: Uninitialized variable");
else
return table[variable];
}
void SymbolTable::init() {
table.clear(); // Clears the map, removes all elements.
}
My guess is that you have another file that includes the header file #include "symboltable.h". And that other source file doesn't #include <map> nor #include <string> nor has using namespace std before it includes "symboltable.h".
Check which file is being compiled when you get the error. Is it maybe a different source file than the .cpp that you mentioned? Possibly something like main.cpp?
Another way to solve your problem is to put the includes you need in your header file and use std::map instead of simply map. Also you use string which is also inside the namespace std. So that needs to be std::string. And put the missing #include <string>.
Yes, you indeed have to #include <map> in the header file.
You use map in the declaration of the class, so the compiler needs to know what this map refers to. Since the definition of it is in <map> you need to include that header before using the map template class.
You could also instead #include <map> in every source file before the #include "symboltable.h" line, but usually you would just include these kind of prerequisites in the header.