首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用g++编译C++文件时出错

使用g++编译C++文件时出错
EN

Stack Overflow用户
提问于 2012-03-01 00:09:38
回答 1查看 539关注 0票数 0

我正在使用cygwin中的g++,我试图编译一个.cpp文件,但是我遇到了一个错误。

代码如下:

代码语言:javascript
复制
#include "randomc.h"
#include <time.h>             // Define time() 
#include <stdio.h>            // Define printf() 
#include <cstdlib>



int main(int argc, char *argv[] ) {
int seed = atoi(argv[1]);
int looptotal = atoi(argv[2]);

//initializes rng, I want to use argv[1] to set seed 
void CRandomMother::RandomInit (int seed) {
int i;
// loop for the amount of times set by looptotal and return random number
for (i = 0; i < looptotal; i++) {
double s;
s = Random();
printf("\n%f", s)
}

}
return 0;

}

以下是我在尝试使用cygwin终端和g++进行编译时遇到的错误

代码语言:javascript
复制
Administrator@WIN-19CEL322IRP /cygdrive/c/xampp/xampp/htdocs$  g++ ar.cpp -o prog
ar.cpp: In function `int main(int, char**)':
ar.cpp:13: error: a function-definition is not allowed here before '{' token
ar.cpp:13: error: expected `,' or `;' before '{' token

.cpp文件和头文件randomc.h位于我的xampp位置。我认为这一点都不重要,不是吗?有人能告诉我怎样才能编译并运行这个程序吗?谢谢。

EN

回答 1

Stack Overflow用户

发布于 2012-03-01 00:11:09

将函数定义移到main之外

代码语言:javascript
复制
//initializes rng, I want to use argv[1] to set seed 
void CRandomMother::RandomInit (int seed, int looptotal) {
   int i;
   // loop for the amount of times set by looptotal and return random number
   for (i = 0; i < looptotal; i++) {
      double s;
      s = Random();
      printf("\n%f", s)
   }
}

int main(int argc, char *argv[] ) {
   int seed = atoi(argv[1]);
   int looptotal = atoi(argv[2]);
   return 0;
}

错误消息对我来说似乎很清楚。

在C++中,不允许在另一个函数中定义函数。

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9502584

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档