Singleton导致链接器错误:"已定义"

时间:2015-02-13 15:12:24

标签: c++ visual-studio-2013

我写了一个管理当前执行会话的简单类。因此,我决定使用单例模式,但编译在链接步骤崩溃。这是错误:

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(单身人士)

#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=";
}

关于我如何调用单身人士的示例:

#include "../data/Session.h"

static Session* session = Session::Instance();

你能帮我解决一下错误吗?

提前感谢。

1 个答案:

答案 0 :(得分:1)

你的一个标题(你没有显示,但它包含在“ClientTsFrm.cpp”和“FrmSaveChat.cpp”中)定义了一个名为“session”的变量Session*

其他错误是由于您忘记定义Session的大多数静态成员而引起的。