【发布时间】:2018-01-17 19:48:33
【问题描述】:
我试图创建一个非常简单的程序来计算 C++ 中两个对象之间的引力,所以我想为 G 声明一个宏,其值等于 6.754*10^-11,我使用了这个 -
#define G 6.754e-11.0;
但是它产生了很多如下所示的杂散错误-
aalpanigrahi@aalpanigrahi:~/Desktop/CS/C++/Other PSETS$ g++ gravity.cpp -o gravity
gravity.cpp:5:28: error: stray ‘\342’ in program
#define G (6.754)*pow(10.0,��11.0);
^
gravity.cpp:10:52: note: in expansion of macro ‘G’
float gforce(float m1,float m2,float r){return G*m1*m2/(r*r);}
^
gravity.cpp:5:29: error: stray ‘\210’ in program
#define G (6.754)*pow(10.0,��11.0);
^
gravity.cpp:10:52: note: in expansion of macro ‘G’
float gforce(float m1,float m2,float r){return G*m1*m2/(r*r);}
^
gravity.cpp:5:30: error: stray ‘\222’ in program
#define G (6.754)*pow(10.0,�11.0);
^
gravity.cpp:10:52: note: in expansion of macro ‘G’
float gforce(float m1,float m2,float r){return G*m1*m2/(r*r);}
所以,我决定使用数学库函数 cmath 来使用 pow() ,但它仍然没有用。我该怎么做才能创建这样一个宏?
注意 - 我在 Ubuntu 上使用 G++,如下所示 -
aalpanigrahi@aalpanigrahi:~/Desktop/CS/C++/Other PSETS$ g++ --version
g++ (Ubuntu 7.2.0-8ubuntu3) 7.2.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
【问题讨论】:
-
删除
#define...行末尾的; -
也不要使用宏。
-
我强烈建议不要使用单字母名称,例如
G。 -
stray ‘\342’ in program您是否从网站复制和粘贴?有时在网站上使用看起来相同但不同的字符,主要使用”而不是"