1>game.obj : error LNK2001: unresolved external symbol "public: bool __cdecl GameStore::loadFromXml(void)" (?loadFromXml#GameStore##QEAA_NXZ)
1>protocolgame.obj : error LNK2001: unresolved external symbol "public: struct StoreCategory * __cdecl GameStore::getStoreCategoryByName(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?getStoreCategoryByName#GameStore##QEAAPEAUStoreCategory##AEBV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z)
1>C:\Users\Slavi Dodo\Documents\GitHub\forgottenserver\vc14\x64\Release\theforgottenserver.exe : fatal error LNK1120: 2 unresolved externals
I am so confused! I don't even understand why the error comes.
These is the declaration "gamestore.h"
#ifndef FS_GAMESTORE_H_ADCAA356C0DB44CEBA994A0D678EC92D
#define FS_GAMESTORE_H_ADCAA356C0DB44CEBA994A0D678EC92D
struct StoreOffer {
StoreOffer(uint32_t id, uint32_t thingId, uint8_t addon, uint32_t price, uint8_t type, uint8_t state, std::string name, std::string description, std::vector<std::string> icons)
: id(id), thingId(thingId), addon(addon), price(price), type(type), state(state), name(name), description(description), icons(icons) {}
uint32_t id;
uint32_t thingId;
uint32_t price;
uint8_t type;
uint8_t state;
uint8_t addon;
std::string name;
std::string description;
std::vector<std::string> icons;
};
struct StoreCategory
{
StoreCategory(std::string name, std::string description, std::vector<std::string> icons, std::vector<StoreOffer> offers)
: name(name), description(description), icons(icons), offers(offers) {}
std::string name;
std::string description;
std::string parentCategory;
std::vector<std::string> icons;
std::vector<StoreOffer> offers;
};
class GameStore
{
public:
bool reload();
bool loadFromXml();
StoreOffer* getStoreItemById(uint32_t id);
StoreCategory* getStoreCategoryByName(const std::string& name);
const std::vector<StoreCategory>& getStoreCategories() const {
return storeCategories;
}
bool enabled;
std::vector<StoreCategory> storeCategories;
};
#endif
And this is the definitions "gamestore.cpp"
#include "otpch.h"
#include "gamestore.h"
#include "pugicast.h"
#include "tools.h"
bool GameStore::reload()
{
storeCategories.clear();
return loadFromXml();
}
bool GameStore::loadFromXml()
{
return true;
}
StoreOffer* GameStore::getStoreItemById(uint32_t id) {
for (auto it : storeCategories) {
for (auto item : it.offers) {
if (item.id == id) {
return &item;
}
}
}
return nullptr;
}
StoreCategory* GameStore::getStoreCategoryByName(const std::string& name) {
auto it = std::find_if(storeCategories.begin(), storeCategories.end(), [name](StoreCategory& storeCategory) {
return storeCategory.name == name;
});
return it != storeCategories.end() ? &*it : nullptr;
}
I have read more about the error, but not even found a solution to my situation.
As you see the "loadFromXml" is just returning true, not accepting any arguments as well being public, the problem is very confusing!
Check if gamestore.cpp is being compiled or not!
If not add it to projectname.vcproj in CLCompile list.
Related
#include <iostream>
class t1
{
public:
~t1();
static t1& fun();
private:
t1()
{
}
};
t1& t1::fun()
{
return t1();
}
int main()
{
t1::fun();
return 0;
}
I am getting unresolved external symbol. please help. the errors are below
Error 2 error LNK2019: unresolved external symbol "public: __thiscall t1::~t1(void)" (??1t1##QAE#XZ) referenced in function "public: static class t1 & __cdecl t1::fun(void)" (?fun#t1##SAAAV1#XZ) D:\LXI\LXIRef\RefDesign_V01.00\Software\Solution\TestWebServer\TestWebServer.obj TestWebServer
Error 3 error LNK1120: 1 unresolved externals D:\LXI\LXIRef\RefDesign_V01.00\Software\Solution\Debug\TestWebServer.exe 1 1 TestWebServer
Give definitions to constructor and destructor.
#include <iostream>
class t1
{
public:
~t1() {} // <<<< defined here
static t1& fun();
private:
t1() {} // << defined here
};
t1& t1::fun()
{
return t1();
}
int main()
{
t1::fun();
return 0;
}
I declared the the following class:
#pragma once
#include <stdio.h>
#include <vector>
#include <string>
namespace util
{
class FileReader
{
public:
FileReader();
~FileReader();
bool open(const std::wstring& name);
void close();
bool read(std::vector<char>& buf, __int64 startFrom, int size);
__int64 size() const;
private:
FILE* m_file;
std::wstring m_name;
__int64 m_size;
};
}
And its implementation:
#include "FileReader.hpp"
namespace util
{
bool FileReader::open(const std::wstring& name)
{
if (!name.empty() && (m_name != name))
{
close();
if (_wfopen_s(&m_file, name.c_str(), L"rb") == 0)
{
m_name = name;
// Get the file size
_fseeki64(m_file, 0, SEEK_END);
m_size = _ftelli64(m_file);
rewind(m_file);
}
else
{
m_file = NULL;
}
}
return (m_file != NULL);
}
// ....
}
in a seperate library and use it like this:
FileTransfer.cpp
#include <util/FileReader.hpp>
// .....
if (!m_fileReader.open(m_localFileName)) // std::wstring m_localFileName;
{
::MessageBoxA(NULL, "Failed to open file", "Error", MB_ICONERROR);
stopFileTransmission();
return;
}
in another project. Both projects compile successfully, but the FileTransfer.obj fails to link:
Error 2 error LNK2019: unresolved external symbol "public: bool
__thiscall util::FileReader::open(class std::basic_string,class
std::allocator > const &)"
(?open#FileReader#util##QAE_NABV?$basic_string#GU?$char_traits#G#std##V?$allocator#G#2##std###Z)
referenced in function
__catch$?onRequestDirClicked#FileTransferWindow##AAEXXZ$0 C:\Users\x\Documents\dev\Server\FileTransfer.obj Server
I remember it was working when I used std::string, so I assume it has something todo with std::wstring.
Any idea what could be the issue?
It seems, the problem was that the two projects had different values for the setting
Treat wchar_t as built-in type
Setting it to No (/Zc:wchar_t-) for both projects, solved the linker error. I still don't really know what the consequences will be though.
try using extern "C" to declare the open function.
I wrote a simple class which manages the current execution session. Therefore, I decided to use a singleton pattern but the compilation crashes at linked step. This is the error:
error LNK2005: "class Session * session" (?session##3PAVSession##A) already defined in ClientTsFrm.obj
(...)client\FrmSaveChat.obj TeamTranslate
error LNK2005: "class Session * session" (?session##3PAVSession##A) already defined in ClientTsFrm.obj
(...)client\Login.obj TeamTranslate
error LNK2019: unresolved external symbol "protected: __thiscall Session::Session(void)" (??0Session##IAE#XZ)
referenced in function "public: static class Session * __cdecl Session::Instance(void)" (?Instance#Session##SAPAV1#XZ)
(...)client\Session.obj TeamTranslate
error LNK2001: unresolved external symbol "private: static char const * const Session::_nick" (?_nick#Session##0PBDB)
(...)client\Session.obj TeamTranslate
error LNK2001: unresolved external symbol "private: static char const * const Session::_service" (?_service#Session##0PBDB)
(...)client\Session.obj TeamTranslate
error LNK2001: unresolved external symbol "private: static char const * const Session::_serverAddress" (?_serverAddress#Session##0PBDB)
(...)client\Session.obj TeamTranslate
error LNK2001: unresolved external symbol "private: static char const * const Session::_googleAPI" (?_googleAPI#Session##0PBDB)
(...)client\Session.obj TeamTranslate
error LNK2001: unresolved external symbol "private: static char const * const Session::_language" (?_language#Session##0PBDB)
(...)client\Session.obj TeamTranslate
error LNK2001: unresolved external symbol "private: static int Session::_numbLanguageSelected" (?_numbLanguageSelected#Session##0HA)
(...)client\Session.obj TeamTranslate
error LNK2001: unresolved external symbol "private: static char const * const Session::_translationEngine" (?_translationEngine#Session##0PBDB)
(...)client\Session.obj TeamTranslate
error LNK1120: 8 unresolved externals
(...)client\bin\TeamTranslate.exe TeamTranslate
IntelliSense: identifier "wxZipStreamLink" is undefined
(..)\include\wx\zipstrm.h 417 5
session.h (singleton)
#ifndef __SESSION_H__
#define __SESSION_H__
#include <cstring>
#include <stdio.h>
class Session {
public:
static Session* Instance();
void setNick(char* nick);
const char* getNick();
void setService(char* serv);
const char* getService();
void setLanguage(char* lang);
const char* getLanguage();
const char* getServerAddress();
void setServerAddress(char *sv);
void setGoogleAPIKey(char* code);
const char* getGoogleAPIKey();
void setNumbLanguageSelected(int v);
int getNumbLanguageSelected();
const char* Session::getTranslationEngine();
void Session::setTranslationEngine(char *sv);
char* Session::getGoogleURLTranslation();
void update();
bool read();
protected:
Session();
private:
static Session* _instance;
static const char* _nick; // client nickname
static const char* _service; // service used to translation (Google, Bing,....)
static const char* _serverAddress;
static const char* _googleAPI;
static const char* _language;
static int _numbLanguageSelected;
static const char* _translationEngine;
};
#endif
Session.cpp
#include "Session.h"
Session* Session::_instance = 0;
Session* Session::Instance() {
if (_instance == 0) {
_instance = new Session;
_instance->read();
}
return _instance;
}
void Session::setGoogleAPIKey(char* code){
_googleAPI = strdup(code);
}
const char* Session::getGoogleAPIKey(){
return _googleAPI;
}
void Session::setLanguage(char* lang){
_language = strdup(lang);
}
const char* Session::getLanguage(){
return _language;
}
void Session::setNick(char* nick){
_nick = strdup(nick);
}
const char* Session::getNick(){
return _nick;
}
void Session::setService(char* serv){
_service = strdup(serv);
}
const char* Session::getService(){
return _service;
}
const char* Session::getServerAddress(){
return _serverAddress;
}
void Session::setServerAddress(char *sv){
_serverAddress = strdup(sv);
}
void Session::setNumbLanguageSelected(int v){
this->_numbLanguageSelected = v;
}
int Session::getNumbLanguageSelected(){
return _numbLanguageSelected;
}
const char* Session::getTranslationEngine(){
return _translationEngine;
}
void Session::setTranslationEngine(char* sv){
_translationEngine = strdup(sv);
}
void Session::update(){
FILE* config = fopen("conf\\config.txt", "w");
fprintf(config, "%s\n", _serverAddress);
fprintf(config, "%s\n", _nick);
fprintf(config, "%d\n", _numbLanguageSelected);
fprintf(config, "%s\n", _language);
fprintf(config, "%s", _translationEngine);
fflush(config);
fclose(config);
}
bool Session::read(){
FILE * config;
if (config = fopen("conf\\config.txt", "r"))
{
fscanf(config, "%s", _serverAddress);
fscanf(config, "%s", _nick);
fscanf(config, "%d", _numbLanguageSelected);
fscanf(config, "%s", _language);
fscanf(config, "%s", _translationEngine);
fflush(config);
fclose(config);
return true;
} else
return false;
}
char* Session::getGoogleURLTranslation(){
return "https://www.googleapis.com/language/translate/v2?key=";
}
Example about how I call the singleton:
#include "../data/Session.h"
static Session* session = Session::Instance();
Can you help me to fix the errors?
thanks in advance.
One of your headers (which you didn't show, but it is included in both "ClientTsFrm.cpp" and "FrmSaveChat.cpp") defines a variable called "session" of type Session*.
The other errors are caused by your forgetting to define most static members of Session.
I have copied header file and cpp file from one of my old project( build in VS-2010) into new project( developing it in QT5.1) and have encounter this weird error
1>Message_list.obj : error LNK2019: unresolved external symbol "public: bool __thiscall Message_list::NoMessageTime::operator()(struct Message const &,struct Message const &)const " (??RNoMessageTime#Message_list##QBE_NABUMessage##0#Z) referenced in function "bool __cdecl std::_Debug_lt_pred<class Message_list::NoMessageTime,struct Message,struct Message>(class Message_list::NoMessageTime,struct Message &,struct Message const &,wchar_t const *,unsigned int)" (??$_Debug_lt_pred#VNoMessageTime#Message_list##UMessage##U3##std##YA_NVNoMessageTime#Message_list##AAUMessage##ABU3#PB_WI#Z)
I have no idea how to solve it.
In my header file
Message_list.h
#include <set>
#include <QSTRING>
#include "Message.h"
class Message_list
{
class NoMessageTime
{
public:
bool operator ()( const Message& left, const Message& right ) const;
};
typedef std::multiset<Message, NoMessageTime> MessageSet;
public:
void add( const Message& message );
private:
/** Inserts an item into the correct place in the list
*/
void Message_list::insert_item( const Message& message );
MainWindow* dialog_;
MessageSet messages_;
};
Message_list.cpp
void Message_list::addd( const Message& message )
{
QMutex mutex;
mutex.lock();
// Do something here
messages_.insert( message ); // error comes here
mutex.unlock();
}
Same files compiled successfully in vs-2010..
I'd like to pass my 2D Array of class Menza into function..
class Menza
{
public:
string PrintLunch() const {return Lunch;};
unsigned int PrintID() const {return ID;};
double PrintPrice() const {return Price;};
double PrinteValue() const {return eValue;};
string PrintDescription() const {return Description;};
void ChangeLunch(string Change) {Lunch = Change;};
void ChangePrice(double Change) {Price = Change;};
void ChangeID(int Change) {ID = Change;};
void ChangeeValue(double Change) {eValue = Change;};
void ChangeDescription(string Change) {Description = Change;};
private:
string Lunch;
double Price;
unsigned int ID;
string Description;
double eValue;
};
const int Lunches = 5;
void LoadFile(bool FileChoice,Menza (*InputFromFile)[Lunches]);
void CustomerSelection(Menza CustomerSelect[],Menza (*InputFromFile)[Lunches]);
int main()
{
Menza InputFromFile[Lunches][Lunches];
Menza CustomerSelect[Lunches];
bool FileChoice = false;
LoadFile(FileChoice,InputFromFile);
CustomerSelection(CustomerSelect,InputFromFile);
}
Once I compile this, it shows me:
Semestralka.obj : error LNK2019: unresolved external symbol "void __cdecl LoadFile(bool,class Menza (*)[5])" (?LoadFile##YAX_NPAY04VMenza###Z) referenced in function _main
1>E:\My VSB\ZP projekty\Semestralka\Debug\Semestralka.exe : fatal error LNK1120: 1 unresolved externals
Can someone explain me whats wrong in this function call?
Thanks
You don't have definition of LoadFile function, only declaration. Therefore compiler have no way to understand what this function should do.
You must define it or link a library where it is defined (and include a header from this library). (Same is true for CustomerSelection too).
Read more about difference between definition and declaration here: declare_vs_define