stl - passing values to Pair in c++ -


i have pair type of string , string &, may pass values pair shown below

typedef std::pair<std::string, std::string&> namednode; voi main(){ std::string name = "name"; std::string * value = new std::string("value") ; namednode(name,*value);} 

when pass values pair shown above got following errors error c2529: '_val2' : reference reference illegal c:\program files (x86)\microsoft visual studio 9.0\vc\include\utility

error c2665: 'std::pair<_ty1,_ty2>::pair' : none of 3 overloads convert argument types d:\jzon\sample.cpp

and below warning warning 1 warning c4181: qualifier applied reference type; ignored c:\program files (x86)\microsoft visual studio 9.0\vc\include\utility

i'm using c++98, microsoft visual studio 2008, os-windows xp

thanks..,

no it's *value.

  • the standard main in c++ int main() when no interest in command line paramenters , int main(int argc, char* argv[]) or int main(int argc, char** argv) when need process parameters.

code (tested in gcc 4.9.1 c++11):

#include <utility> #include <iostream> #include <string>  typedef std::pair<std::string, std::string&> namednode; int main() {     std::string name = "name";     std::string* value = new std::string("value");     namednode a(name, *value);     std::cout << a.first << " " << a.second << std::endl;     return 0; } 

the & operator mean reference of in case of std::string need pass std::string not std::string*

hints: don't use new in c++, it's sure not needed , allow bad practice (ex: memory leaks when exceptions throw, etc...), use instead std::unique_ptr or std::shared_ptr (c++11), or boost::shared_ptr , boost::scope_ptr (if c++11 not allowed).


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -