I'm working on a legacy code which is poorly written and only compiles within Microsoft Visual Studios and with Visual C++ compiler. GCC, G++, or Clang all fails to compile the code due to build time errors. I have narrowed down the issue to the following class declaration which instantiate a STL container of the class type within the class declaration:
#include <map>
#include <set>
#include <iomanip>
#include <string>
#include <cmath>
#include <iterator>
#include <unordered_map>
#include <bits/unique_ptr.h>
#define HASH_MAP unordered_map
using namespace std;
namespace XYZ {
class abc {
public:
typedef HASH_MAP<double, abc> MAP; // This is the problem ?
typedef MAP::iterator Iterator;
typedef MAP::const_iterator ConstIterator;
typedef pair<double, abc> Pair;
bool less(abc::Pair& a, abc::Pair& b) { return a.first < b.first; }
public:
abc():_test(0) {}
~abc() { }
// Some function definitions
...
...
...
};
}
I want to know what is the best way to refactor this code segment while preserving the structure of the code. For example, I was trying to make the MAP definition with a pointer type (i.e., typedef HASH_MAP<double, XYZ*> MAP) This change worked with GCC compiler however, since, I'm changing to pointer type I would have to dig deep in to the code base and modify most of the code base as this class is playing a key role in other dependent code.
So I was wondering if there is an alternative to fix this issue which would not require significant change to the original code base. I was thinking in the line of making a friend class similar.
Following is the compiler error:
In file included from /usr/include/c++/4.8/bits/stl_algobase.h:64:0,
from /usr/include/c++/4.8/bits/stl_tree.h:61,
from /usr/include/c++/4.8/map:60,
from /home/user/work/wxy.h:4,
from /home/user/work/abc.h:4,
from /home/user/work/abc.cpp:1:
/usr/include/c++/4.8/bits/stl_pair.h: In instantiation of ‘struct std::pair<const double, XYZ::abc>’:
/usr/include/c++/4.8/type_traits:615:28: required from ‘struct std::__is_destructible_impl<std::pair<const double, XYZ::abc> >’
/usr/include/c++/4.8/type_traits:637:12: required from ‘struct std::__is_destructible_safe<std::pair<const double, XYZ::abc>, false, false>’
/usr/include/c++/4.8/type_traits:652:12: required from ‘struct std::is_destructible<std::pair<const double, XYZ::abc> >’
/usr/include/c++/4.8/type_traits:116:12: required from ‘struct std::__and_<std::is_destructible<std::pair<const double, XYZ::abc> >, std::__is_direct_constructible_impl<std::pair<const double, XYZ::abc>, const std::pair<const double, XYZ::abc>&> >’
/usr/include/c++/4.8/type_traits:817:12: required from ‘struct std::__is_direct_constructible_new_safe<std::pair<const double, XYZ::abc>, const std::pair<const double, XYZ::abc>&>’
/usr/include/c++/4.8/type_traits:895:12: [ skipping 4 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ]
/usr/include/c++/4.8/type_traits:968:12: required from ‘struct std::__is_copy_constructible_impl<std::pair<const double, XYZ::abc>, false>’
/usr/include/c++/4.8/type_traits:974:12: required from ‘struct std::is_copy_constructible<std::pair<const double, XYZ::abc> >’
/usr/include/c++/4.8/bits/alloc_traits.h:540:12: required from ‘struct std::__is_copy_insertable<std::allocator<std::pair<const double, XYZ::abc> > >’
/usr/include/c++/4.8/bits/alloc_traits.h:560:63: required by substitution of ‘template<class _Alloc> using __check_copy_constructible = std::__allow_copy_cons<std::__is_copy_insertable<_Alloc>::value> [with _Alloc = std::allocator<std::pair<const double, XYZ::abc> >]’
/usr/include/c++/4.8/bits/unordered_map.h:97:11: required from ‘class std::unordered_map<double, XYZ::abc>’
/home/user/work/abc.h:27:20: required from here
/usr/include/c++/4.8/bits/stl_pair.h:102:11: error: ‘std::pair<_T1, _T2>::second’ has incomplete type
_T2 second; /// @c second is a copy of the second object
^
In file included from /home/user/work/abc.cpp:1:0:
/home/user/work/abc.h:24:11: error: forward declaration of ‘class XYZ::abc’
class abc {
^
Aucun commentaire:
Enregistrer un commentaire