Namespace compilation issues - c++

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.

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

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

Related

C++ Xcode Library - Undeclared math.h features

I have recently attempted to switch my C++ project's target from an executable to a dynamic library (.dylib), and as soon as I rebuilt with the new target, I get a few errors saying that some defines and declarations in math.h are undeclared, such as M_PI and the sqrt() function.
The error message is as follows: error: use of undeclared identifier 'M_PI', and error: use of undeclared identifier 'sqrt'
This only occurs when I am building my project as a library, and I cannot figure out why it is doing this.
If anyone can help me out on this, it would be much appreciated!
Edit:
Also, if I try to change my include to #include <cmath>, I get more errors, such as: No member named 'signbit' in the global namespace.
Make sure #include <cmath> is at the top of the code, above int main(). I had the same problem. moving the #include <cmath> below <iostream> and above int main() solved the problem for me.

WindowsRuntimeComponent runtime error

I am having problems creating a managed class with namespace in visual studio 2012 Windows Runtime Component of C++.
Below is the code:-
#pragma once
#include <string>
using namespace std;
namespace WindowsRuntimeComponent1
{
public ref class Class1 sealed
{
public:
Class1();
string getString(string desc);
};
}
i'm getting error at 'public' which it expected a declaration.
beside that, exception return by visual studio 2012 is error C2059:syntax error:'public', error C2143:syntax error:missing ';' before '{', error C2447:'{':missing function header (old-style-formal list?)
Can anyone help me solve this problem. Thank you.
oYou have to change the runtime compiler. Go to Project -> Properties -> General and change it to "Common Language Runtime Support to /clr".
EDIT: Well, there is no need to worry about all the compiler errors. Google is your friend! if you don't know how to fix it, just look for it. All errors are described in detail. But most of the errors are self-explanatory, e.g. "error C4703, potentially uninitialized local pointer variable used" says you have to initialize your var int *xxx=0;.

Strange C2065 error in extern library

I have an extern code written in C in my C++ project in MS VS 2010. It comes with .h and .c files and I use include like this:
extern "C"{
#include "Extern\libname.h"
}
It worked nice. Then I read Google C++ style guide and removed all entries of
using namespace std;
replaced them with just
std::whatINeed
everywhere.
And the error showed up -
error C2065: 'FILE' : undeclared identifier
in the header of extern library. And then I move cursor to the word "FILE" in that code, there is
tydef _iobuf FILE
What should I add into my code to fix it? I don't want to change extern code because it can be updated and I will be forced to add my fixes on every update. Also, I am sure that there is a simple bug caused by me.
add
#include <cstdio>
above you include.

cvCalcOpticalFlowHS not being recognised C++

I've recently installed OpenCV and everything so far has been working, but for some reason even if I include: cv.h, highgui.h I get this error when I want to use the function:
cvCalcOpticalFlowHS: error C2065: 'cvCalcOpticalFlowHS' : undeclared identifier.
I've tried to look up which header file needs to be included, but I've had no succes so far.
Do
#include "opencv2/legacy/legacy.hpp"

Can't declare C++ vector in xcode ios project

I'm trying to use a vector in a C++ class with xcode but it's giving me errors. The file has the .mm extension that is required for C++ files.
This is my code:
class Synth{
private:
int bpm;
std::vector<Note> notesList;
public:
};
It's giving me these two errors:
error: Semantic Issue: Use of undeclared identifier 'std'
error: Parse Issue: Expected member name or ';' after declaration specifiers
I also tried with using namespace std; on top but that made no difference.
Any ideas what could be causing this?
Yes, you need to include the header:
#include <vector>
Don't use using namespace std in a header file, rather keep your code as it is, with the explicit qualifier: std::vector.