简体   繁体   English

单例导致链接器错误:“已定义”

[英]Singleton causing linker errors: “already defined”

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) session.h(单例)

#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 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* . 您的标题之一(未显示,但同时包含在“ ClientTsFrm.cpp”和“ FrmSaveChat.cpp”中)定义了一个名为Session*的变量“ session”。

The other errors are caused by your forgetting to define most static members of Session . 其他错误是由您忘记定义Session大多数静态成员引起的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM