简体   繁体   English

名称空间外部变量,c ++结构问题的未定义引用/多个定义

[英]Undefined Reference/ Multiple Definition for namespace extern variable , c++ structuring issue

I am making a namespace to help me debug a program but I'm having a problem figuring out how structure everything and get it to build without issue. 我正在创建一个名称空间来帮助我调试一个程序,但是我在弄清楚如何构建所有内容并让它构建没有问题时遇到了问题。

Here's my current header: 这是我当前的标题:

#ifndef HELPER_H
#define HELPER_H
#include <string>
#include <fstream>
#include <sstream>

namespace Helper
{
    enum LOG { ONSCREEN, OFFSCREEN };
    extern std::ofstream logfile;
    //std::ofstream logfile("log.txt", std::ios_base::out | std::ios_base::app );


    void EnableLogging();
    void Log(std::string s, LOG type);

    template <class T>
    std::string ToString(const T& t)
    {
        std::ostringstream sstr;
        sstr << t;
        return sstr.str();
    }
}

#endif // HELPER_H

Here's the Helper cpp file: 这是Helper cpp文件:

#include "Helper.h"
#include <cstdio>

void Helper::EnableLogging()
{
    #ifdef WIN32
        // To use console on pc
    std::ofstream ctt("CON");
    freopen("CON", "w", stdout);
    freopen("CON", "w", stderr);
    #endif
    #ifdef GP2X
        //To log to text file on the caanoo
    logfile.open("log.txt", std::ios_base::out | std::ios_base::app );
    #endif

}

void Helper::Log(std::string s, LOG type)
{
    if(type == OFFSCREEN)
    {
        #ifdef GP2X
        //log << "L" << __LINE__ << "|T" << SDL_GetTicks() << "| " << s << std::endl;
        logfile << s << std::endl;
        #endif
        #ifdef WIN32
        printf("%s",s.c_str());
        #endif
    }
}

At the moment I am getting an undefined reference to Helper::logfile error, which I completely understand is because I've used the extern keyword. 目前我得到一个未定义的Helper :: logfile错误引用 ,我完全理解这是因为我使用了extern关键字。

Without the extern keyword I get a different error: multiple definition of Helper::logfile . 如果没有extern关键字,我会得到一个不同的错误: Helper :: logfile的多重定义 The error is reported as 'first defined..' in another source file that I am trying to include "Helper.h" in. The line number that error is reported on is a constructor within said source file but I suspect that has little to do with anything. 该错误报告为另一个源文件中的“first defined ..”我试图include "Helper.h" 。报告错误的行号是所述源文件中的构造函数但我怀疑没有做任何事。

I'm sure I am structuring the helper code wrong for compile but I can't figure out how I should be doing it? 我确定我正在编译帮助代码错误的编译但我无法弄清楚我应该怎么做呢?

You need to declare the variable in the header, as you do, to make the name available wherever its needed. 您需要像标题一样在标头中声明变量,以便在需要的地方使名称可用。

// Helper.h
extern std::ofstream logfile;

You need to define it in the source file; 您需要在源文件中定义它; the One Definition Rule requires that you have exactly one definition. 单一定义规则要求您只有一个定义。

// Helper.cpp
std::ofstream Helper::logfile("log.txt", std::ios_base::out | std::ios_base::app );

With no definition, the variable doesn't exist, hence the "undefined reference" error. 没有定义,变量不存在,因此“未定义的引用”错误。

With a definition in the header, it's defined in every translation unit that includes the header, hence the "multiple definition" error. 通过标题中的定义,它在包含标题的每个翻译单元中定义,因此出现“多重定义”错误。

With a definition in one source file, it's defined once, and the linker is happy. 在一个源文件中定义,它定义一次,链接器很高兴。

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

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