简体   繁体   English

错误:没有重载函数的实例

[英]Error : no instance of overloaded function

I'm trying to insert some lines in my Database (SQL Server 2008) with this code : 我正在尝试使用以下代码在我的数据库(SQL Server 2008)中插入一些行:

CDB.cpp 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 up the connection
        mySQLConnection->Open();

        myDataAdapter->SelectCommand = gcnew SqlCommand("select * from Alerte", mySQLConnection);
        myDataAdapter->InsertCommand = gcnew SqlCommand("insert into 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 now = time(0);
        tm ltm;
        localtime_s(&ltm, &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 for the 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 * from AppMetier", mySQLConnection);

        mySQLConnection->Open(); // Open up the connection
        myReader = mySQL->ExecuteReader();

        while (myReader->Read())
            listeAppsMetiers.insert(listeAppsMetiers.end(), myReader["chemin"]->ToString()); // the error is marked at the point, just before insert(...

        return listeAppsMetiers;
    }
    catch (Exception ^ e)
    {
        Console::Write(e->ToString());
    }
    __finally
    {
        myReader->Close();
        mySQLConnection->Close();
    }
}

CDB.h 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 the marked line and I haven't found how to fix them : 但是,我在标记的行上有错误,我还没有找到解决方法:

no instance of overloaded function "std::list<_Ty, _Alloc>::insert[with _Ty=std::string, _Alloc=std::allocator]" matches the argument list argument types are : (std::_List_iterator>>, System::String ^) object type is: LISTSTR 没有重载函数的实例“std :: list <_Ty,_Alloc> :: insert [with _Ty = std :: string,_Alloc = std :: allocator]”匹配参数列表参数类型是:(std :: _ List_iterator >> ,System :: String ^)对象类型是:LISTSTR

Any brilliant idea, please ? 请问有什么好主意吗?

As explained in the comments above: You're trying to insert the value myReader["chemin"]->ToString() , which is of the Microsoft-invented type String^ , into a list of normal std::string s. 正如上面的注释中所解释的那样:您正在尝试insert值为myReader["chemin"]->ToString() (属于Microsoft发明的类型String^ )插入到正常的std::string s列表中。 There's no implicit conversion between Microsoft String and std::string ; Microsoft Stringstd::string之间没有隐式转换; you'll have to do the conversion explicitly. 你必须明确地进行转换。

Luckily there is a CLR library function to do this: marshal_as . 幸运的是,有一个CLR库函数可以做到这一点: 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 this answer is copied from @sriwantha-attanayake's answer to this similar question: https://stackoverflow.com/a/8896629/1424877 这个答案中的代码复制自@ sriwantha-attanayake对这个类似问题的回答: https ://stackoverflow.com/a/8896629/1424877

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM