2011-04-27 56 views
1

這是我正在編譯的程序。爲什麼這個C++程序不能編譯?

#include <iostream> 
#include <string> 
#include <vector> 
#include <unistd.h> 
using namespace std; 

vector<string> paramlist; 
const char *programname = "abc"; 

const char **args = new const char* [paramlist.size()+2]; // extra room for program name and sentinel 
args [0] = programname;   // by convention, args[0] is program name 
for (int j = 0; j < paramlist.size()+1; ++j)  // copy args 
args [j+1] = paramlist[j] .c_str(); 

args [paramlist.size()+1] = NULL; // end of arguments sentinel is NULL 

execv (programname, (char **)args); 

當我嘗試編譯我收到以下錯誤消息:

test.cpp:11: error: expected constructor, destructor, or type conversion before ‘=’ token 
test.cpp:12: error: expected unqualified-id before ‘for’ 
test.cpp:12: error: expected constructor, destructor, or type conversion before ‘<’ token 
test.cpp:12: error: expected unqualified-id before ‘++’ token 
test.cpp:15: error: array bound is not an integer constant 
test.cpp:15: error: expected constructor, destructor, or type conversion before ‘=’ token 
test.cpp:17: error: expected constructor, destructor, or type conversion before ‘(’ token 
+10

此代碼是否在一個函數內?你不能在函數外部任意運行代碼。 – GManNickG 2011-04-27 01:10:49

回答

2

你的程序包含代碼,但它需要包含在一個函數中。嘗試使用int main之類的函數將using namespace std;行後的所有代碼包裝起來。 Google爲任何「Hello world」C++示例查看示例。