gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 15:07:56 27 4
gpt4 key购买 nike

我正在使用来自 cygwin 的 g++,我正在尝试编译一个 .cpp 文件,但我遇到了一个错误,

代码如下:

#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++ 进行编译时遇到的错误

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 位置。我认为这根本不重要,不是吗?有人可以告诉我如何编译和运行它吗?谢谢。

最佳答案

将函数定义移到 main 之外:

//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++ 中,不允许在另一个函数内定义函数。

关于c++ - 使用 g++ 编译 C++ 文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9502584/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com