繁体   English   中英

为什么这个C ++程序不能编译?

[英]Why won't this C++ program compile?

这是我要编译的程序。

#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

您至少需要一个int main函数

您的程序包含代码,但需要包含在函数中。 尝试在using namespace std;之后包装所有代码using namespace std; 符合int main类的函数。 Google为任何“ Hello world” C ++示例提供了示例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM