我尝试将一些项目从 Visual Studio 2010
升级到 2015
。在 2010
中,一切正常,但在 2015
中,我遇到了我不太理解的编译器错误。
我将代码压缩成一个 SSCCE
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <string>
#include <sstream>
#define UNUSED(x) (void(x))
class ColumnDef
{
public:
};
class XMLColumnDef
{
public:
typedef std::pair<std::string, XMLColumnDef> pair;
typedef std::pair<std::string, XMLColumnDef *> ptr_pair;
public:
// Move constructor for moving the column into an container
// without destroying it. Note that the source is no longer valid after
// such a move.
XMLColumnDef(XMLColumnDef &oSource)
{
mColumn = oSource.mColumn;
oSource.mColumn = NULL;
}
XMLColumnDef(ColumnDef *pColumn)
{
mColumn = pColumn;
}
virtual ~XMLColumnDef(void)
{
delete mColumn;
}
private:
ColumnDef *mColumn;
};
class XMLNodeObserver
{
public:
typedef std::map<std::string, XMLNodeObserver *> map;
typedef std::pair<std::string, XMLNodeObserver *> pair;
typedef std::map<std::string, XMLColumnDef> row_def;
public:
XMLNodeObserver(void) {}
virtual ~XMLNodeObserver(void) {};
void addObserver(XMLNodeObserver::map &oNodeObserver, std::string const &oPath, XMLNodeObserver *oObserver = NULL)
{
UNUSED(oNodeObserver);
UNUSED(oPath);
UNUSED(oObserver);
}
void addColumnDef(XMLNodeObserver::map &oNodeObserver, std::string const &oPath, ColumnDef *oColumn, XMLNodeObserver *oObserver = NULL)
{
if (oObserver == NULL)
oObserver = this;
addObserver(oNodeObserver, oPath, oObserver);
XMLColumnDef::pair pr = std::make_pair(oPath, XMLColumnDef(oColumn));
if (oObserver->mColumnDefs.find(oPath) != oObserver->mColumnDefs.end())
{
// If a node already exists, we report an internal error, because this indicates a program bug and should be fixed.
std::cerr << "INTERNAL ERROR: Duplicate rowdef key in XML node observer: " << oPath << std::endl;
}
oObserver->mColumnDefs.insert(pr);
}
private:
row_def mColumnDefs;
bool mDebugMode;
};
int main()
{
XMLNodeObserver o;
XMLNodeObserver::map om;
o.addColumnDef(om, "/testpath", new ColumnDef());
std::cout << "\nDone! Press any key..." << std::endl;
std::cin.ignore();
return 0;
}
我在这一行中得到的错误:
oObserver->mColumnDefs.insert(pr);
我得到的错误是这样的:
junk.cpp(104): error C2664: 'void std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>>::insert(std::initializer_list<std::pair<const _Kty,_Ty>>)': cannot convert argument 1 from 'XMLColumnDef::pair' to 'std::pair<const _Kty,_Ty> &&'
with
[
_Kty=std::string,
_Ty=XMLColumnDef,
_Pr=std::less<std::string>,
_Alloc=std::allocator<std::pair<const std::string,XMLColumnDef>>
]
and
[
_Kty=std::string,
_Ty=XMLColumnDef
]
junk.cpp(104): note: Reason: cannot convert from 'XMLColumnDef::pair' to 'std::pair<const _Kty,_Ty>'
with
[
_Kty=std::string,
_Ty=XMLColumnDef
]
junk.cpp(104): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
看起来 Visual Studio 2015
不喜欢参数 oPath
是 const &
但这不重要,因为它应该分配给 pair
成员。
我还删除了 typedefs
并尝试使用长名称对其进行编译,但这似乎不是问题所在(无论如何也不应该),因为我遇到了同样的错误。
喜欢:
std::pair<std::string, XMLColumnDef> pr = std::pair<std::string, XMLColumnDef>(oPath, XMLColumnDef(oColumn));
最佳答案
map 中的对类型为 pair<const key,value>
而不是 pair<key,value>
.尝试使用 MyMapType::value_type
typedef 代替。您可以免费获得它!
例子:
typedef std::map<std::string, XMLNodeObserver *> MyMapType;
typedef MyMapType::value_type MyPairType;
关于c++ - 无法在 Visual Studio 2015 中将 typedef 转换为 std::pair,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33997710/