2010-11-18 6 views
5

私はC++、Linuxの中で働いていますし、私は次のような問題が発生します。C++の構造体のデータメンバー

struct testing{ 
uint8_t a; 
uint16_t b; 
char c; 
int8_t d; 

}; 

testing t; 

t.a = 1; 
t.b = 6; 
t.c = 'c'; 
t.d = 4; 
cout << "Value of t.a >>" << t.a << endl; 
cout << "Value of t.b >>" << t.b << endl; 
cout << "Value of t.c >>" << t.c << endl; 
cout << "Value of t.d >>" << t.d << endl; 

私のコンソールに出力されている:

Value of t.a >> 
Value of t.b >>6 
Value of t.c >>c 
Value of t.d >> 

それはTAとTDのように思えますint8_tおよびuint8_tタイプでは欠落しています。それはなぜそうですか?

ありがとうございました。

答えて

10

int8_tとuint8_t型は、おそらくのcharとunsigned char型として定義されています。ストリーム< <オペレータはそれらを文字として出力します。それぞれ1と4に設定されているため、制御文字であり、文字を印刷しないため、コンソールには何も表示されません。それらを65と66( 'A'と 'B')に設定し、何が起こるかを見てください。

EDIT:代わりに文字の数値をプリントアウトするために、あなたは適切な型にそれらをキャストする必要があります:Linuxのmanページ、int8_tとuint8_tが実際にtypedefedさ

cout << static_cast<unsigned int>(t.a) << endl; 
+0

bravo buddy:+ 1 – Chubsdad

+2

...「<< unsigned(ta)<<」と「<< unsigned(tb)<<」 –

+1

...として出力するか、<< static_cast (ta)<<および<< static_cast (tb)<< ...^_^... – paercebal

3

これは、これらの変数がoperator<<過負荷を選択したときに 'char'型として処理されているためです。

試してみてください。

cout << "Value of t.a >>" << static_cast<int>(t.a) << endl; 
2

thisでcharとして:あなたはhereを見つけることができるよう

typedef signed char int8_t 
typedef unsigned char uint8_t 

値1とチャーの4は、制御文字です。

これは、何も印刷されていないのが見える理由です。