我在編譯時出現此錯誤(G ++ 4.4.6):C++缺少初始化程序錯誤
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文件中聲明的對象。
你用'-std = C++ 0x'編譯嗎? – ildjarn 2012-04-11 22:43:33
我對C++有點新鮮,所以如果你能解釋一下-std = C++ 0x是什麼或者意味着非常感謝 – 2012-04-11 22:47:18
@PatMurray:基於範圍的for循環(你的第27行)是C++ 11的一個特性。您必須將'-std = C++ 0x'或(更新版本中的-std = c + 11)傳遞給編譯器才能使用C++ 11功能。不過,我建議首先將您的編譯器升級到新版本。 – 2012-04-11 22:49:18