c++ - Error : no instance of overloaded function -
i'm trying insert lines in database (sql server 2008) code :
cdb.cpp
#include "cdb.h" void cdb::ajouteralerte(){ sqlconnection ^ mysqlconnection; sqldataadapter ^ mydataadapter; dataset ^ mydataset; datarow ^ myrow; sqlparameter ^ myparameter; try { mysqlconnection = gcnew sqlconnection("data source=nectarys-pc;initial catalog=monitoringn;integrated security=true;"); mydataadapter = gcnew sqldataadapter(); mydataset = gcnew dataset(); // open connection mysqlconnection->open(); mydataadapter->selectcommand = gcnew sqlcommand("select * alerte", mysqlconnection); mydataadapter->insertcommand = gcnew sqlcommand("insert alerte (motif,datealerte," + "fixee,nomposte,nomapplication,nomfichier,fichiermodel_id) values (@motif,@datealerte," + "@fixee,@nomposte,@nomapplication,@nomfichier,@fichiermodel_id)", mysqlconnection); myparameter = mydataadapter->insertcommand->parameters->add( gcnew sqlparameter("@motif", sqldbtype::varchar)); myparameter->sourcecolumn = "motif"; myparameter->sourceversion = datarowversion::current; myparameter = mydataadapter->insertcommand->parameters->add( gcnew sqlparameter("@datealerte", sqldbtype::varchar)); myparameter->sourcecolumn = "datealerte"; myparameter->sourceversion = datarowversion::current; myparameter = mydataadapter->insertcommand->parameters->add( gcnew sqlparameter("@fixee", sqldbtype::bit)); myparameter->sourcecolumn = "fixee"; myparameter->sourceversion = datarowversion::current; myparameter = mydataadapter->insertcommand->parameters->add( gcnew sqlparameter("@nomposte", sqldbtype::varchar)); myparameter->sourcecolumn = "nomposte"; myparameter->sourceversion = datarowversion::current; myparameter = mydataadapter->insertcommand->parameters->add( gcnew sqlparameter("@nomapplication", sqldbtype::varchar)); myparameter->sourcecolumn = "nomapplication"; myparameter->sourceversion = datarowversion::current; myparameter = mydataadapter->insertcommand->parameters->add( gcnew sqlparameter("@nomfichier", sqldbtype::varchar)); myparameter->sourcecolumn = "nomfichier"; myparameter->sourceversion = datarowversion::current; myparameter = mydataadapter->insertcommand->parameters->add( gcnew sqlparameter("@fichiermodel_id", sqldbtype::int)); myparameter->sourcecolumn = "fichiermodel_id"; myparameter->sourceversion = datarowversion::current; time_t = time(0); tm ltm; localtime_s(<m, &now); std::stringstream str; str << ltm.tm_mday << "/" << 1 + ltm.tm_mon << "/" << 1900 + ltm.tm_year << " " << 1 + ltm.tm_hour << ":" << 1 + ltm.tm_min << ":" << 1 + ltm.tm_sec; string^ dateajoutsysteme = gcnew system::string(str.str().c_str()); boolean fixee = true; mydataadapter->fill(mydataset, "alerte"); myrow = mydataset->tables["alerte"]->newrow(); myrow->itemarray[0] = "a"; myrow->itemarray[1] = dateajoutsysteme; myrow->itemarray[2] = fixee; myrow->itemarray[3] = "b"; myrow->itemarray[4] = "c"; myrow->itemarray[5] = "d"; myrow->itemarray[6] = null; mydataset->tables["alerte"]->rows->add(myrow); //we use insertcommand property update. mydataadapter->update(mydataset, "alerte"); } catch (exception ^ e) { console::write(e->tostring()); } __finally { mysqlconnection->close(); system("pause"); } } liststr cdb::getlisteappsmetiers(){ sqlconnection ^ mysqlconnection; sqlcommand ^ mysql; sqldatareader ^ myreader; sqldataadapter ^ mydataadapter; dataset ^ mydataset; datarow ^ myrow; sqlparameter ^ myparameter; liststr::iterator i; liststr listeappsmetiers; try { mysqlconnection = gcnew sqlconnection("data source=nectarys-pc;initial catalog=monitoringn;integrated security=true;"); mysql = gcnew sqlcommand("select * appmetier", mysqlconnection); mysqlconnection->open(); // open connection myreader = mysql->executereader(); while (myreader->read()) listeappsmetiers.insert(listeappsmetiers.end(), myreader["chemin"]->tostring()); // error marked @ point, before insert(... return listeappsmetiers; } catch (exception ^ e) { console::write(e->tostring()); } __finally { myreader->close(); mysqlconnection->close(); } }
cdb.h
#using <mscorlib.dll> #using <system.dll> #using <system.data.dll> #using <system.xml.dll> #pragma warning (disable:4786) #include <list> #include <sstream> #include <time.h> #include <string> #include <iostream> #include <tchar.h> using namespace system; using namespace system::data; using namespace system::xml; using namespace system::data::sqlclient; typedef std::list<std::string> liststr; class cdb{ cdb(){} //ctor virtual ~cdb(){} public: void ajouteralerte(){} liststr getlisteappsmetiers(){} };
but, i've got errors on marked line , haven't found how fix them :
no instance of overloaded function "std::list<_ty, _alloc>::insert[with _ty=std::string, _alloc=std::allocator]" matches argument list argument types : (std::_list_iterator>>, system::string ^) object type is: liststr
any brilliant idea, please ?
as explained in comments above: you're trying insert
value myreader["chemin"]->tostring()
, of microsoft-invented type string^
, list of normal std::string
s. there's no implicit conversion between microsoft string
, std::string
; you'll have conversion explicitly.
luckily there clr library function this: marshal_as
.
#include <msclr/marshal_cppstd.h> ... while (myreader->read()) { listeappsmetiers.insert( listeappsmetiers.end(), msclr::interop::marshal_as<std::string>( myreader["chemin"]->tostring() )); }
the code in answer copied @sriwantha-attanayake's answer similar question: https://stackoverflow.com/a/8896629/1424877
Comments
Post a Comment