0

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.

Aditya Sehgal
  • 2,867
  • 3
  • 27
  • 37

2 Answers2

2

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

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
0

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

Community
  • 1
  • 1
MasterPlanMan
  • 992
  • 7
  • 14
  • @AdityaSehgal this won't fix everything. Your code is wrong, see my answer. – juanchopanza Aug 14 '13 at 16:50
  • Juan point is making a valid point - so you better consider what he just said. The code above is error prone in a definition file (I'm pretty sure I've seen that kind of use of namespace in the std for patching namespace conflicts). The code is still valid though. – MasterPlanMan Aug 14 '13 at 17:15
  • @AdamGalarneau Right, it would lead to a linker error if any attempt to use `LLL_NWK::lllInit()` is made. – juanchopanza Aug 14 '13 at 18:01
  • @AdamGalarneau : thanks for the info. have made the necessary changes. – Aditya Sehgal Aug 14 '13 at 20:24