Library file path and etc questions in my first CPP program

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::DisplayTimeMilitary()
{
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;
}
 
V

Victor Bazarov

Kuhl said:
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

Do not name your C++ files ".c" and compile them with 'gcc'. You're
trying to compile your code as a C program, and it fails (justifiedly).
Name it .C or .cpp and compile it with 'g++'.
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>

This is a non-standard header. Any consequence of including it cannot
be explained from the language point of view, unless the header is
presented here in full.

I assume that you actually do (as you should):

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::DisplayTimeMilitary()
{
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;
}

V
 
R

red floyd

Kuhl said:
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::DisplayTimeMilitary()
{
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;
}

1. As Victor said, you're compiling as C, not as C++.
2. The reason you couldn't find <iostream.h> is that it doesn't exist
as a Standard header. Use <iostream>, and prefix cout and endl with
std::

e.g.: std::cout << somthing << std::endl;
 
J

James Kanze

Which should tell you something to start off with.

Yes. A lot of implementors use this hack. Despite the fact
that it doesn't work correctly.

Don't use an absolute path in an include. Ever.
Do not name your C++ files ".c" and compile them with 'gcc'. You're
trying to compile your code as a C program, and it fails (justifiedly).
Name it .C or .cpp and compile it with 'g++'.

Or .cc, or .cxx. Under Unix, I think .cc is the most common.
Under Windows, .cpp, certainly. .C and .cxx are more or less
historical artifacts, I think.
This is a non-standard header. Any consequence of including
it cannot be explained from the language point of view, unless
the header is presented here in full.

Well, anyone with more than ten years experience with C++ should
be familiar with <iostream.h>. And the presence of a directory
named backward in the path should be a pretty good hint to
anyone without that experience. No point in presenting the
header in full---just say that if you're not maintaining legacy
code, use <iostream> instead, and everything should work.
Provided your program conforms to <iostream>'s semantics, rather
I assume that you actually do (as you should):
#include <iostream>
using namespace std;

I doubt it, given the error messages he's shown. But replacing
the include <iostream.h> with the above, and above all,
compiling the program as a C++ program, and not as C, should
reduce the number of errors significantly. (With gcc or g++,
you can force the compiler to treat any file as a c++ source.
The option is -x c++, I think. With gcc, you can also manually
request the standard C++ library in the link phase. Both are,
IMHO, good examples of going out of your way to create problems.
Just name the file .cpp or .cc, and compile it with g++.)
 
K

Kuhl

Hi, thank you all. I tried the answers. I still have following
questions.

1. I am not sure whether my command of the compile is correct:
g++ -Wall cpp_008.cpp -o cpp_008

2. I tried the approach of changing first two lines of my code as
following, but compile still has errors. What's the error in my code?
#include <iostream>
using namespace std;

# g++ -Wall cpp_008.cpp -o cpp_008
cpp_008.cpp:24: ISO C++ forbids declaration of `SetTime' with no type
cpp_008.cpp:24: prototype for `int Time::SetTime(int, int, int)' does
not match
any in class `Time'
cpp_008.cpp:8: candidate is: void Time::SetTime(int, int, int)
cpp_008.cpp:24: `int Time::SetTime(int, int, int)' and `void
Time::SetTime(int,
int, int)' cannot be overloaded
cpp_008.cpp:31: ISO C++ forbids declaration of `DisplayTimeMilitary'
with no
type
cpp_008.cpp:31: prototype for `int Time::DisplayTimeMilitary()' does
not match
any in class `Time'
cpp_008.cpp:9: candidate is: void Time::DisplayTimeMilitary()
cpp_008.cpp:31: `int Time::DisplayTimeMilitary()' and `void
Time::DisplayTimeMilitary()' cannot be overloaded
#

3. I tried another approach: #include <iostream>, but I removed "using
namespace std;" this time. Instead, I added "std::" in front of all
cout and endl. The errors are same as before.

# g++ -Wall cpp_008.cpp -o cpp_008
cpp_008.cpp:23: ISO C++ forbids declaration of `SetTime' with no type
cpp_008.cpp:23: prototype for `int Time::SetTime(int, int, int)' does
not match
any in class `Time'
cpp_008.cpp:7: candidate is: void Time::SetTime(int, int, int)
cpp_008.cpp:23: `int Time::SetTime(int, int, int)' and `void
Time::SetTime(int,
int, int)' cannot be overloaded
cpp_008.cpp:30: ISO C++ forbids declaration of `DisplayTimeMilitary'
with no
type
cpp_008.cpp:30: prototype for `int Time::DisplayTimeMilitary()' does
not match
any in class `Time'
cpp_008.cpp:8: candidate is: void Time::DisplayTimeMilitary()
cpp_008.cpp:30: `int Time::DisplayTimeMilitary()' and `void
Time::DisplayTimeMilitary()' cannot be overloaded
#

4. I implemented all the edits:
#include <iostream>
using namespace std;
And also added "std::" in front of all cout and endl. The errors are
still same as before.

What's the correct solution? Thanks.
 
V

Victor Bazarov

Kuhl said:
Hi, thank you all. I tried the answers. I still have following
questions.

1. I am not sure whether my command of the compile is correct:
g++ -Wall cpp_008.cpp -o cpp_008

Seems OK. I would probably force the compiler to the "strict" mode, but
it's not mandatory - some folks like using language extensions.
2. I tried the approach of changing first two lines of my code as
following, but compile still has errors. What's the error in my code?

What's the code?
#include <iostream>
using namespace std;

There is no error in those two lines.
# g++ -Wall cpp_008.cpp -o cpp_008
cpp_008.cpp:24: ISO C++ forbids declaration of `SetTime' with no type
cpp_008.cpp:24: prototype for `int Time::SetTime(int, int, int)' does
not match
any in class `Time'
cpp_008.cpp:8: candidate is: void Time::SetTime(int, int, int)
cpp_008.cpp:24: `int Time::SetTime(int, int, int)' and `void
Time::SetTime(int,
int, int)' cannot be overloaded
cpp_008.cpp:31: ISO C++ forbids declaration of `DisplayTimeMilitary'
with no
type
cpp_008.cpp:31: prototype for `int Time::DisplayTimeMilitary()' does
not match
any in class `Time'
cpp_008.cpp:9: candidate is: void Time::DisplayTimeMilitary()
cpp_008.cpp:31: `int Time::DisplayTimeMilitary()' and `void
Time::DisplayTimeMilitary()' cannot be overloaded
#

Apparently somewhere in your 'Time' class there is some illegal code.
Most of us here don't care much about guessing what your code looks like
from the posted compiler errors. RTFFAQ, section 5.
3. I tried another approach: #include <iostream>, but I removed "using
namespace std;" this time. Instead, I added "std::" in front of all
cout and endl. The errors are same as before.

Because they have nothing to do with said:
# g++ -Wall cpp_008.cpp -o cpp_008
cpp_008.cpp:23: ISO C++ forbids declaration of `SetTime' with no type
cpp_008.cpp:23: prototype for `int Time::SetTime(int, int, int)' does
not match
any in class `Time'
cpp_008.cpp:7: candidate is: void Time::SetTime(int, int, int)
cpp_008.cpp:23: `int Time::SetTime(int, int, int)' and `void
Time::SetTime(int,
int, int)' cannot be overloaded
cpp_008.cpp:30: ISO C++ forbids declaration of `DisplayTimeMilitary'
with no
type
cpp_008.cpp:30: prototype for `int Time::DisplayTimeMilitary()' does
not match
any in class `Time'
cpp_008.cpp:8: candidate is: void Time::DisplayTimeMilitary()
cpp_008.cpp:30: `int Time::DisplayTimeMilitary()' and `void
Time::DisplayTimeMilitary()' cannot be overloaded
#

Do you really think that "the same" can have a different meaning to
somebody, and that's why you need to post the compiler output with the
only difference in the line numbers? Hmm...
4. I implemented all the edits:
#include <iostream>
using namespace std;
And also added "std::" in front of all cout and endl. The errors are
still same as before.

What's the correct solution? Thanks.

The correct solution is to post your code (along with the compiler
diagnostic messages). RTFFAQ, section 5.

V
 
L

Lionel B

Hi, thank you all. I tried the answers. I still have following
questions.

1. I am not sure whether my command of the compile is correct:
g++ -Wall cpp_008.cpp -o cpp_008

Looks ok to me.
2. I tried the approach of changing first two lines of my code as
following, but compile still has errors. What's the error in my code?

See below
#include <iostream>
using namespace std;

That's fine.
# g++ -Wall cpp_008.cpp -o cpp_008
cpp_008.cpp:24: ISO C++ forbids declaration of `SetTime' with no type
cpp_008.cpp:24: prototype for `int Time::SetTime(int, int, int)' does
not match
any in class `Time'
cpp_008.cpp:8: candidate is: void Time::SetTime(int, int, int)
cpp_008.cpp:24: `int Time::SetTime(int, int, int)' and `void
Time::SetTime(int,
int, int)' cannot be overloaded
cpp_008.cpp:31: ISO C++ forbids declaration of `DisplayTimeMilitary'
with no
type
cpp_008.cpp:31: prototype for `int Time::DisplayTimeMilitary()' does not
match
any in class `Time'
cpp_008.cpp:9: candidate is: void Time::DisplayTimeMilitary()
cpp_008.cpp:31: `int Time::DisplayTimeMilitary()' and `void
Time::DisplayTimeMilitary()' cannot be overloaded
#

3. I tried another approach: #include <iostream>, but I removed "using
namespace std;" this time. Instead, I added "std::" in front of all cout
and endl. The errors are same as before.

Because this was not the cause of the errors (there is no suggestion in
the error messages that it was).

[...]
4. I implemented all the edits:
#include <iostream>
using namespace std;
And also added "std::" in front of all cout and endl. The errors are
still same as before.

What's the correct solution? Thanks.

Not sure exactly what code you compiled to get the above error messages.
These are corrections to your original code. All are trivial - you just
want to be a bit more careful and learn to understand error messages...
error messages may appear obscure (just wait till you get on to
templates ;-)) but they do tell you what's wrong. As you get to know your
compiler better this gets easier.

#include <iostream>

using namespace std;
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 )

Missing return type. In general definition must always match declaration -
the error messages are telling you this:

void 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::DisplayTimeMilitary()

And again:

void Time::DisplayTimeMilitary()
{
cout<< ( this->hour < 10 ? "0":"")<< this->hour<<":"
<< ( this->minute < 10 ? "0":"")<< this->minute << (
this->second < 10 ? "0":"")<<this->second<< endl;
}

You don't need 'this->' anywhere here (why did you think you did?)
int main()
{
Time t1;
cout<<"t1 is:";
t1.DisplayTimeMilitary();

Time t2;
t2.SetTime(12, 10, 18);
cout<<"t2 is:";
t2.isplayTimeMilitary();

Typo:

t2.DisplayTimeMilitary();
return 0;
}

Compiles and runs ok for me.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top