Namespace compilation issues

时间秒杀一切 提交于 2019-12-11 19:24:10

问题


I am new to working in Visual Studio (am using version 2005). I am running into a problem with namespaces that I am not able to figure out.

I am trying to create a static library which I will link to an Application later.

So, I have a XXX.h file with the following code

#ifndef _XXX_X_H
#define _XXX_X_H

namespace LLL_NWK
{
   void lllInit();
}
#endif

I include XXX.h in XXX.c and the code looks like

#include "XXX.h"

using namespace LLL_NWK;

void lllInit()
{
}

However, when I build the Library I encounter the following errors

error C2061: syntax error : identifier 'LLL_NWK'
error C2059: syntax error : ';'
error C2449: found '{' at file scope (missing function header?)
error C2059: syntax error : '}'

I am unable to figure out the cause of this error. Would appreciate some help and pointers.


回答1:


First, using namespace LLL_NWK is not appropriate here. You are declaring and defining a function void lllInit() outside of namespace LLL_NWK. You need to place the definition inside of the namespace, which can be done like this:

void LLL_NWK::lllInit()
{
}

or like this:

namespace LLL_NWK
{
   void lllInit()
   {
   }
}

Second, make sure you compile the code as C++.




回答2:


That code is not supported by the C compiler - makes sure to rename the filename to .cpp instead .c. In this case, namespace is not supported. See this post: Namespaces in C



来源:https://stackoverflow.com/questions/18237144/namespace-compilation-issues

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!