【问题标题】:error C2064: term does not evaluate to a function taking 3 arguments错误 C2064:术语不计算为采用 3 个参数的函数
【发布时间】:2020-02-19 08:44:18
【问题描述】:

环境: Visual Studio 2008 专业版

我正在尝试调试十六进制到十进制的转换,但不幸的是得到“术语不评估为采用 3 个参数的函数”这个错误。谁能建议如何解决这个问题?

代码:

#include <string>
using namespace std;

int main()
{
    int stoi;
    int number = 0;

    string hex_string = "12345";
    number = stoi(hex_string, 0, 16);
    cout << "hex_string: " << hex_string << endl;
    cout << "number: " << number << endl;

    return 0;
}

【问题讨论】:

  • stoi 是一个整数,你为什么要把它当作一个函数来调用?
  • @VTT 如果未声明它会给出类似“错误 C3861: 'stoi': identifier not found”的错误
  • 你想使用 std::stoi en.cppreference.com/w/cpp/string/basic_string/stol (使用命名空间 std 错误的另一个原因)
  • @Adderbill 不,它没有 - wandbox.org/permlink/p0BmWNaYTAaFm8HR
  • 对 C++11(其中引入了 stoi)的支持充其量是有限的。您需要不同的解决方案或不同的编译器。 (请注意,如果这是学校练习,您应该自己进行转换,而不是使用库。)

标签: c++ visual-studio type-conversion hex decimal


【解决方案1】:

这就是为什么你不应该这样做using namespace std;。通过将 std:: 放在 std 命名空间中的所有内容之前,摆脱它并修复程序。

#include <iostream>
#include <string>

int main()
{
    int stoi;
    int number = 0;

    std::string hex_string = "12345";
    number = std::stoi(hex_string, 0, 16);
    std::cout << "hex_string: " << hex_string << std::endl;
    std::cout << "number: " << number << std::endl;

    return 0;
}

您也可以将 stoi 整数重命名为不与 std::stoi 冲突,但将 it is strongly recommended 重命名为在您的代码中不包含 using namespace std;

如果您因为Visual Studio 2008 doesn't support C++11 而根本无法使用stoi,并且您无法升级到较新的版本,请参阅here 了解替代方案。但从长远来看,如果可能,安装更新的 IDE 可能会更好。

【讨论】:

  • 现在的错误类似于 1]error C2039: 'stoi' : is not a member of 'std' 2]error C2064: term doesn't evaluate to a function with 3 arguments
  • 你的版本是什么?您至少需要 C++11 才能使用stoi
  • 你应该使用 C++11 或更高版本
  • @Blaze 我有 Microsoft Visual C++ 10.0.40219
  • @Adderbill 在这种情况下您不能使用 std::stoi 并且必须使用替代方案。请参阅here 获取一些建议,尤其是this 答案。
【解决方案2】:

因为 stoi 是来自 string library 的函数,所以您不会将 stoi 重新定义为 int stoi。删除int stoi就成功了。

像这样的完整代码

#include <string>
#include <iostream>
using namespace std;

int main()
{

    int number = 0;

    string hex_string = "12345";
    number = stoi(hex_string, nullptr, 16);
    cout << "hex_string: " << hex_string << endl;
    cout << "number: " << number << endl;

    return 0;
}

【讨论】:

  • 1] 错误 C2039: 'stoi' : is not a member of 'std' and 2] 错误 C3861: 'stoi': identifier not found, 这是我删除 int stoi 时的错误跨度>
  • @Adderbill 您能否根据我们的建议编辑您的发布,之后我们可以为您提供帮助
【解决方案3】:

感谢大家的回复! 在 Visual Studio 2008 上成功调试的最终代码,用于将十六进制转换为十进制

#include <iostream>
using namespace std ;
#include <sstream>

int wmain() {
int binNumber ;
unsigned int decimal;
string hexString = "0x3d"; //you may or may not add 0x before
stringstream myStream;
myStream <<hex <<hexString;
myStream >>binNumber;
cout <<binNumber <<decimal;
return 0;
}

【讨论】:

    猜你喜欢
    • 2011-09-27
    • 1970-01-01
    • 2018-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多