错误 C2664:'Strategy::Interphase':无法将参数 2 从 'char *' 转换为'char *[]'

Error C2664: 'Strategy::Interphase' : cannot convert parameter 2 from 'char *' to 'char *[]'

本文关键字:char 转换 Strategy C2664 Interphase 错误 参数      更新时间:2023-10-16

我从来都不太懂指针、字符串、字符等。我需要帮助解决这个错误。下面是代码片段…

string H = "פטיש";
string G = "Σφυρί";
bool Interphase(int argc, char * args[]); //DLL import from Analytical.a
char * Hcopy = new char[H.length() + 1]; 
std::strcpy(Hcopy, H.c_str());
char * Gcopy = new char[G.length() + 1]; 
std::strcpy(Gcopy, G.c_str());
while (Interphase(147, Hcopy) == true || Interphase(148, Gcopy) == true)//C2664 here!
    {// Do stuff...}

请注意,代码已更改,仅反映错误。

还有如何在Visual Studio 2012 Ultimate中编译而不使用
Warning C4566: character represented by universal-character-name 'u05E9' cannot be represented in the current code page (1252)

谢谢。

您的错误:

错误C2664: 'Strategy::Interphase':无法将参数2从'char *'转换为'char *[]'

意味着你给期望有char*[]的函数一个char*

bool Interphase(int argc, char * args[]);

注意第二个参数,它是指向数组(或数组的数组,或指针指向指针)的指针。

你的代码可以修改为给它指针的地址:

while (Interphase(1, &Hcopy) == true || Interphase(1, &Gcopy) == true)
    // ...

,但我怀疑这是如何使用的API。

我不熟悉这个函数,但我猜它更像:

const char** args = {"arg1", "arg2"};
Interphase(2, args);

你的警告:

警告C4566:由universal-character-name 'u05E9'表示的字符无法在当前代码页(1252)中表示

表示char不能保存非ascii字符。

如果使用Unicode,则必须使用宽字符和字符串(wchar_twstring)以允许更大的数字。否则,您将出现溢出并可能发生换行。

stringwstring的比较链接。


作为旁注,你真的不应该使用c风格的字符串(char*)。使用std::string代替。原因:

  • 需要自己释放它们(你的代码泄漏内存)
  • 不安全的api(需要将大小与数据一起传递)