C ++缺少初始化程序错误

时间:2012-04-11 22:42:36

标签: c++ debugging

我在编译期间收到此错误(g ++ 4.4.6):

main.cpp: In function ‘int main()’:
main.cpp:27: error: expected initializer before ‘:’ token
main.cpp:33: error: expected primary-expression before ‘for’
main.cpp:33: error: expected ‘;’ before ‘for’
main.cpp:33: error: expected primary-expression before ‘for’
main.cpp:33: error: expected ‘)’ before ‘for’
main.cpp:33: error: expected initializer before ‘:’ token
main.cpp:36: error: could not convert ‘((list != 0u) ? (list->SortedList::~SortedList(), operator delete(((void*)list))) : 0)’ to ‘bool’
main.cpp:37: error: expected primary-expression before ‘return’
main.cpp:37: error: expected ‘)’ before ‘return’

我的代码如下:

#include <iostream>
#include "Student.h"
#include "SortedList.h"

using namespace std;

int main() {
    SortedList *list = new SortedList();

    Student create[100];
    int num = 100000;

    for (Student &x : create) { // <--Line 27
        x = new Student(num);
        num += 10;
    }

    for (Student &x : create)
    list->insert(&x);

    delete list;
    return 0;
}

任何可能知道错误来源的人都会有很大的帮助。此外,Student和SortedList是在.h文件中声明的对象。

1 个答案:

答案 0 :(得分:6)

根据this page on GCC's website,基于范围的for仅适用于g ++ 4.6及更高版本,因此您必须将代码转换为普通for循环或使用std::for_each或某事,或升级你的编译器。