C++出现未知错误,错误:令牌之前的预期主表达式';'
Unknown error on C++, error: expected primary expression before ';' token
C++上的未知错误,错误:';"标记之前的预期主表达式。这是我用C++编写的代码:
#include <iostream>
#include <math.h>
#include <stdio.h>
#define G 6.674E-11
using namespace std;
int main()
{
//Ms = Mass of sun, Me = Mass of Earth, Fg = Gravitational force between them, As = Acceleration of Sun, Ae = Acceleration of Earth, Ve_x
// = initial velocity of Earth in x direction, Ve_y = initial velocity of Earth in y direction, Vs_x = initial velocity of the Sun in x direction
// Vs_y = initial velocity of sun in y direction, t = time, F = Gravitational force ` between the two bodies.
float Ms, Me, Fg, As, Ae, Ve_x, Ve_y, Vs_x, Vs_y, pos_E, pos_S, r_x, r_y, r, t;
float S_dist;
float E_dist;
float F;
float Ve[2];
float Vs[2];
float pe[2];
float ps[2];
FILE *fileptr;
cout <<"Enter mass of the Sun in kgn";
cin >> Ms;
cout <<"Enter mass of the earth in kgn";
cin >> Me;
cout <<"Enter intial velocity of the sun in x direction in m/sn";
cin >> Vs[0];
cout <<"Enter intial velocity of the sun in y direction in m/sn";
cin >> Vs[1];
cout <<"Enter intial velocity of the earth in x direction in m/sn";
cin >> Ve[0];
cout <<"Enter intial velocity of the earth in y direction in m/sn";
cin >> Ve[1];
cout <<"Enter intial position of the sun in x componentn";
cin >> ps[0];
cout <<"Enter intial position of the sun in y directionn";
cin >> ps[1];
cout <<"Enter intial position of the earth in x directionn";
cin >> pe[0];
cout <<"Enter intial position of the earth in y directionn";
cin >> pe[1];
for (t=0; t<30000; t+1)
{
float E_dist;
float S_dist;
float F;
E_dist=sqrt( ((pe[0]-pe[0])*(pe[0]-pe[0])) + ((pe[1]-pe[1])*(pe[1]-pe[1])) );
S_dist=sqrt( ((ps[0]-ps[0])*(ps[0]-ps[0])) + ((ps[1]-ps[1])*(ps[1]-ps[1])) );
r_x=( (pe[0]-pe[0]) - (ps[0]-ps[0]) );
r_y=( (pe[1]-pe[1]) - (ps[1]-ps[1]) );
r= sqrt( (r_x)*(r_x) + (r_y)*(r_y) );
F=(G*Me*Ms)/(r*r);
Ae = F/Me;
As = F/Ms;
Ve_x = Ve[0];
Ve_y = Ve[1];
Vs_x = Vs[0];
Vs_y = Vs[1];
}
cout<<"At time "<<t<<":n The position of the Sun is "<<S_dist<<"n The position of the Earth is "<<E_dist
<<"n The acceleration of the Sun is "<<As<<" n The acceleration of the Earth is "<<Ae<<" nThe velocity of the Sun in the x direction is "
<<Vs_x<<" n The velocity of the Sun in the y direction is "<<Vs_y<<" n The velocity of the Earth in the x direction is "<<Ve_x<<
" n The velocity of the Earth in the y direction is "<<Ve_y<<" n The gravitational force between the Sun and the Earth is "<<F<<; // ERROR OCCURRED HERE.
}
如有任何帮助,将不胜感激,谢谢。
我认为错误是您的最后一行以这样的结尾:
<<F<<;
请注意,<<
运算符仅应用于一个参数。 你的意思是写这样的东西吗?
<<F<<endl;
对于它的价值,我强烈建议将该输出行拆分为多行以清楚起见。 你现在拥有的是正确的,但它非常难以阅读。 将其重写为
cout << "At time " <<t<<":n The position of the Sun is "<<S_dist<<"n";
<< " The position of the Earth is "<<E_dist << "n";
<< "The acceleration of the Sun is "<<As<<"n"
<< "The acceleration of the Earth is "<<Ae<<"n";
<< "The velocity of the Sun in the x direction is "<<Vs_x<<" n";
<< "The velocity of the Sun in the y direction is "<<Vs_y<<" n";
<< "The velocity of the Earth in the x direction is "<<Ve_x<< "n";
<< "The velocity of the Earth in the y direction is "<<Ve_y<<" n";
<< "The gravitational force between the Sun and the Earth is "<<F<<;
会使此错误更容易发现,因为行号信息会更有用。 另外,我建议在<<
运算符之间添加空格,以使其更易于阅读。
templatetypedef 指出了编译器错误,但代码中还有另一个问题:for
循环是无限的:
for (t=0; t<30000; t+1)
应该是:
for (t=0; t<30000; t++)
或者由于t
是一个float
,基于在浮点上使用增量(运算符++)是否不好的样式?
for (t = 0; t < 30000; t+=1.0f)
您的第一个语法错误是因为语句末尾有另一个<<
cout<<F<<;
将其更改为 cout<<F;
,除非您有另一个 var 要打印或您想使用像 cout<<F<<endl;
这样的结束行。
你还有另一个语法错误!您的main
返回 int,但您最后没有返回任何内容:
return 0;
另一个非常非常重要的问题是关于变量F
!
逻辑错误!!
int main()
{
....
float S_dist; //real S_dist !!!
float E_dist; //real E_dist !!!
float F; //real F !!!
for (t=0; t<30000; t++)
{
float F; // just exists in for!!!
float E_dist; //just exists in for!!!
float E_dist; //just exists in for!!!
....
F=(G*Me*Ms)/(r*r); //changing local F, which just exists in for
E_dist=sqrt( ((pe[0]-pe[0])*(pe[0]-pe[0])) + ((pe[1]-pe[1])*(pe[1]-pe[1])) );
S_dist=sqrt( ((ps[0]-ps[0])*(ps[0]-ps[0])) + ((ps[1]-ps[1])*(ps[1]-ps[1])) );
}
cout<<F<<E_dist<<S_dist; //this prints the main vars!
return 0;
}
请务必阅读更多有关局部变量的信息。请记住,t+1
根本不会改变t
!您应该在for
语句的最后一部分写t++
或t+=1
!除非你会得到一个无限循环,否则你的t
根本不会增长!
相关文章:
- 在函数中使用 const int size 参数创建数组会在 Visual Studio 中抛出错误 C++:表达式的计
- 错误:表达式必须是可修改的 I值
- 错误:表达式必须具有算术、无作用域枚举或带有运算符重载的指针类型
- 错误:表达式不能用作函数
- C++/错误:表达式必须具有整数或无作用域枚举类型
- 错误表达式必须是插入函数(二叉树 C++)中的可修改 Ivalue
- 合并排序错误 - 表达式必须具有常量值
- 数组声明中的错误:表达式必须具有常量值
- 错误:表达式列表在初始化器[-fpermissive] Double Paycalc(p,yir,y)中被视为复合表达式
- 错误:表达式不能用作函数
- 我该如何解决此错误:表达式必须具有恒定值.环境是2013年
- 错误:表达式必须是可修改的LVALUE
- 错误:表达式必须具有指向类C++的指针
- 错误:表达式不能用作函数
- 错误:表达式必须具有积分或未引人注目的枚举类型
- 参数化sc_lv时报告错误(表达式必须具有常量值)
- 无法使用函数变量?错误表达式必须具有类类型
- 错误:表达式必须具有类类型
- 错误:表达式不能用作函数.如何删除它
- C++ - 错误:表达式必须具有指向对象的指针类型