使用G++编译时遇到以下错误

3

从我的之前的问题可以看出,我对编程有点陌生。我想问一下是否有人能帮助我解决最近遇到的问题。我正在尝试使用g++编译脚本main.cpp,但是我得到了以下错误:

Donny@Donny-PC /cygdrive/c/Users/Donny/Desktop/equation/equations/equations
$ g++ main.cpp -o don.exe
main.cpp:3:11: error: ‘::main’ must return ‘int’
 void main(){
       ^
main.cpp: In function ‘int main()’:
main.cpp:36:22: error: ‘pow’ was not declared in this scope
  float n=pow(10.0,9.0);
                  ^
main.cpp:43:27: error: ‘sin’ was not declared in this scope
  float R56=(lb1/sin(theta1)) * ((tan(theta1))-theta1) + (lb2/sin(theta1)) *                ((tan(theta1))-theta1) +
                       ^
main.cpp:43:44: error: ‘tan’ was not declared in this scope
  float R56=(lb1/sin(theta1)) * ((tan(theta1))-theta1) + (lb2/sin(theta1)) *     ((tan(theta1))-theta1) +
                                        ^
main.cpp:48:40: error: ‘cos’ was not declared in this scope
  d*((pow(tan(theta1),2))/cos(theta1)) +
                                    ^

奇怪的是,当使用Microsoft Visual Studio 2010 C++编译时,此代码可以正常工作。非常感谢您的帮助!
编辑:
因此,我已经修复了上面显示的很多错误,但我仍然有一点难以解决void main错误。以下是我的代码:
#include<iostream>
#include<cmath>
using namespace std;
void main(){
    float r, i, f, beta, alpha;
    cout<<"Enter value of R : ";............

任何帮助或示例将不胜感激。

2
第一个错误很容易处理;将您的main定义更改为int,并在成功完成后返回0。其他错误可能是由于您省略了<cmath>(我认为这些函数在其中定义)引起的。 - wolfPack88
奇怪的是它使用Microsoft Visual Studio 2010 C++编译成功了。 - andy256
我怀疑这一点。这两个编译器并不会有太大的差异,以至于允许一个void签名的主方法。 - NWard
1
@NWard,这是微软支持的非标准扩展。只要你的 main() 函数不尝试返回任何内容,它就可以被微软的 C++ 编译器允许。 - Charles E. Grant
你会惊讶于MSVC容许你逃避的很多愚蠢问题数量。我一直在使用gulp.watch编写代码时,在一个单独的终端窗口中用-Wall运行win-LLVM clang,以显示编译错误。 - Qix - MONICA WAS MISTREATED
显示剩余5条评论
1个回答

2
第一个错误应该不用解释。标准规定,main函数必须返回int类型,但您已将其声明为void类型。从您的main函数中返回0以表示正常终止。微软编译器在这一点上没有那么严格。
所有剩余的错误都可以通过使用#include <math.h>来解决。

1
但是由于代码是C++,因此#include <cmath>头文件更为合适。 - Zan Lynx
1
你不需要在main函数中显式地使用return 0return EXIT_SUCCESS - Robert Allan Hennigan Leahy
嗨,这是我为 <cmath> 做出的修改,但我需要帮助理解 void main 该怎么做:#include #include using namespace std; void main(){ float r, i, f, beta, alpha; cout<<"输入 R 的值:"; - user1886681
@user1886681请停止称之为"void main",这是一个int main - SirDarius

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接