Hi guys,
when i am compile this program it gives error. i am wondering why it is so.. plz help me.
thankx

#include <iostream>
#include <map>
#include <string>                   

using namespace std;                   
class ConfigPath
{                                                    
public:
    ConfigPath()
    {
      m_name = "VIPIN DAHIYA";
    }
      ~ConfigPath(){}
   
private:                           
   string m_name;
}; 
   
int main()                                                            
{  
   ConfigPath m1;
   ConfigPath m2;                                         
   map <string, ConfigPath *> name_path;     
   map <int, ConfigPath *> name_path;
   string s1 = "V1";
   string s2 = "V2";
  name_path.insert(map <string, ConfigPath *>:: value_type(s1 ,m1));
  name_path.insert(map <string, ConfigPath *>:: value_type(s2 ,m2));
return 0;
}
map.cpp: In function ‘int main()’:
map.cpp:24: error: conflicting declaration ‘std::map<int, ConfigPath*, std::less<int>, std::allocator<std::pair<const int, ConfigPath*> > > name_path’
map.cpp:23: error: ‘name_path’ has a previous declaration as ‘std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, ConfigPath*, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, ConfigPath*> > > name_path’
map.cpp:27: error: no matching function for call to ‘std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, ConfigPath*>::pair(std::string&, ConfigPath&)’
/usr/include/c++/4.2/bits/stl_pair.h:84: note: candidates are: std::pair<_T1, _T2>::pair(const _T1&, const _T2&) [with _T1 = const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _T2 = ConfigPath*]
/usr/include/c++/4.2/bits/stl_pair.h:80: note:                 std::pair<_T1, _T2>::pair() [with _T1 = const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _T2 = ConfigPath*]
/usr/include/c++/4.2/bits/stl_pair.h:69: note:                 std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, ConfigPath*>::pair(const std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, ConfigPath*>&)
map.cpp:28: error: no matching function for call to ‘std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, ConfigPath*>::pair(std::string&, ConfigPath&)’
/usr/include/c++/4.2/bits/stl_pair.h:84: note: candidates are: std::pair<_T1, _T2>::pair(const _T1&, const _T2&) [with _T1 = const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _T2 = ConfigPath*]
/usr/include/c++/4.2/bits/stl_pair.h:80: note:                 std::pair<_T1, _T2>::pair() [with _T1 = const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _T2 = ConfigPath*]
/usr/include/c++/4.2/bits/stl_pair.h:69: note:                 std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, ConfigPath*>::pair(const std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, ConfigPath*>&)

Oh I am sorry guys .. that is a copy/paste mistake.

actually code is like that

#include <iostream>
#include <map>
#include <string>

using namespace std;
class ConfigPath
{
public:
    ConfigPath()
    {
      m_name = "VIPIN DAHIYA";
    }
      ~ConfigPath(){}

private:
   string m_name;
};

int main()
{
   ConfigPath m1;
   ConfigPath m2;
   map <string, ConfigPath *> name_path;
   string s1 = "V1";
   string s2 = "V2";
  name_path.insert(map <string, ConfigPath *>:: value_type(s1 ,m1));
  name_path.insert(map <string, ConfigPath *>:: value_type(s2 ,m2));
return 0;
}

i didn't have two object with same name and still getting error.

You're map is supposed to store a string and a ConfigPaths* , but you are trying to pass it s1 (a string) and m1 (a ConfigPath, NOT a ConfigPath*)

I've never used that ::value_type, so I could be wrong, but thats the first thing I'd look at.

Thankx guys.

I got it . if i am using key value then to access value i have to get iterator.second
.That i was missing and yes i have to pass a pointer object it was a mistake.

This article has been dead for over six months. Start a new discussion instead.