I've done a lot of searching on this issue but none of the answers helped with my issue.
My setup
- Visual Studio 2017
- Windows 10
I have a large codebase, where in one class Bar
// Bar.h
class Bar {
public:
Bar();
~Bar();
public:
... // other methods.
private:
std::map<std::string, Foo> m_foos;
};
then in its implementation
// Bar.cpp
Bar::Bar() {
m_foos.insert(std::make_pair(std::string("id"), Foo())); // Error 1
m_foos.insert({std::string("id"), Foo()}); // Error 2
m_foos.insert(std::pair<std::string, Foo>(std::string("id"), Foo())); // Error 3
}
Trying to compile the above code gives me:
Error 1
error C2664: 'std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>> std::_Tree<std::_Tmap_traits<_Kty,Foo,_Pr,_Alloc,false>>::insert(std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>>,const std::pair<const _Kty,Foo> &)': cannot convert argument 1 from 'std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char>>,Foo>' to 'std::pair<const _Kty,_Ty> &&'
Error 2
error C2664: 'std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>> std::_Tree<std::_Tmap_traits<_Kty,Foo,_Pr,_Alloc,false>>::insert(std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>>,const std::pair<const _Kty,Foo> &)': cannot convert argument 1 from 'initializer list' to 'std::pair<const _Kty,_Ty> &&'
Error 3
error C2440: '<function-style-cast>': cannot convert from 'initializer list' to 'std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char>>,Foo>'
However, with the same setup and compiler, the following code compiles successfully:
#include "pch.h"
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <map>
#include <string>
#include <vector>
class Foo {
public:
Foo() { mi = 1; }
~Foo() {}
private:
int mi;
};
class MyFoo : public Foo {
public:
MyFoo() :Foo() {
mj = 2;
mk = nullptr;
}
~MyFoo() {}
private:
int mj;
int* mk;
};
class YourFoo : public Foo {
public:
YourFoo() :Foo() { mj = 2; }
~YourFoo() {}
private:
int mj;
};
int main()
{
std::map<int, Foo> yourmap;
yourmap.insert(std::make_pair(3, Foo()));
yourmap.insert({ 4, Foo() });
yourmap.insert(std::pair<int, Foo>(5, Foo()));
}
Where am I wrong?
Aucun commentaire:
Enregistrer un commentaire