gpt4 book ai didi

c++ - 将数组作为值传递的函数中基于范围的 for 循环

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:07:07 25 4
gpt4 key购买 nike

<分区>

这些天我正在自学 C++,我无法理解为什么这段代码不能使用 #g++ -std=c++11 source.cpp 进行编译。实际上,我使用哪个特定标准并不重要,它只是无法编译。

#include <iostream>
#include <string>
using namespace std;
int print_a(char array[])
{
for(char c : array)
cout << c;
cout << endl;
return 0;
}
int main(void)
{
char hello[] {"Hello!"};
print_a(hello);
return 0;
}

错误信息:

debian@debian:~/Documents$ g++ -std=c++11 source.cpp
source.cpp: In function ‘int print_a(char*)’:
source.cpp:6:15: error: ‘begin’ was not declared in this scope
for(char c : array)
^
source.cpp:6:15: note: suggested alternatives:
In file included from /usr/include/c++/4.9/bits/basic_string.h:42:0,
from /usr/include/c++/4.9/string:52,
from /usr/include/c++/4.9/bits/locale_classes.h:40,
from /usr/include/c++/4.9/bits/ios_base.h:41,
from /usr/include/c++/4.9/ios:42,
from /usr/include/c++/4.9/ostream:38,
from /usr/include/c++/4.9/iostream:39,
from source.cpp:1:
/usr/include/c++/4.9/initializer_list:89:5: note: ‘std::begin’
begin(initializer_list<_Tp> __ils) noexcept
^
/usr/include/c++/4.9/initializer_list:89:5: note: ‘std::begin’
source.cpp:6:15: error: ‘end’ was not declared in this scope
for(char c : array)
^
source.cpp:6:15: note: suggested alternatives:
In file included from /usr/include/c++/4.9/bits/basic_string.h:42:0,
from /usr/include/c++/4.9/string:52,
from /usr/include/c++/4.9/bits/locale_classes.h:40,
from /usr/include/c++/4.9/bits/ios_base.h:41,
from /usr/include/c++/4.9/ios:42,
from /usr/include/c++/4.9/ostream:38,
from /usr/include/c++/4.9/iostream:39,
from source.cpp:1:
/usr/include/c++/4.9/initializer_list:99:5: note: ‘std::end’
end(initializer_list<_Tp> __ils) noexcept
^
/usr/include/c++/4.9/initializer_list:99:5: note: ‘std::end’

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