为什么我需要:: sin,而不是std :: sin? [英] why do I need ::sin, not std::sin?

查看:128
本文介绍了为什么我需要:: sin,而不是std :: sin?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




简单的问题。为什么这不起作用:


#include< algorithm>

#include< cmath>

#include< iostream>


int

main()

{

double a [2];

a [0] = 0; a [1] = M_PI / 2.。

std :: transform(a,a + 2,a,std :: sin);

std :: cout<< a [0]<< , << a [1]<< std :: endl;

返回0;

}


for gcc和icc变换的第三个参数的类型是未知。如果我用
使用global :: sin它可以正常工作。为什么?我包括cmath,并且在命名空间std中定义了罪恶是否为
,为什么它不知道?


问候,

alex

解决方案

Alexander Stippler写道:



简单的问题。为什么这不起作用:

#include< algorithm>
#include< cmath>
#include< iostream>

int
main()
{
双a [2];
a [0] = 0; a [1] = M_PI / 2 .;
std :: transform (a,a + 2,a,std :: sin);
std :: cout<< a [0]<< , << a [1]<< std :: endl;
返回0;

对于gcc和icc,变换的第三个参数的类型是未知的。如果我使用global :: sin它可以正常工作。为什么?我包括cmath,并且在命名空间std中定义了sin,所以为什么它不为人知?

问候,
alex




IANAG(我不是大师),我可能会离开基地(请更正我

伙计们 - 请不要火焰)。


难道std :: sin是模板吗?你需要专门实例化

std :: sin,即


std :: transform(a,a + 2,a,std :: sin< double>)


您可能还想使用std :: ptr_fun作为std :: sin(或:: sin)的包装器。


Alexander Stippler在新闻中写道:40 ****** @ news.uni-ulm.de in

comp.lang.c ++:
< blockquote class =post_quotes>

简单的问题。为什么这不起作用:

#include< algorithm>
#include< cmath>
#include< iostream>

int
main()
{
双a [2];
a [0] = 0; a [1] = M_PI / 2 .;
std :: transform (a,a + 2,a,std :: sin);
std :: cout<< a [0]<< , << a [1]<< std :: endl;
返回0;
}
对于gcc和icc,变换的第三个参数的类型是未知的。如果我使用global :: sin它可以正常工作。为什么?我包括cmath
并且在命名空间std中定义了sin,为什么它不为人知?

问候,
alex




std :: sin有几个重载,你需要选择一个:


std :: transform(a,a + 2,a,(double) (*)(double)std :: sin);


注意M_PI不是AFAICT标准C ++,你可以使用:


double const M_PI =(2.0 * std :: acos(0.0));


虽然可能:


double const double_pi =(2.0 * std :: acos(0.0));


会更好。


HTH。


Rob。

-
http://www.victim-prime.dsl.pipex.com/


red floyd写道:


不是std :: sin一个模板吗?




不,只是一个普通的函数,类型为

floa的参数重载t,double和long double。 (对于

类型复杂< T>的参数,还有一个sin模板,但这里没有涉及)


-


Pete Becker

Dinkumware,Ltd。( http://www.dinkumware.com


Hi,

simple question. Why does this not work:

#include <algorithm>
#include <cmath>
#include <iostream>

int
main()
{
double a[2];
a[0] = 0;a[1] = M_PI/2.;
std::transform(a, a+2, a, std::sin);
std::cout << a[0] << ", " << a[1] << std::endl;
return 0;
}

for gcc and icc the type of the third argument of transform is unknown. If I
use the global ::sin it works fine. Why? I include cmath and there sin is
defined in namespace std, so why is it unknown?

regards,
alex

解决方案

Alexander Stippler wrote:

Hi,

simple question. Why does this not work:

#include <algorithm>
#include <cmath>
#include <iostream>

int
main()
{
double a[2];
a[0] = 0;a[1] = M_PI/2.;
std::transform(a, a+2, a, std::sin);
std::cout << a[0] << ", " << a[1] << std::endl;
return 0;
}

for gcc and icc the type of the third argument of transform is unknown. If I
use the global ::sin it works fine. Why? I include cmath and there sin is
defined in namespace std, so why is it unknown?

regards,
alex



IANAG (I am not a guru), and am probably way off base (please correct me
guys -- please no flames).

Isn''t std::sin a template? Would you need to specifically instantiate
std::sin, i.e.

std::transform(a, a+2, a, std::sin<double>)

You might also want to use std::ptr_fun as a wrapper to std::sin (or ::sin).


Alexander Stippler wrote in news:40******@news.uni-ulm.de in
comp.lang.c++:

Hi,

simple question. Why does this not work:

#include <algorithm>
#include <cmath>
#include <iostream>

int
main()
{
double a[2];
a[0] = 0;a[1] = M_PI/2.;
std::transform(a, a+2, a, std::sin);
std::cout << a[0] << ", " << a[1] << std::endl;
return 0;
}

for gcc and icc the type of the third argument of transform is
unknown. If I use the global ::sin it works fine. Why? I include cmath
and there sin is defined in namespace std, so why is it unknown?

regards,
alex



There are several overloads of std::sin, you need to pick one:

std::transform(a, a+2, a, (double (*)(double)std::sin);

Note M_PI isn''t AFAICT Standard C++, you can use:

double const M_PI = (2.0 * std::acos( 0.0 ));

Though maybe:

double const double_pi = (2.0 * std::acos( 0.0 ));

Whould be better.

HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/


red floyd wrote:


Isn''t std::sin a template?



No, just an ordinary function, with overloads for arguments of type
float, double, and long double. (There''s also a sin template for
arguments of type complex<T>, but that''s not involved here)

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)


这篇关于为什么我需要:: sin,而不是std :: sin?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆