小编Dav*_*ycc的帖子

基于函数循环的范围,它将数组作为值传递

我这些天自己学习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;
}
Run Code Online (Sandbox Code Playgroud)

错误消息:

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, …
Run Code Online (Sandbox Code Playgroud)

c++ for-loop c++11

6
推荐指数
1
解决办法
1732
查看次数

标签 统计

c++ ×1

c++11 ×1

for-loop ×1