gpt4 book ai didi

c++ - 为什么包含任意 STL header 可以解决这些编译错误?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:03:41 26 4
gpt4 key购买 nike

我的程序中有这个全局函数:

static bool IsValidType(const CString& cType)
{
for (auto pType : {"bmp","jpg","jpeg","gif","tif","tiff","png"})
if (cType == CString(pType))
return true;
return false;
}

它给了我以下编译错误:

error C3312: no callable 'begin' function found for type 'initializer-list'
error C3312: no callable 'end' function found for type 'initializer-list'
error C2065: 'pType' : undeclared identifier

我可以通过在函数体之前包含一个任意的 STL 头来解决它,例如:

#include <string>
static bool IsValidType(const CString& cType)
{
...
}

当然,我认为这不是正确的做法。

您能否向我解释为什么包含任意 STL header 可以解决此问题,以及我应该如何正确解决它?

谢谢。

最佳答案

由于您使用的是 initializer_list你应该包括 initializer_list

#include <initializer_list>

Including string resolve the error as string probably includes initializer_list, 但这种间接包含不是推荐的方式。

关于c++ - 为什么包含任意 STL header 可以解决这些编译错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45659489/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com