相关疑难解决方法(0)

使用成员函数作为比较器排序问题

试图编译以下代码我得到这个编译错误,我该怎么办?


ISO C++禁止获取非限定或带括号的非静态成员函数的地址,以形成指向成员函数的指针.

class MyClass {
   int * arr;
   // other member variables
   MyClass() { arr = new int[someSize]; }

   doCompare( const int & i1, const int & i2 ) { // use some member variables } 

   doSort() { std::sort(arr,arr+someSize, &doCompare); }

}; 
Run Code Online (Sandbox Code Playgroud)

c++ sorting stl compiler-errors

30
推荐指数
4
解决办法
3万
查看次数

abs和fabs有什么区别?

我在这里检查了absfabspython 之间的区别

据我所知,速度和传递的类型有一些不同,但我的问题与VS上的原生c ++有关

关于VS我尝试了以下内容Visual Studio 2013 (v120):

float f1= abs(-9.2); // f = 9.2
float f2= fabs(-9); // Compile error [*]
Run Code Online (Sandbox Code Playgroud)

所以fabs(-9)它会给我一个编译器错误,但是当我尝试执行以下操作时:

double i = -9;
float f2= fabs(i); // This will work fine
Run Code Online (Sandbox Code Playgroud)

我从第一个代码中理解它不会编译因为fabs(-9)需要一个double,并且编译器无法将-9转换为-9.0,但在第二个代码中编译器将在编译时转换i=-9i=-9.0所以fabs(i)将正常工作.

有更好的解释吗?

另一件事,为什么编译器不能fabs(-9)像在c#中那样自动接受 并将int值转换为double?

[*]:

Error: more than one instance of overloaded function "fabs" matches the argument list:
        function "fabs(double _X)"
        function "fabs(float _X)"
        function …
Run Code Online (Sandbox Code Playgroud)

c++ cmath visual-studio-2013

18
推荐指数
2
解决办法
2万
查看次数

标签 统计

c++ ×2

cmath ×1

compiler-errors ×1

sorting ×1

stl ×1

visual-studio-2013 ×1