I am trying to compile some C++ code (which can be compiled with Visual Studio 2012 on Windows) with g++-4.4.
I have this snippet of code,
const std::string cnw::restoreSession(const std::vector<string> &inNwsFile) {
for (std::string &nwFile : inNwsFile){
// some...
}
}
that I cannot compile because of this error:
CNWController.cpp:154: error: expected initializer before ‘:’ token
Can you give me some advise on how to solve this problem?
Your compiler is too old to support range-based for syntax. According to GNU it was first supported in GCC 4.6. GCC also requires you to explicitly request C++11 support, by giving the command-line option -std=c++11, or c++0x on compilers as old as yours.
If you can't upgrade, then you'll need the old-school equivalent:
for (auto it = inNwsFile.begin(); it != inNwsFile.end(); ++it) {
std::string const &nwFile = *it; // const needed because inNwsFile is const
//some...
}
I believe auto is available in GCC 4.4 (as long as you enable C++0x support), to save you writing std::vector<string>::const_iterator.
If you really do need a non-const reference to the vector's elements then, whichever style of loop you use, you'll need to remove the const from the function parameter.
Related
I am trying to port some C++17 code I made on ubuntu (gnu++11)
typedef boost::variant<int, float, std::string > Variant;
using Func = std::function<std::vector<unsigned char>(std::vector<Variant>)>;
void addexecutorfunc( Func callback, const auto&...args )
{
std::vector<Variant> vec = {args...};
executor.add(vec, std::move(callback));
}
this code compiles and works fine on ubuntu, but when trying to compile on windows with visual studio 2017(v141) [ISO C++ Latest Draft Standard(/std:c++latest)], then I get following:
error C3533: a parameter cannot have a type that contains 'auto'
I think perhaps it has to do with the Concepts lite not being implemented in current C++17 version or is this wrong?
If I could setup compiler to use auto as parameter and parameter packs, then that would be best, but if this is not possible, then I will have to rewrite my code to follow C++17 windows standard - any suggestions on how to do this without ending up in a template hell
void addexecutorfunc( Func callback, const auto&...args )
auto as a parameter to a (non-lambda) function is a GNU extension. It is not part of standard C++17, and is not supported by either of the other two major C++ compilers, Clang and MSVC. Rather unfortunately, GCC seems to allow it in -std=c++14 mode as well as with -std=gnu++14.
The standard C++ equivalent would be a function template
template <typename... Ts>
void addexecutorfunc(Func callback, const Ts&... args)
which should work as expected.
I tried to get myself into C++ and purchased the book "Programming - Principles and Practice Using C++" by Bjarne Stroustrup.
When I tried to get compile the following source code:
#include "std_lib_facilities.h"
int main(){
cout<<"Hello, World!\n";
keep_window_open();
return 0;
}
I am getting following compile error:
In file included from /Users/hypertrooper/Documents/Programming - Principles and Practice Using C++/hello_world.cpp:1:
std_lib_facilities.h:71:20: warning: alias declarations are a C++11 extension [-Wc++11-extensions]
using size_type = typename std::vector<T>::size_type;
^
std_lib_facilities.h:102:20: warning: alias declarations are a C++11 extension [-Wc++11-extensions]
using size_type = std::string::size_type;
^
std_lib_facilities.h:107:8: warning: comparison of unsigned expression < 0 is always false [-Wtautological-compare]
if (i<0||size()<=i) throw Range_error(i);
~^~
std_lib_facilities.h:113:8: warning: comparison of unsigned expression < 0 is always false [-Wtautological-compare]
if (i<0||size()<=i) throw Range_error(i);
~^~
std_lib_facilities.h:213:107: error: expected '(' for function-style cast or type construction
inline int randint(int min, int max) { static default_random_engine ran; return uniform_int_distribution<>{min, max}(ran); }
~~~~~~~~~~~~~~~~~~~~~~~~~~^
std_lib_facilities.h:222:20: warning: alias declarations are a C++11 extension [-Wc++11-extensions]
using Value_type = typename C::value_type;
^
std_lib_facilities.h:225:18: warning: alias declarations are a C++11 extension [-Wc++11-extensions]
using Iterator = typename C::iterator;
^
6 warnings and 1 error generated.
I get it that my compiler is not using C++11 feature, but I do not know how I can update the compiler. I should let know that I am using a MacOSX (10.10 Yosemite) and tried to compile with xCode and textmate. I even tried to follow this tutorial(https://wiki.helsinki.fi/display/HUGG/Installing+the+GNU+compilers+on+Mac+OS+X), but it did not helped. (At least when I tried to compile with text mate)
I hope you are able to help me. :(
If you are on a Mac or Linux, the compiler is usually g++ or clang; to access C++11, just specify -std=c++11 as an option when invoking the compiler (Assuming that you have an up-to-date version).
What you need to do is open the project settings -> Build Settings and set C++ Language Dialect to C++11 and the C++ Standard Library to libc++ (LLVM C++ standard library with C++11 support).
From your build settings, adjust the C++ Language Dialect to C++11...
Also make sure you are using the LLVM C++ library, as it has full C++ 11 support.
I've tried to compile elementary example:
#include <vector>
int main ()
{
std::vector<int> testV;
for (const auto& test : testV)
{ }
return 0;
}
And I've received error:
test.cpp: In function 'int main()':
test.cpp:5:29: error: 'begin' was not declared in this scope
test.cpp:5:29: error: 'end' was not declared in this scope
test.cpp:5:29: error: unable to deduce 'const auto&' from '<expression error>'
Does STLport support const auto ?
EDIT: I'm using GCC 4.6
With 4.7 and more everything is ok.
gcc 4.6 came out in the spring of 2011, was not without bugs in most C++11 features. Moroever, around the same time the rules for ADL lookup in range-for were also modified (note that this was prior to the official ratification of the C++11 Standard in the summer of 2011). See this Q&A for more details. It's probably not worth debugging this and the recommended course of action is to upgrade to a recent version of gcc (4.7 or preferably 4.8).
I'm trying to port/build g++ to run on my system, and running into the following error while building libstdc++:
.../gcc-4.6.2/i686-pc-linux-gnu/libstdc++-v3/include/mutex:226:50: error: could not convert '{0}' from '<brace-enclosed initializer list>' to 'std::timed_mutex::__native_type {aka pthread_mutex_t}'
The relevant code in include/mutex is:
class timed_mutex
{
// ...
__native_type _M_mutex;
// ...
timed_mutex() : _M_mutex(__GTHREAD_MUTEX_INIT) { } // Line 226
// ...
}
__native_type is pthread_mutex_t and __GTHREAD_MUTEX_INIT expands to {0}.
I'm not very familiar at all with C++, just C, but I can't see anything obviously wrong here. What does the error mean?
The correct syntax is:
timed_mutex() : _M_mutex({__GTHREAD_MUTEX_INIT}) { } // i.e. _M_mutex({{0}})
However that feature is available only with C++11. Demo.
For older compilers, you can't use initializer list with constructor.
The reason for having 2 {} is that, pthread_mutex_t is a union defined as shown here. Which contains, a struct, char[24], long int; thus naturally the initialization syntax would differ.
Update:
When I tried to compile <mutex> header in a test file, it gives following error:
/usr/include/c++/4.6/bits/c++0x_warning.h:32:2: error: #error This
file requires compiler and library support for the upcoming ISO C++
standard, C++0x. This support is currently experimental, and must be
enabled with the -std=c++0x or -std=gnu++0x compiler options.
Quite possibly the particular file follows the initializer syntax of C++11.
I have been developing a library that is getting pretty large, and now I am adding some template-based parts that use C++0x features. So I tried to compile my library (which compiled entirely without warnings on the current standard) with the flag -std=c++0x using gcc version 4.4.5 (on Linux). Now I got a huge influx of error messages related to the conversion of "temporaries" variables to non-const references. The problem is, they are not temporary!
Here is a small piece of code that reproduces the error:
#include <iostream>
#include <map>
struct scanner {
scanner& operator &(std::pair<std::string, int&> i) {
std::cout << "Enter value for " << i.first << ": ";
std::cin >> i.second;
return *this;
};
};
struct vect {
int q[3];
void fill(scanner& aScan) {
aScan & std::pair<std::string, int&>("q0",q[0])
& std::pair<std::string, int&>("q1",q[1])
& std::pair<std::string, int&>("q2",q[2]);
};
};
int main() {
vect v;
scanner s;
v.fill(s);
return 0;
};
If you compile this with the current standard (no c++0x flag) it will compile and run as expected. However, if you compile it with -std=c++0x, it will throw the following error at compile-time:
/usr/include/c++/4.4/bits/stl_pair.h:94: error: invalid initialization of non-const reference of type ‘int&’ from a temporary of type ‘int’
I really can't figure this out. I have looked over the web and SO, but none seem to have this problem. Is it a bug in std::pair? I would really like to know what the problem is.. thank you for any insight you can give.
PS: don't complain about the "quality" or "stupidity" of the code above, it is not real code.. just an example that shows the error.
Your code is not valid C++03, comeau gives (after adding a return statement to op&):
"stl_pair.h", line 44: error: qualifiers dropped in binding reference of
type "int &" to initializer of type "const int"
pair(const _T1& __a, const _T2& __b) : first(__a), second(__b) {}
^
detected during instantiation of "std::pair<_T1, _T2>::pair(const
_T1 &, const _T2 &) [with _T1=std::string, _T2=int &]" at
line 17 of "ComeauTest.c"
...
The problem is a reference inside a pair. If I remember correctly, it is a gcc extension that allows this. Gcc does accept it with -std=gnu++98, which is the default for C++, but with -std=c++98, gcc 4.4.3 gives:
In file included from /usr/include/c++/4.4/bits/stl_algobase.h:66,
from /usr/include/c++/4.4/algorithm:61,
from PREAMBLE:7:
/usr/include/c++/4.4/bits/stl_pair.h: In instantiation of ‘std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int&>’:
<input>:5: instantiated from here
/usr/include/c++/4.4/bits/stl_pair.h:83: error: forming reference to reference type ‘int&’
...
There are LOTS of bugs in gcc C++0x support, it's very much unfinished and development is ongoing. This is doubly true for a version as old as gcc-4.4.5. If you're serious about starting C++0x development before the standard is ratified, you need to use the bleeding-edge version of the compiler and standard library.
It compiles fine both with and without -std=c++0x with GCC 4.5.2.
I guess GCC 4.4.5 doesn't support C++0x that much to get this working.
Except for the missing return *this; in scanner& operator &(std::pair<std::string, int&> i), your code is valid C++0x.