类型转换 [英] type conversion

查看:56
本文介绍了类型转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参见下面的示例:为什么a的输出工作和

''b''的输出无法编译?有没有办法写类'某事'',以便

''b''默认正确转换? (包括iostream,字符串,使用

命名空间std)

模板< typename T>

struct something {

T x;

运营商T();

};


int

main( int,char **)

{

something< int> a;

something< string> b;


ax = 4711;

bx =" hello world!" ;;


cout< ;< a<< ENDL; //编译好!

cout<< b<< ENDL; //运算符<< ......没有匹配,编译失败!

}

编译器= gcc 3.3.5


感谢您的帮助。


弗兰克

See example below: Why does the output of ''a'' work and the output of
''b'' fails to compile? Is there a way to write class ''something'' so that
''b'' converts correctly by default? (include iostream, string, use
namespace std)
template <typename T>
struct something {
T x;
operator T();
};

int
main(int, char**)
{
something<int> a;
something<string> b;

a.x = 4711;
b.x = "hello world!";

cout << a << endl; // compiles fine!
cout << b << endl; // operator<< ... no match, compile fails!
}
compiler = gcc 3.3.5

Thanks for any Help.

Frank

推荐答案

frs写道:
参见示例下面:为什么a的输出工作和
b的输出无法编译?有没有办法写类'某事'',以便
''b''默认正确转换? (包括iostream,string,use
namespace std)

template< typename T>
struct something {
T x;
operator T() ;
};

int
main(int,char **)
{
某事< int> a;
某事< string> b;

a.x = 4711;
b.x =" hello world!" ;;

cout<< a<< ENDL; //编译好!
cout<< b<< ENDL; //运算符<< ......没有匹配,编译失败!

编译器= gcc 3.3.5

感谢您的帮助。

Frank
See example below: Why does the output of ''a'' work and the output of
''b'' fails to compile? Is there a way to write class ''something'' so that
''b'' converts correctly by default? (include iostream, string, use
namespace std)
template <typename T>
struct something {
T x;
operator T();
};

int
main(int, char**)
{
something<int> a;
something<string> b;

a.x = 4711;
b.x = "hello world!";

cout << a << endl; // compiles fine!
cout << b << endl; // operator<< ... no match, compile fails!
}
compiler = gcc 3.3.5

Thanks for any Help.

Frank




这可能与字符串本身是一个

模板有关。

例如以下编译


模板< typename T>

struct something {

T x ;

运算符T();

};


struct S

{

};


ostream& operator<<(ostream&,const S&);


int main(int,char **)

{

某事< int> a;

某事< S> b;


a.x = 4711;

b.x = S();


cout<< a<< ENDL; //编译好!

cout<< b<< ENDL; //汇编很好!

}


我不知道你问题的完整答案,但我的建议真的很好

会远离这些东西。我认为即使是BS也说要远离语言中那些不起眼的角落。


John



It''s probably got something to do with the fact that string itself is a
template.

For instance the following does compile

template <typename T>
struct something {
T x;
operator T();
};

struct S
{
};

ostream& operator<<(ostream&, const S&);

int main(int, char**)
{
something<int> a;
something<S> b;

a.x = 4711;
b.x = S();

cout << a << endl; // compiles fine!
cout << b << endl; // compiles fine!
}

I don''t know the complete answer to your question but really my advice
would be to stay away from this stuff. I think even BS said stay away
from the obscure corners of the language.

John




" John Harrison" <乔************* @ hotmail.com>在消息中写道

新闻:TJ ************** @ newsfe7-win.ntli.net ...

"John Harrison" <jo*************@hotmail.com> wrote in message
news:TJ**************@newsfe7-win.ntli.net...
frs写道:
参见下面的例子:为什么a的输出工作和
b的输出无法编译?有没有办法写类'某事'',以便
''b''默认正确转换? (包括iostream,string,use
namespace std)

template< typename T>
struct something {
T x;
operator T() ;
};

int
main(int,char **)
{
某事< int> a;
某事< string> b;

a.x = 4711;
b.x =" hello world!" ;;

cout<< a<< ENDL; //编译好!
cout<< b<< ENDL; //运算符<< ......没有匹配,编译失败!

编译器= gcc 3.3.5

感谢您的帮助。

Frank
See example below: Why does the output of ''a'' work and the output of
''b'' fails to compile? Is there a way to write class ''something'' so that
''b'' converts correctly by default? (include iostream, string, use
namespace std)
template <typename T>
struct something {
T x;
operator T();
};

int
main(int, char**)
{
something<int> a;
something<string> b;

a.x = 4711;
b.x = "hello world!";

cout << a << endl; // compiles fine!
cout << b << endl; // operator<< ... no match, compile fails!
}
compiler = gcc 3.3.5

Thanks for any Help.

Frank



这可能与字符串本身是一个
模板有关。

例如以下编译

模板< typename T>
struct something {
T x;
运营商T();
};

struct S
{
};

ostream& operator<(&ostream&,const S&);

int main(int,char **)
{
某事< int> a;
某事< S> b;

a.x = 4711;
b.x = S();

cout<< a<< ENDL; //编译好!
cout<< b<< ENDL; //编译好!
}

我不知道你问题的完整答案,但我的建议真的是远离这些东西。我认为即使是BS也要远离语言的模糊角落。

John



It''s probably got something to do with the fact that string itself is a
template.

For instance the following does compile

template <typename T>
struct something {
T x;
operator T();
};

struct S
{
};

ostream& operator<<(ostream&, const S&);

int main(int, char**)
{
something<int> a;
something<S> b;

a.x = 4711;
b.x = S();

cout << a << endl; // compiles fine!
cout << b << endl; // compiles fine!
}

I don''t know the complete answer to your question but really my advice
would be to stay away from this stuff. I think even BS said stay away from
the obscure corners of the language.

John




如果是这样的话那么这条线


something< string> b;


将无法编译。


一种可能性是使用的字符串不是std :: string而且不是' '$

提供带流的输出操作。


Ben



Had that been the case then the line

something<string> b;

would have failed to compile.

One possibility is that the string used wasn''t std::string and doesn''t
provide output operation with streams.

Ben


嗯,它确实没有'用std :: string编译......我无法弄清楚原因是什么'

...


无论如何以下OP会做OP可能想做的事情:


模板< typename T>

struct something

{

T x;

};


模板< typename OStreamT,typename T>

OStreamT&运算符<< (

OStreamT& os,const something< T>& o)

{

return os<< o.x;

}


int main(无效)

{

something< int> a;

something< std :: string> b;


ax = 4711;

bx =" hello world!" ;;


std: :cout<< a<< std :: endl;

std :: cout<< b<< std :: endl;

}


ben
hmm, it really didn''t compile with std::string...I can''t figure out what''s
the cause...

Anyway the following will do what the OP might want to do:

template <typename T>
struct something
{
T x;
};

template <typename OStreamT, typename T>
OStreamT& operator << (
OStreamT& os, const something<T>& o)
{
return os << o.x;
}

int main(void)
{
something<int> a;
something<std::string> b;

a.x = 4711;
b.x = "hello world!";

std::cout << a << std::endl;
std::cout << b << std::endl;
}

ben


这篇关于类型转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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