include STL header file inside a function C++14 - c++

tl;dr: Can I somehow make this code work in C++14 (GCC 6.3)?
int main(){
#include<vector>
std::vector<int> v{1,2,3};
return 0;
}
But code below works just fine!
#include <iostream>
using namespace std;
int main() {
#include<cstdio>
using namespace __gnu_cxx;
printf("Hello world\n.");
return 0;
}
Using C++14 (gcc-6.3) code doesn't compile with error message being
error: 'namespace' definition is not allowed here
namespace std
^~~~~~~~~
Why I want to do this?
I don't have access outside of the function where I am allowed to code. I can't #include in global area.
UPD: Changing to cstdlib also works problem is not exclusion by header guard (according to me) but namespace problem. Because C++ header files have namespace std, while c header files doesn't. I wanted to ask whether there is some tweak for namespace issue?

Can I somehow make this code work
No. Standard headers (and most library headers in general) must be included in the global namespace scope.
But code below works just fine!
But it's not guaranteed to work. It just happened to work, probably because <iostream> had already included <cstdio> and so your own inclusion was removed by header guards.

Related

C++ compilling errors when using Quadprog++ with Eigen together

this is my first question here, I've searched it all over for a long time yet no solution.
I'm using QUadprog++ to solve a quadratic problem. When I use it in a test alone, it was alright. But when I implement it into my project, which contains Eigen, the Eigen operations will have errors like "Matrix A has no member named ‘lu_inverse’". If I comment the header files of Quadprog++ (Array.hh and Quadprog++.hh) out, the errors just disappear. So I assume that it was a conflict error between the header files of Eigen and Quadprog++. Does anyone have some clue? Thanks in advance!
You can also switch to one of QuadProgpp versions which can work with Eigen types directly: https://github.com/asherikov/QuadProgpp, https://www.cs.cmu.edu/~bstephe1/eiquadprog.hpp; or try an alternative implementation of the same algorithm (also Eigen based) https://github.com/asherikov/qpmad.
if your using namespace quadprogpp; then dont. your different libraries have the same typenames and thats causing the errors you have. It may be a few more characters to type quadprogpp::someFunction(); but its worth it. This is also why you shouldn't ever put a using namespace in a header ever. Its because you pollute all files that include that header with the namespace symbols and name conflicts can ensue which is the same kind of error your having right now.
the Quadprog library is in it's own namespace.
#if !defined(_ARRAY_HH)
#define _ARRAY_HH
#include <set>
#include <stdexcept>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
namespace quadprogpp {
enum MType { DIAG };
template <typename T>
class Vector
notice how just after the #includes there is a decleration of namespace quadprogpp{} and everything that is defined in its enclosing brackets will be defined in scope to quadprogpp, so to use any of this library you have to prefix eveything with the namespace name. this is no different than using things from the standard library. I'm quite sure you've written the standard c++ hello world
#include<iostream>
int main()
{
std::cout << "hello world!" << std::endl;
return 0;
}
cout and endl being part of namespace std have to be prefixed with std:: to access them. Many new programmers to c++ dislike this and one of the very first things they google is how to not have to type out namespaces.
#include<iostream>
using namespace std;
int main()
{
cout << "hello world" << endl;
return 0;
}
the next thing new programmers often do is learn to place their definitions in header files and their program logic in cpp files. Thats when they commit the next common mistake.
#ifndef MYHEADER
#define MYHEADER
#include<string>
#include<vector>
#include<iostream>
using namespace std; //Never do this in a header.
doing that pollutes all of your code with everything in the standard library. That may seem like a trivial thing but when you start using another library or perhaps you create your own type that has the same name as things in the standard library that causes name collisions.
That's when the compiler simply cant reason about which Vector you want. But both Quadprog.hh and Array.hh in Quadprog++ are wrapped in namespace quadprogpp to specifically prevent name collision, which is the whole purpose of namespaces. So there is somewhere in your code, likely a header file, where you've made the statement of using namespace quadprogpp;, or some other namespace that defines an Array type, and the compiler can't deduce which type your referring to in your code.
Other than removing your using namespace statements you can also prefix a typename with its namespace qualifer to disambiguate which type your talking about. In your case I'm confident your Array should be declared as quadprogpp::Array arraynamme; rather than simply Array arrayname;

Using namespaces inside template implementation files

To keep class definition in header file clean, I decided to move implementations of templated functions into another *.h file, which I include in the main header. Now I would like to make use of using namespace there, to make code more readable.
But using namespaces would affect the whole application, since the file gets included in the header which itself gets included in the whole application. The using of the namespaces would spread out of the file.
How can I handle this?
You can put using namespace XX inside function definitions to scope the using declaration to that function:
int func( ...args... )
{
using namespace std;
// ... body of function
}
Use a namespace alias.
namespace submod_ = topspace::other_project::module::submodule;
Then you can use submod_ wherever you would require the very long namespace.
This requires you to still use submod_ where you would have the long namespace qualifier. In that sense, it doesn't exactly answer your question. But I would argue that the explicitness of the qualification aids readability and helps prevent mistakes.
There are real examples of StackOverflow questions where a "using" declaration brought in "lurking" functions which the code author did not realize.
You can place the using namespace in the namespace of your 'main header':
Header.h
#include <string>
namespace Test
{
using namespace std;
string f()
{
return "Test";
};
}
main.cpp
#include "Header.h"
int main()
{
Test::f();
string test; // Error: identifier "string" is undefined
std::string test;
return 0;
}

Global namespace is not checked in other namespaces

I'm trying to use namespaces in my code so I have a header file that looks like this :
#include <string>
namespace AppNamespace
{
class A
{
std::string name;
};
}
When I try to compile this, it says "'string' is not a member of AppNamespace::std". If I remove the std:: in front of string, or if I write ::std::string name, then it will compile fine.
This is of course a simplified example, I have many header files and not all of them show this behavior. I am not sure what can cause this, I thought that the compiler would always try the global namespace as well.
I am currently using Visual Studio 2012 if this matters.
This is of course a simplified example, I have many header files and not all of them show this behavior. I am not sure what can cause this, I thought that the compiler would always try the global namespace as well.
At some point you must have something like this:
namespace AppNamespace
{
#include <string> // or #include "my_header" which in turn includes <string>
class A
{
std::string name;
};
}
The #include directive does not respect namespaces. You need to move them all out to the global namespace scope, or each (possibly nested) inclusion of a standard header will cause undefined behavior in the form of creating a nested namespace std.
Using:
using namespace std;
#include <iostream>
#include "test_header.h"
int main() ...
The code compiles either way with your above example as a header.
Moving
using namespace std;
below the header file (in my case test_header.h) will cause it to fail if I don't use std::string.
Is that the problem you are having?

String was not declared?

#include <string.h>
using namespace std;
namespace charcount
{
int ShowPerCent();
int PerCent();
int Values(char letter);
int analize(string var);
}
This code is all part of "functions.h" of my project. This says:
functions.h: 7:13: error: 'string' was not declared in this scope
And I don't understand why says that this. I try with std::string and nope. Anyone know what happens? If you need more additional information ask.
The correct header is <string>. Change the include directive to:
#include <string>
The C++ standard library headers do not end with .h.
It's considered very bad practice to do using namespace std;, especially in a header file. This pollutes the global namespace with names from the std namespace and propagates said pollution to any file that includes it.
In C,
#include <string.h>
gives you the C string header (strlen(), strcmp() et al.).
In C++,
#include <string.h>
is deprecated, but gives you the same C string header. You are encouraged to use
#include <cstring>
instead, which gives you the same functions but in the std:: namespace (where they belong).
If you want std::string, the object-oriented auto-allocating auto-expanding C++ niceness, you would have to:
#include <string>
And please, don't use using namespace, especially not in combination with std::. The idea is to be explicit about which namespace a given identifier comes from.
Edit: Seconding sftrabbit, who typed quicker than me. While using namespace might be pardonable in your .cpp files, in headers it's a capital offense, because including your header could make perfectly valid C++ code invalid all of a sudden, because you changed the namespace.

Dev-C++ compile errors

I want to use "Dev-C++" for compile c++ codes.
So I download and install it, and write this code:
#include <iostream.h>
main () {
cout << "124";
}
but when I compiled it, it said:
In file included from
E:/Dev-Cpp/include/c++/3.4.2/backward/iostream.h:31,
from [myfile path]\Untitled1.cpp:1:
E:/Dev-Cpp/include/c++/3.4.2/backward/backward_warning.h:32:2:
warning: #warning This file includes
at least one deprecated or antiquated
header. Please consider using one of
the 32 headers found in section
17.4.1.2 of the C++ standard. Examples include substituting the header
for the header for C++ includes,
or instead of the
deprecated header . To
disable this warning use
-Wno-deprecated.
After I saw errors, I change my code to this code:
#include <iostream>
main () {
cout << "124";
}
but it said again that errors.
I compile first code easily in Turbo C++, BUT in Dev-C++ ...
What can I do?
First, make sure you write out the full definition of main, including the int return type. Leaving out the return type is an old, antiquated practice which doesn't fly these days.
Second, in the new-style headers—the ones missing the .h extension—the standard library is under the std namespace. There are two ways to make your program work:
1. Add an std:: qualifier to cout.
#include <iostream>
int main () {
std::cout << "124";
}
2. Add a using declaration to allow unqualified references to the std namespace.
#include <iostream>
using namespace std;
int main () {
cout << "124";
}
Make sure you put int in front of main () {
I believe any C/C++ program's main() function is required by POSIX and the appropriate language standards to return an int (someone correct me if I'm wrong).
EDIT: Also, be sure to include using namespace std; above int main ().