注册 登录
编程论坛 C++教室

error C2228: left of '.hour' must have class/struct/union type

cancerhd 发布于 2007-08-02 11:04, 2317 次点击

#include<iostream>

using std::cout;
using std::endl;

struct Time
{
int hour;
int minute;
int second;
}; //½á¹¹´ø·ÖºÅ

void printMilitary(const Time &);
void printStandard(const Time &);

int main()
{
Time dinnerTime;

dinnerTime.hour = 18;
dinnerTime.second = 30;
dinnerTime.minute = 0;

cout<<"dinner will be held at: ";
printMilitary(dinnerTime);
cout<<"military time,\nwhile is: ";
(dinnerTime);
cout<<"stand time: \n";

dinnerTime.hour = 29;
dinnerTime.minute = 73;

cout<<"\n time with incalid values:";
printMilitary(dinnerTime);
cout<<endl;
return 0;
}

void printMilitary (const Time &)
{
cout<<(t.hour < 10 ?"0" :" ")<<t.hour<<":"
<<(t.minute < 10 ?"0" :" ")<<t.minute;

}

void printStandard (const Time &)
{
cout<<((t.hour = 0 || t.hour =12 ) ?
12 : t.hour & 12)
<<":"<<(t.minute < 10 ? "0" : " ")<<t.minute
<<":"<<(t.second < 10 ? "0" : " ")<<t.second
<<(t.hour < 12 ? "AM" : "PM");
}

2 回复
#2
medicihophy2007-08-02 11:08
以下是引用cancerhd在2007-8-2 11:04:50的发言:

#include<iostream>

using std::cout;
using std::endl;

struct Time
{
int hour;
int minute;
int second;
}; //½á¹¹´ø·ÖºÅ

void printMilitary(const Time &);
void printStandard(const Time &);

int main()
{
Time dinnerTime;

dinnerTime.hour = 18;
dinnerTime.second = 30;
dinnerTime.minute = 0;

cout<<"dinner will be held at: ";
printMilitary(dinnerTime);
cout<<"military time,\nwhile is: ";
(dinnerTime);
cout<<"stand time: \n";

dinnerTime.hour = 29;
dinnerTime.minute = 73;

cout<<"\n time with incalid values:";
printMilitary(dinnerTime);
cout<<endl;
return 0;
}

void printMilitary (const Time & t)where your 't'?
{
cout<<(t.hour < 10 ?"0" :" ")<<t.hour<<":"
<<(t.minute < 10 ?"0" :" ")<<t.minute;

}

void printStandard (const Time & t)where your 't'?
{
cout<<((t.hour = 0 || t.hour =12 ) ? and look at '=',should it be '=='?
12 : t.hour & 12)
<<":"<<(t.minute < 10 ? "0" : " ")<<t.minute
<<":"<<(t.second < 10 ? "0" : " ")<<t.second
<<(t.hour < 12 ? "AM" : "PM");
}

[此贴子已经被作者于2007-8-2 11:12:08编辑过]

#3
cancerhd2007-08-02 12:19
回复:(medicihophy)以下是引用cancerhd在2007-8-2 ...
Yes,that's right.Thank you very much.
1