Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在C++;?_C++_Math_Syntax - Fatal编程技术网

C++ 如何在C++;?

C++ 如何在C++;?,c++,math,syntax,C++,Math,Syntax,守则: #define e 2.71828183; double p ( int x ) { return 1 / ( 1 + pow ( e, -1.0 * x ) ); } 我得到: math.cpp: In function ‘double p(int)’: math.cpp:11: error: expected ‘)’ before ‘;’ token math.cpp:11: error: expected ‘)’ before ‘;’ token math.cpp:11

守则:

#define e 2.71828183;

double p ( int x )
{
    return 1 / ( 1 + pow ( e, -1.0 * x ) );
}
我得到:

math.cpp: In function ‘double p(int)’:
math.cpp:11: error: expected ‘)’ before ‘;’ token
math.cpp:11: error: expected ‘)’ before ‘;’ token
math.cpp:11: error: expected primary-expression before ‘,’ token
math.cpp:11: error: expected ‘;’ before ‘)’ token

有一个
在宏替换结束时:

#define e 2.71828183;
在预处理时,返回语句将如下所示:

return 1 / ( 1 + pow ( 2.71828183;, -1.0 * x ) );
                                 ^^
这会导致语法错误


要修复此问题,请删除

宏不应该有分号。

您的问题是关于C++:

在这里,您可以看到宏替换的问题。相反,请使用常量:

double const e = 2.71828183;

具体来说,删除分号。
#define e
可能不是一个好主意。用
exp()
代替
pow(e,…)
怎么样?(这并不是为了回答您的问题。)更喜欢“const double e=2.71828183;”这不会有您的答案problem@stefaanv:见鬼,也把它放在命名空间中:
namespace math{const double e=2.71828183;}
注意使用
const double
的原因是#define是一个残酷的工具,它将用该数字替换在代码中找到的任何其他“e”