gpt4 book ai didi

c++ - 在类声明中使用类型定义迭代器的问题

转载 作者:行者123 更新时间:2023-11-28 00:02:14 26 4
gpt4 key购买 nike

<分区>

我正在处理一段遗留代码,该代码编写得很糟糕,只能在 Microsoft Visual Studios 和 Visual C++ 编译器中编译。由于构建时错误,GCC、G++ 或 Clang 都无法编译代码。我已将问题缩小到以下实例化 STL 的类声明class type 的容器在类声明中:

#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() { }

};
}

我想知道在保留代码结构的同时重构此代码段的最佳方法是什么。例如,我试图制作 MAP使用指针类型定义(即 typedef HASH_MAP<double, XYZ*> 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 {
^

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com