【发布时间】:2016-10-25 22:27:10
【问题描述】:
我正在编写一个写得很糟糕的遗留代码,只能在 Microsoft Visual Studios 和 Visual C++ 编译器中编译。由于构建时间错误,GCC、G++ 或 Clang 都无法编译代码。我已将问题缩小到以下类声明,它在类声明中实例化 class type 的 STL 容器:
#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() {}
~abc() { }
};
}
我想知道在保留代码结构的同时重构此代码段的最佳方法是什么。例如,我试图使用指针类型(即typedef HASH_MAP<double, XYZ*> MAP)创建MAP 定义,但是此更改适用于 GCC 编译器,因为我要更改为指针类型,我必须深入研究代码库并修改大部分代码库,因为该类在其他依赖代码中起着关键作用。
所以我想知道是否有替代方法可以解决此问题,而无需对原始代码库进行重大更改。我正在考虑制作类似的friend 类。
以下是编译错误:
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 {
^
【问题讨论】:
-
命名空间怎么可能是
std::unordered_map的模板参数? -
那只是一个
typedef没有实例化 -
我很好奇为什么
public:被声明了两次?abc或namespace XYZ中的任何一个类都缺少左侧}。 -
@user2079303 一定是笔误,其他类方法也是
XYZ()和~XYZ() -
你
#include <utility>了吗?你在用using namespasce std;吗?如果不是,那么标准库中的所有名称都需要在名称前加上std::。
标签: c++ c++11 stl iterator incomplete-type