我正在制作一个命名空间来帮助我调试程序,但是我遇到了一个问题,弄清楚如何结构所有内容并在没有问题的情况下构建它。

问题描述 投票:0回答:1

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

在我得到

未定定义的助手::logfile
错误时,我完全理解的是,这是因为我使用了外部关键字。 

没有外部关键字,我会遇到不同的错误:helper ::logfile

的多数定义。该错误报告为第一个定义。有任何东西。
我确定我在编译中构造了辅助代码,但我不知道该怎么做?

您需要像您一样declare the标题中的变量,以便在需要的任何地方提供该名称。 include "Helper.h" 您需要在源文件中定义它;一个定义规则要求您完全具有一个定义。 // Helper.h extern std::ofstream logfile;

没有定义,该变量不存在,因此“未定义的参考”错误。

具有标题中的定义,它是在包含标头的每个翻译单元中定义的,因此“多个定义”错误。

具有一个源文件中的定义,它已定义一次,链接器很高兴。

c++ header namespaces declaration
1个回答
2
投票
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.