【问题标题】:I don't know how to call my own function! C++ [duplicate]我不知道如何调用我自己的函数! C++ [重复]
【发布时间】:2014-04-02 06:26:55
【问题描述】:

这是我在这里的第一个问题。所以,我正在尝试制作一个函数,它从像这样“5 5 4 2”这样写的文本框中获取数字,并将它们分成单独的整数,然后计算平均值。到目前为止我所做的是:

double prosjek(string a)
    {
    string razmak = " "; //the string separator, space between the numbers
    string token = a.substr(0, a.find(razmak)); //finds those separators in the textbox
    size_t pos = 0; //sets the position to zero
    int suma=0; //initializes the sum
    int brojac=0; //initializes the counter
    while ((pos = a.find(razmak)) != std::string::npos) { //loops through string and separates the numbers
        token = a.substr(0, pos);
        int numb;
        istringstream ( token ) >> numb;
        suma+=numb;
        a.erase(0, pos + razmak.length());
        brojac++;
    }
    double prosjek=suma/brojac;
    return prosjek; //returns the average
}

我不知道如何为特定的文本框调用此函数。我试过这个:

txtAverage->Text=prosjek(txtWithNumbers->Text->ToString);

但我从 IntelliSense 收到此错误消息: 错误 1 ​​错误 C3867:'System::String::ToString':函数调用缺少参数列表;使用 '&System::String::ToString' 创建指向成员的指针

编辑:

更新的代码(仍需修复):

string RefStringToNativeString(System::String const^ s)
        {
        return msclr::interop::marshal_as<std::string>(s);
        }

String^ NativeStringToRefString(const std::string& s)
        {
        System::String^ result = gcnew System::String(s.c_str());
        return result;
        }

string prosjek(string a)
    {
        string razmak = " ";
        string token = a.substr(0, a.find(razmak));
        size_t pos = 0;
        int suma=0;
        int brojac=0;
        while ((pos = a.find(razmak)) != std::string::npos) {
            token = a.substr(0, pos);
            int numb;
            istringstream ( token ) >> numb;
            suma+=numb;
            a.erase(0, pos + razmak.length());
            brojac++;
        }
        double pr=suma/brojac;
        return pr.ToString();
    }


private: System::Void btnIzrPr_Click(System::Object^  sender, System::EventArgs^  e) {

    txtAverage->Text = NativeStringToRefString(prosjek(RefStringToNativeString(txtWithNumbers->Text)));

}

【问题讨论】:

  • 这看起来不像 C++。不是 C++/CLI 吗?
  • 根据下面的评论,是的

标签: c++ .net c++-cli


【解决方案1】:

您实际上是在使用 C++/CLI 而不是 C++ 进行编码。您正在尝试从 .net 托管的 ref 字符串转换为 C++ 字符串。这样做:

#include <msclr/marshal_cppstd.h>

std::string RefStringToNativeString(System::String^ s)
{
    return msclr::interop::marshal_as<std::string>(s);
}

之后,您将面临将 double 转换为托管 ref 字符串的问题。好吧,假设您可以将双精度转换为 C++ 字符串。那么你需要:

System::String^ NativeStringToRefString(const std::string& s)
{
    return gcnew System::String(s.c_str());
}

你可以像这样绕过第二个函数:

txtAverage->Text = 
    prosjek(RefStringToNativeString(txtWithNumbers->Text)).ToString();

如果您真的要使用 C++/CLI,那么您不妨使用 .net 字符串而不是 C++ 字符串来避免所有这些来回。

【讨论】:

  • 所以调用应该是这样的:txtAverage->Text = NativeStringToRefString(prosjek(RefStringToNativeString(txtWithNumbers->Text)));
  • 差不多了。您仍然需要将 prosjek() 的返回值从 double 转换为 std::string。或者直接使用System::String.Format进行转换。
  • 像这样:double pr=suma/brojac;返回 pr.ToString;和 ofc 字符串 prosjek(....){...}
  • 例如,d.ToString()System::String::Format("{0}", d)
  • 我收到此错误消息:5 IntelliSense:不存在合适的构造函数来将“System::String ^”转换为“std::basic_string, std: :allocator>"
【解决方案2】:

试试:

txtAverage->Text = prosjek(txtWithNumbers->Text->ToString());
//                                                      ^^^^

【讨论】:

  • 我仍然收到同样的错误信息 + 这个:4 IntelliSense: function prosjek" cannot be called with the given argument list argument types are: (System::String ^)
  • std::string ns("Hello");字符串^ ms(ns.c_str());
  • 使用命名空间 System::Runtime::InteropServices;
  • @user3487018:那不是 C++。
  • @lizusek 不,你不想在这里使用Marshal::PtrToStringAnsi
猜你喜欢
  • 1970-01-01
  • 2023-01-03
  • 1970-01-01
  • 2021-09-26
  • 2016-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多