G++ 4.4 compile error, lower versions works - c++

I have my program written in C++ and it is can be successfully compiled on Ubuntu 9.04 with g++ 4.3.4 and Solaris OS with g++ 3.4.3. Now I have upgraded my Ubuntu to version 9.10 and g++ to version 4.4.1. Now compiler invokes the error in STL.
/usr/include/c++/4.4/bits/stl_deque.h: In member function ‘void std::deque<_Tp, _Alloc>::swap(std::deque<_Tp, _Alloc>&)’:
In file included from /usr/include/c++/4.4/deque:65,
/usr/include/c++/4.4/bits/stl_deque.h:1404: error: ‘swap’ is not a member of ‘std’
/usr/include/c++/4.4/bits/stl_deque.h:1405: error: ‘swap’ is not a member of ‘std’
/usr/include/c++/4.4/bits/stl_deque.h:1406: error: ‘swap’ is not a member of ‘std’
/usr/include/c++/4.4/bits/stl_deque.h:1407: error: ‘swap’ is not a member of ‘std’
I don't know how to fix it and if is possible that stl contains a bug. Can you help me, please?
Thanks a lot for all advices.

#include <algorithm>

In older versions of GCC, if you included any standard library header, that header would usually include many others. As mentioned by others, in your case <algorithm> got included this way.
This behavior isn't required by the standard. Many other implementations of the standard library don't exhibit this behavior, or to a lesser degree. It was just a design decision made by the GCC developers long ago. It seems they are now reducing this behavior, bringing it more in line with other implementations. This gives you more control over what gets included and what doesn't. Old code will break, but it's easily fixed by including the missing headers.

A post on an Apple forum suggests
#include <algorithm>

Related

How to build code that contains constant expression with c++11

Hello I am testing new c++ library : TagParser from Martchus
https://github.com/Martchus/tagparser
I am getting that error when I compile the following code:
CODE:
#include <tagparser/mediafileinfo.h>
#include <tagparser/diagnostics.h>
using namespace TagParser;
// create a MediaFileInfo for high-level access to overall functionality of the library
MediaFileInfo fileInfo;
// create container for errors, warnings, etc.
Diagnostics diag;
...
ERROR:
In file included from /usr/include/c++/5/cstdint:35:0,
from /usr/local/include/c++utilities/conversion/types.h:4,
from /usr/local/include/tagparser/tagtarget.h:6,
from /usr/local/include/tagparser/settings.h:4,
from /usr/local/include/tagparser/abstractcontainer.h:5,
from /usr/local/include/tagparser/mediafileinfo.h:4,
from TagParserTest.cpp:1:
/usr/include/c++/5/bits/c++0x_warning.h:32:2: error:
#error This file requires compiler and library support for the ISO C++ 2011 standard.
This support must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
When I try to build with the -std=c++11 option I get this error:
In file included from /usr/local/include/tagparser/abstractcontainer.h:5:0,
from /usr/local/include/tagparser/mediafileinfo.h:4,
from TagParserTest.cpp:1:
/usr/local/include/tagparser/settings.h: In function ‘constexpr TagParser::TagCreationFlags& TagParser::operator|=(TagParser::TagCreationFlags&, TagParser::TagCreationFlags)’:
/usr/local/include/tagparser/settings.h:53:1: error: expression ‘(lhs = ((TagParser::TagCreationFlags)(((std::underlying_type<TagParser::TagCreationFlags>::type)lhs) | ((std::underlying_type<TagParser::TagCreationFlags>::type)rhs))))’ is not a constant-expression
}
I really don't know how to solve this. Can anyone help me?
Many thanks to Daniel H.
When I use c++14 instead of c++11 it just works fine.
Thanks.

Error "is_enum not declared in this scope" when trying to compile my wxWidgets program

The G++ compiler shows the following errors when I try to compile:
C:\wx\include\wx\strvararg.h|350|error: 'is_enum' in namespace 'std' does not name a template type|
C:\wx\include\wx\strvararg.h|354|error: 'is_enum' was not declared in this scope|
C:\wx\include\wx\strvararg.h|354|error: template argument 1 is invalid|
I am using Code::Blocks for this, with a non-monolithic DLL build of wxWidgets.
Thanks for any help!
As Brian already indicated, you should enable C++11. There are two ways of doing this as you can see What are the differences between -std=c++11 and -std=gnu++11?.
As I remember, when compiling the whole wxWidgets library, I used -std=gnu++11 and had no problems.
Just to add to macroland comment:
You should consider upgrading to gcc version 5.2+. This version(s) does have a full set of c++11 features, whereas previous versions have them partially.

Getting error 'char16_t and char32_t undeclared'

I am developing a program in C++ on Linux. The gcc version is 4.5.1 20100924. I want to use std::atomic_int in my program. I have included atomic header as below:
include <atomic>
When I compile the program I get below errors:
In file included from /usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/atomic_base.h:87:0,
from /usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/atomic:41,
from ../Source/Main.h:33:
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/atomicfwd_cxx.h:107:25: error: ‘char16_t’ was not declared in this scope
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/atomicfwd_cxx.h:107:33: error: template argument 1 is invalid
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/atomicfwd_cxx.h:107:53: error: invalid type in declaration before ‘;’ token
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/atomicfwd_cxx.h:110:25: error: ‘char32_t’ was not declared in this scope
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/atomicfwd_cxx.h:110:33: error: template argument 1 is invalid
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/atomicfwd_cxx.h:110:53: error: invalid type in declaration before ‘;’ token
If I include <cstdint>, I get the same errors. The headers uchar.h and cuchar.h are not there on my system. How can I resolve the compilation errors?
Thank you in advance.
EDITED:
I was wrong about that. just pass --std=c++0x to g++, and that would do it.
You seem to not have enabled C++11 support in your compiler or you use a compiler that has these new types not declared.
For char16_t and char32_t, you need no extra include.
g++ howto:
Type g++ --version. If it is at least 4.4, then it has support for new string literals. If not: You need a newer compiler version.
Then, make sure to pass --std=c++0x or --std=c++11 to the compiler.

error: strstream.h: No such file or directory

I am trying to run an old C++ code in Linux (Redhat). I am using gcc version 4.1.2.
I got the following error:
error: strstream.h: No such file or directory
/trnuser1/rmtrain/DevelopmentEnv/Generic/CoreObjects/GCVTransformationServices.cpp:41: error: âostrstreamâ was not declared in this scope
/trnuser1/rmtrain/DevelopmentEnv/Generic/CoreObjects/GCVTransformationServices.cpp:41: error: expected `;' before âstrDestXMLâ
/trnuser1/rmtrain/DevelopmentEnv/Generic/CoreObjects/GCVTransformationServices.cpp:62: error: âstrDestXMLâ was not declared in this scope
This code was running fine under Solaris with gcc version 2.95. The line pointed to by the error contains the following statement:
ostrstream strDestXML;
How do I solve this?
You can #include <strstream> (note absence of the '.h' suffix).
But if you want to properly port the code to modern C++, you should consider changing this to #include <sstream> and std::ostringstream strDestXML; as suggested in the comment.
Standard C++ headers do not have extension.
#include <sstream>
Standard classes are contained in std namespace:
std::ostringstream strDestXML;
Finally, strstream is deprecated; use stringstream instead - that's why I used it here.
And, just a note about GCC version - 4.1.2 is old, no matter what - use something newer.
The modern name for this include is <strstream>. (Although it's formally deprecated, it's still required.) The classes it defines are in namespace std, and have slightly different semantics than the classical iostream, so you may have to do a little bit of modification later anyway. (Depending on how it is being used, it might make sense to change to <sstream>, replacing [io]strstream with std::[io]stringstream.)

How to run program written for old compiler?

I'm looking for an implementation of Hybrid Tree(not important), and find an "old" one here.
The author said they have tried this code on the SUN Sparc platform (running Solaris 2.6) and with gcc-2.8.1 compiler. And my environment is gcc version 4.4.3 (Ubuntu 10.10).
The problem is:
I run "make" with the makefile he provides, but it gives me lots of error message as follows:
g++ -c Node.C
g++ -c DataNode.C
In file included from DataNode.h:18,
from DataNode.C:17:
Query.h:9:20: error: vector.h: No such file or directory
Query.h:10:19: error: stack.h: No such file or directory
Query.h:13:22: error: function.h: No such file or directory
Query.h:14:22: error: iostream.h: No such file or directory
DataNode.C:283:8: warning: extra tokens at end of #endif directive
In file included from DataNode.h:18,
from DataNode.C:17:
Query.h:29: warning: ‘typedef’ was ignored in this declaration
Query.h:44: warning: ‘typedef’ was ignored in this declaration
Query.h:86: error: expected initializer before ‘<’ token
Query.h:118: error: ISO C++ forbids declaration of ‘PQ’ with no type
Query.h:118: error: expected ‘;’ before ‘*’ token
Query.h:122: error: ISO C++ forbids declaration of ‘PQ’ with no type
Query.h:122: error: expected ‘;’ before ‘*’ token
Query.h:126: error: ISO C++ forbids declaration of ‘PQ’ with no type
Query.h:126: error: expected ‘;’ before ‘*’ token
Query.h:135: error: expected initializer before ‘<’ token
DataNode.C: In member function ‘void DataNode::DisconnectBranch(int)’:
DataNode.C:80: error: ‘memmove’ was not declared in this scope
make: *** [DataNode.o] Error 1
I know I need to modify the souce code so as to agree with the morden compiler, such as change vector.h to vector. But I find it's just endless.
So my question is: is there any convienent method to run this program, no matter automatically converting this code to "modern-style" or using a standalone "old-style" compiler ?
Any suggestions?
===Update:===
Thank you all, I installed gcc2.8.1 in a different dir using --prefix=/usr/local/gcc-2.8.1 and modify the "makefile" to use this old-version gcc(/usr/local/gcc-2.8.1/bin/gcc). But when I run "make", it still gives me errors of not finding the headers:
/usr/local/gcc-2.8.1/bin/gcc -c DataNode.C
In file included from DataNode.h:18,
from DataNode.C:17:
Query.h:9: vector.h: No such file or directory
Query.h:10: stack.h: No such file or directory
Query.h:11: deque: No such file or directory
Query.h:12: algorithm: No such file or directory
Query.h:13: function.h: No such file or directory
Query.h:14: iostream.h: No such file or directory
make: *** [DataNode.o] Error 1
Then I tried to find these heads in /usr/local/gcc-2.8.1 using find /usr/local/gcc-2.8.1 -name "*vector*", but got nothing.
So where are these heads for the old-version gcc?
You can make a vertor.h yourself which includes vector. This way you can fix the incompatibilities noninvasively.
Edit:
You may also need to add a
using namespace std;
in the header file(s). This is
generally a bad idea but this is one situation where i'd do it anyway.
Once you get it working i would reccomend rewriting it to use the new style header files and namespaces.
Debian Lenny (oldstable) has gcc 3.4. This might have a better backward compatibility. Try to make compatibility headers for the rest of the issues and include them via an extra -I directory, e.g. a vector.h header file that includes vector.
Do yourself the favor and try not to touch the old code. It is easy to break legacy code in unforeseen ways.
You could try running the program on QEMU which supports Solaris 2.6. The only problem might be hunting for the install disc/image. Also, there are people that sells old Solaris boxes on eBay for cheap, you might be able to grab one.
GCC provides download for very old versions, you might be able to get better chance if you try older version of the compiler.
gcc has -fpermissive option: try it and see whether at least some errors disappear. Also: try making a single header file that will include all requisite headers with using directives. For example, make stdinc.h containing:
#include <vector>
#include <iostream>
#include <stack>
...
using std::vector;
using std::fstream;
...
Replace all mentions of legacy C++ header files with a single include of stdinc.h. Old C++ didn't have namespaces, so even if you replace individual directives with just using namespace std;, clashes are unlikely.
If the only thing is
#include <vector.h>
to
#include <vector>
using namespace std;
Why not try sed?
grep for all includes to see if there are other includes than the c++ headers. If not your lucky.
Another thing that is more tricky is that there is old code that relies on accessing data through iterators in a non-standard way. I saw that in a Doom map editor for linux. Then you may need to do manual stuff.