Quantcast
Channel: Active questions tagged header - Stack Overflow
Viewing all articles
Browse latest Browse all 646

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

$
0
0

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:

#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.

Without the extern keyword I get a different error: multiple definition of 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.

I'm sure I am structuring the helper code wrong for compile but I can't figure out how I should be doing it?


Viewing all articles
Browse latest Browse all 646

Trending Articles