K
Kuhl
Hi, I keyed in my first CPP program in Linux system. I expect the
result should look like below:
t1 is 00:00:00
t2 is 12:10:18
But the compile fails. See below. The source code is at the bottom of
this post.
BTW, the reason why I wrote #include </usr/include/c++/3.2.3/backward/
iostream.h>
is that #include <iostream.h> cannot work.
But unfortunately, there are still two more include statements in
iostream.h
#include "backward_warning.h"
#include <iostream>
These files are in different paths, and there are still included files
even further. I don't know how to solve the library file path issue.
What's more, the compile messages show that there are still more
issues in this source code. Perhaps these issues are very easy for you
to trace out. But for the beginner of CPP like me, I don't know how to
trace it. Would you please give help?
Thank you in advance.
# gcc -Wall cpp_008.c -o cpp_008
cpp_008.c:4: syntax error before '{' token
cpp_008.c:9: syntax error before ':' token
cpp_008.c:13: syntax error before '}' token
cpp_008.c:15: syntax error before ':' token
cpp_008.c:18: warning: type defaults to `int' in declaration of
`minute'
cpp_008.c:18: warning: data definition has no type or storage class
cpp_008.c:19: warning: type defaults to `int' in declaration of
`second'
cpp_008.c:19: warning: data definition has no type or storage class
cpp_008.c:20: syntax error before '}' token
cpp_008.c: In function `main':
cpp_008.c:38: `Time' undeclared (first use in this function)
cpp_008.c:38: (Each undeclared identifier is reported only once
cpp_008.c:38: for each function it appears in.)
cpp_008.c:38: syntax error before "t1"
cpp_008.c:39: `cout' undeclared (first use in this function)
cpp_008.c:40: `t1' undeclared (first use in this function)
cpp_008.c:42: syntax error before "t2"
cpp_008.c:43: `t2' undeclared (first use in this function)
# cat cpp_008.c
#include </usr/include/c++/3.2.3/backward/iostream.h>
class Time
{
public:
Time();
void SetTime( int, int, int);
void DisplayTimeMilitary();
private:
int hour;
int minute;
int second;
};
Time::Time()
{
hour = 0;
minute = 0;
second = 0;
}
Time::SetTime( int h, int m, int s )
{
this->hour = ( h >= 0 && h < 24)? h:0;
this->minute = ( m >= 0 && m < 60)? m:0;
this->second = ( s >= 0 && s < 60)? s:0;
}
Time:isplayTimeMilitary()
{
cout<< ( this->hour < 10 ? "0":"")<< this->hour<<":"
<< ( this->minute < 10 ? "0":"")<< this->minute
<< ( this->second < 10 ? "0":"")<<this->second<< endl;
}
int main()
{
Time t1;
cout<<"t1 is:";
t1.DisplayTimeMilitary();
Time t2;
t2.SetTime(12, 10, 18);
cout<<"t2 is:";
t2.isplayTimeMilitary();
return 0;
}
result should look like below:
t1 is 00:00:00
t2 is 12:10:18
But the compile fails. See below. The source code is at the bottom of
this post.
BTW, the reason why I wrote #include </usr/include/c++/3.2.3/backward/
iostream.h>
is that #include <iostream.h> cannot work.
But unfortunately, there are still two more include statements in
iostream.h
#include "backward_warning.h"
#include <iostream>
These files are in different paths, and there are still included files
even further. I don't know how to solve the library file path issue.
What's more, the compile messages show that there are still more
issues in this source code. Perhaps these issues are very easy for you
to trace out. But for the beginner of CPP like me, I don't know how to
trace it. Would you please give help?
Thank you in advance.
# gcc -Wall cpp_008.c -o cpp_008
cpp_008.c:4: syntax error before '{' token
cpp_008.c:9: syntax error before ':' token
cpp_008.c:13: syntax error before '}' token
cpp_008.c:15: syntax error before ':' token
cpp_008.c:18: warning: type defaults to `int' in declaration of
`minute'
cpp_008.c:18: warning: data definition has no type or storage class
cpp_008.c:19: warning: type defaults to `int' in declaration of
`second'
cpp_008.c:19: warning: data definition has no type or storage class
cpp_008.c:20: syntax error before '}' token
cpp_008.c: In function `main':
cpp_008.c:38: `Time' undeclared (first use in this function)
cpp_008.c:38: (Each undeclared identifier is reported only once
cpp_008.c:38: for each function it appears in.)
cpp_008.c:38: syntax error before "t1"
cpp_008.c:39: `cout' undeclared (first use in this function)
cpp_008.c:40: `t1' undeclared (first use in this function)
cpp_008.c:42: syntax error before "t2"
cpp_008.c:43: `t2' undeclared (first use in this function)
# cat cpp_008.c
#include </usr/include/c++/3.2.3/backward/iostream.h>
class Time
{
public:
Time();
void SetTime( int, int, int);
void DisplayTimeMilitary();
private:
int hour;
int minute;
int second;
};
Time::Time()
{
hour = 0;
minute = 0;
second = 0;
}
Time::SetTime( int h, int m, int s )
{
this->hour = ( h >= 0 && h < 24)? h:0;
this->minute = ( m >= 0 && m < 60)? m:0;
this->second = ( s >= 0 && s < 60)? s:0;
}
Time:isplayTimeMilitary()
{
cout<< ( this->hour < 10 ? "0":"")<< this->hour<<":"
<< ( this->minute < 10 ? "0":"")<< this->minute
<< ( this->second < 10 ? "0":"")<<this->second<< endl;
}
int main()
{
Time t1;
cout<<"t1 is:";
t1.DisplayTimeMilitary();
Time t2;
t2.SetTime(12, 10, 18);
cout<<"t2 is:";
t2.isplayTimeMilitary();
return 0;
}