我已经编写了一个利用Fast Light Toolkit的小程序,由于某种原因,当尝试访问cmath头中的函数时,会生成一个编译器错误。
如error ::acos尚未声明。
这对于它试图在头中使用的几乎每个函数都是如此。我能错过什么呢?
我已经包含的头文件是
Simple_window.h
Graph.h这两个都是FLTK的一部分。
代码是这样的:
#include "Simple_window.h" // get access to our windows library
#include "Graph.h" // get access to graphics library facilities
int main()
{
using namespace Graph_lib; // our graphics facilities are in Graph_lib
Point tl(100,100); // to become top left corner of window
Simple_window win(tl,600,400,"Canvas"); // make a simple window
Polygon poly; // make a shape (a polygon)
poly.add(Point(300,200)); // add a point
poly.add(Point(350,100)); // add another point
poly.add(Point(400,200)); // add a third point
poly.set_color(Color::red); // adjust properties of poly
win.attach(poly); // connect poly to the window
win.wait_for_button(); // give control to display engine
}编辑:以下是生成编译器错误时的示例代码。这在cmath头文件中。
namespace std
{
// Forward declaration of a helper function. This really should be
// an `exported' forward declaration.
template<typename _Tp> _Tp __cmath_power(_Tp, unsigned int);
inline double
abs(double __x)
{ return __builtin_fabs(__x); }
inline float
abs(float __x)
{ return __builtin_fabsf(__x); }
inline long double
abs(long double __x)
{ return __builtin_fabsl(__x); }
using ::acos; //ERROR HERE
inline float
acos(float __x)
{ return __builtin_acosf(__x); }
inline long double
acos(long double __x)
{ return __builtin_acosl(__x); }
template<typename _Tp>
inline typename __enable_if<double, __is_integer<_Tp>::_M_type>::_M_type
acos(_Tp __x)
{
return __builtin_acos(__x);
}Edit: Code::blocks正在将文件保存为C文件....
发布于 2009-08-01 17:00:06
当您包含标准C库的C++版本()时,所有符号都在std名称空间中定义。在C++中,您不需要链接数学库(不需要-lm)
#include <cmath>
#include <iostream>
int main()
{
std::cout << std::fabs( -10.5 ) << std::endl;
}发布于 2013-09-24 03:52:57
我遇到了这个问题-它让我抓狂,但我找到了原因,这与我在这个问题上看到的报道略有不同。
在这种情况下,一般的cmath.h头(或math.h -在C++或C中出现的错误和解决方案)具有架构环境开关,以包括特定于架构的数学子标头。体系结构开关(环境变量)尚未定义,因此它实际上并不包括真正定义数学函数的头部。
所以确实只有一个math.h或cmath.h,并且包含了它,但这还不足以获得数学函数。在我的例子中,我没有定义体系结构变量,而是找到了正确的子数学头的位置,并将它们添加到我的编译路径中。然后这个项目成功了!
在将Linux项目移植到OS-X时,这似乎是一个经常出现的问题。我可以想象,当一个项目在不同的平台之间移动,使得标准库的头以不同的方式排列时,就可能发生这种情况。
发布于 2009-08-01 16:20:33
由于如上所示的代码不直接调用acos(),因此您确实使用的其中一个头文件中可能存在错误。在其中一个头文件中似乎有一些(内联的)代码调用acos()函数,但没有确保该函数被正确声明。这可能是宏或内联函数。
最好的修复方法是确保报头是自包含的--更改报头。
如果这是不可能的,权宜之计是在源代码中包含适当的头文件(可能是#include <cmath>)。
如果程序能够访问cmath报头,错误就出在
报头本身。
在这种情况下,您可能需要提供一个调用std::acos()的全局acos()函数(至少是声明,也可能是定义
double acos(double x) { return std::acos(x); }只需确保它不在任何名称空间中-甚至不是匿名名称空间。(检查在MacOS X上用G++ 4.0.1编译,前面有'#include <cmath>‘。假设你有一个有问题的<cmath>头,你可能需要弄清楚:
extern double std::acos(double);
double acos(double x) { return std::acos(x); }
#include <cmath>这非常糟糕--你确定你的编译器没有修复错误的版本吗?
有没有可能在名称空间中包含'#include <cmath>‘?
https://stackoverflow.com/questions/1216878
复制相似问题