C++ style questions (OOP)

N

neo88

Ok, here's the deal. I have a nice class definition and a whole bunch
of inline functions to go along with it in the same file. My question
is, do I need an implementation file for the class, even though I have
an inline for every single function? Or can I just use the class and
it's inlines as a header file for programs that actually use the class
and defined functions?
While I'm here I might as well ask this one too. Here is a function
that I have in the same class that I was talking about above:

inline int Startup(void) {
short quit;
std::cout << "Welcome to the Agent system Neo\n"; IO stuff
std::cout << "Do you wish to continue?(y/n)\n";
while (quit != 'y') && (quit != 'Y')
(quit != 'n') && (quit != 'N') {
if (std::cin << 'y') {
std::cout << "Please wait\n";
} else (std::cin << 'n') {
Close(); // calls Close() function
}

This won't compile. The gcc says that I can't use the
short quit;
because it hasn't been declared. I don't have to declare it private
right? I tried that and it didn't work, I got the same error. How is
the loop structure, am I doing something wrong here? I also get a
parse error before 'quit' in the while loop just before the bitwise
'and' operator.
Thanks in advance for any help, it is greatly appreciated.
neo88
 
B

Buster

neo88 said:
inline int Startup(void) {
short quit;
std::cout << "Welcome to the Agent system Neo\n"; IO stuff
std::cout << "Do you wish to continue?(y/n)\n";
while (quit != 'y') && (quit != 'Y')
(quit != 'n') && (quit != 'N') {
if (std::cin << 'y') {
std::cout << "Please wait\n";
} else (std::cin << 'n') {
Close(); // calls Close() function
}

This won't compile. The gcc says that I can't use the
short quit;
because it hasn't been declared.

Come on now. Didn't it really say "initialized"?
Is it right? What value do you think "quit" has
when you first use it?
 
C

Claudio Puviani

neo88 said:
Ok, here's the deal. I have a nice class definition
and a whole bunch of inline functions to go along
with it in the same file. My question is, do I need
an implementation file for the class, even though I
have an inline for every single function? Or can I
just use the class and it's inlines as a header file
for programs that actually use the class and
defined functions?

You could get away with just the header file, but it's good practice to have
a corresponding .cpp file, even if all it does is include the header file as
a sanity check. As your project grows, you might find that some functions,
old or new, might be better placed out-of-line or you might want to add
version information to the files in static strings or compile-time checks or
static support functions, etc.. Any changes you make in the future will then
affect how your libraries and/or applications are built. Also, unless you
comment it clearly, someone who only sees the hearder file might waste time
looking for the .cpp file. There's value in a consistent build structure.

Claudio Puviani
 
B

Buster

neo88 said:
inline int Startup(void) {
short quit;
std::cout << "Welcome to the Agent system Neo\n"; IO stuff
std::cout << "Do you wish to continue?(y/n)\n";
while (quit != 'y') && (quit != 'Y')
(quit != 'n') && (quit != 'N') {
if (std::cin << 'y') {
std::cout << "Please wait\n";
} else (std::cin << 'n') {
Close(); // calls Close() function
}
[...] I also get a
parse error before 'quit' in the while loop just before the bitwise
'and' operator.

The entire condition for the while loop has to be parenthesized:
while ((quit != 'y') && (quit != 'Y') &&
(quit != 'n') && (quit != 'N')) // ...

(You still need to initialize "quit" first though. Hint: there's
no way for you to do that and have the whole function still make
sense. You should remove all of the lines with "quit" in them.)

Regarding "if (std::cin << 'y')": you can't do it like that, I'm
afraid. The following is legal.

// ...
char c;
if (std::cin >> c)
{
if (c == 'y')
{
std::cout << "Please wait\n";
}
else if (c == 'n')
{
Close (); // The comment "calls Close() function"
// is worse than useless.
}
else
{
// Deal with other possible inputs.
}
}
else
{
// Deal with an input problem.
}
// ...
 
H

Howard

neo88 said:
While I'm here I might as well ask this one too. Here is a function
that I have in the same class that I was talking about above:

inline int Startup(void) {

If you include the implementation code here, there is no need for the
"inline" directive.
short quit;
std::cout << "Welcome to the Agent system Neo\n"; IO stuff

Missing comment marks here, before "IO stuff"? (Lousy comment, by the way.)
std::cout << "Do you wish to continue?(y/n)\n";
while (quit != 'y') && (quit != 'Y')
(quit != 'n') && (quit != 'N') {

Typo? There should be another "&&" between those two lines. Is there?

And why are you comparing a short (quit) against a char ('y', etc.)? Maybe
that's what that error means..there is no variable called quit that matches
the comparison against a char? (Just a guess.)

Also, the *entire* boolean expression has to be in parentheses, not just the
individual sub-expressions.
if (std::cin << 'y') {
std::cout << "Please wait\n";
} else (std::cin << 'n') {
Close(); // calls Close() function

Another strange comment. Pretty obvious that Close() calls Close(), isn't
it?

But why are you calling Close() (whatever that is) inside your loop? Do you
want to "close" every time through the loop? And is there any matching Open
function?

Where's the rest of it? You're two closing braces short here.

This won't compile. The gcc says that I can't use the
short quit;
because it hasn't been declared.

Where does quit ever get a value assigned to it?

I don't have to declare it private
right? I tried that and it didn't work, I got the same error. How is
the loop structure, am I doing something wrong here? I also get a
parse error before 'quit' in the while loop just before the bitwise
'and' operator.

That's a boolean operator, not a bitwise operator. The bitwise operator is
&, not &&.
 
H

Howard

Oh, and I missed those lines. You can't stream data into constant
expressions. I take it you wanted to enter the data into your quit
variable, since that's what you're using to compare againt in your while
loop. You need to stream into a variable.

Howard
 
N

neo88

Howard said:
Oh, and I missed those lines. You can't stream data into constant
expressions. I take it you wanted to enter the data into your quit
variable, since that's what you're using to compare againt in your while
loop. You need to stream into a variable.

Howard

Thanks everyone for all the help. I'm afriad this is a lot of
information for me to digest in one big gulp. To address the "quit"
problem, is this what I need to do:
short quit = 0; // or some such
I hope so. BTW, I like my comments thank you very much.
What did Buster mean by take all the lines with "quit" away? What's
wrong with them?
Oh yeah and the two closing braces are there at the end of the
function, copy and paste mistake... sorry.
One more thing: Should I init "quit" to a variable?
short quit = a;
then stream the input to it like so:
if (std::cin << a) {
std::cout << "Please wait\n";
} else (std::cin << a) {
is the if-else loop a good way to accomplish this?
I'll take all of these suggestions into account and see what I can do
with them. If anyone has anything else to add, please don't hesitate.

My Kung Fu is stronger.
neo88
 
N

neo88

Buster said:
neo88 said:
inline int Startup(void) {
short quit;
std::cout << "Welcome to the Agent system Neo\n"; IO stuff
std::cout << "Do you wish to continue?(y/n)\n";
while (quit != 'y') && (quit != 'Y')
(quit != 'n') && (quit != 'N') {
if (std::cin << 'y') {
std::cout << "Please wait\n";
} else (std::cin << 'n') {
Close(); // calls Close() function
}
[...] I also get a
parse error before 'quit' in the while loop just before the bitwise
'and' operator.

The entire condition for the while loop has to be parenthesized:
while ((quit != 'y') && (quit != 'Y') &&
(quit != 'n') && (quit != 'N')) // ...

(You still need to initialize "quit" first though. Hint: there's
no way for you to do that and have the whole function still make
sense. You should remove all of the lines with "quit" in them.)

Regarding "if (std::cin << 'y')": you can't do it like that, I'm
afraid. The following is legal.

// ...
char c;
if (std::cin >> c)
{
if (c == 'y')
{
std::cout << "Please wait\n";
}
else if (c == 'n')
{
Close (); // The comment "calls Close() function"
// is worse than useless.
}
else
{
// Deal with other possible inputs.
}
}
else
{
// Deal with an input problem.
}
// ...

Buster: I put the IO code you suggested above into my program, and
works fine, thanks. One tiny problem. The gcc says that I can't call
the function Close() because I have used it before it was defined.
It's defined right after Startup(). Here is the Close() function:

inline Close(void) {
std::cout << "Goodbye.\n";
}

So do I need to put this function before the Startup() function? I
thought it didn't really matter....

My Kung Fu is stronger.
neo88
 
B

Buster

neo88 said:
Buster: I put the IO code you suggested above into my program, and
works fine, thanks. One tiny problem. The gcc says that I can't call
the function Close() because I have used it before it was defined.
It's defined right after Startup(). Here is the Close() function:

inline Close(void) {
std::cout << "Goodbye.\n";
}

That's a syntax error. You must specify the return type.
So do I need to put this function before the Startup() function? I
thought it didn't really matter....

You don't have to define a function before you call it, but you do have
to declare it.
 
T

Thomas Matthews

neo88 wrote:
[snip]
inline int Startup(void) {
short quit;
std::cout << "Welcome to the Agent system Neo\n"; IO stuff
std::cout << "Do you wish to continue?(y/n)\n";
while (quit != 'y') && (quit != 'Y')
(quit != 'n') && (quit != 'N') {
if (std::cin << 'y') {
std::cout << "Please wait\n";
} else (std::cin << 'n') {
Close(); // calls Close() function
}

This won't compile. The gcc says that I can't use the
short quit; [snip]

Thanks in advance for any help, it is greatly appreciated.
neo88

Please lookup the functions: std::toupper, std::tolower.
These functions allow you to check for responses without
worrying about case:
while ((std::toupper(quit) != 'Y')
|| (std::tolower(quit) != 'n'))
{
//....

If you must have to check for upper and lower case, then
I recommend placing the responses into a string and searching
it:
const string quit_replies("yYnN");
while (replies.find(quit) == string::npos)
{
}



--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
N

neo88

Thomas Matthews said:
neo88 wrote:
[snip]
inline int Startup(void) {
short quit;
std::cout << "Welcome to the Agent system Neo\n"; IO stuff
std::cout << "Do you wish to continue?(y/n)\n";
while (quit != 'y') && (quit != 'Y')
(quit != 'n') && (quit != 'N') {
if (std::cin << 'y') {
std::cout << "Please wait\n";
} else (std::cin << 'n') {
Close(); // calls Close() function
}

This won't compile. The gcc says that I can't use the
short quit; [snip]

Thanks in advance for any help, it is greatly appreciated.
neo88

Please lookup the functions: std::toupper, std::tolower.
These functions allow you to check for responses without
worrying about case:
while ((std::toupper(quit) != 'Y')
|| (std::tolower(quit) != 'n'))
{
//....

If you must have to check for upper and lower case, then
I recommend placing the responses into a string and searching
it:
const string quit_replies("yYnN");
while (replies.find(quit) == string::npos)
{
}



--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Thanks a lot. That really helps me out! I'll look those up tonight...
didn't know that they exsited. BTW I really like the string idea you
had as well....

My Kung Fu is stronger.
neo88
 
N

neo88

Buster said:
neo88 said:
inline int Startup(void) {
short quit;
std::cout << "Welcome to the Agent system Neo\n"; IO stuff
std::cout << "Do you wish to continue?(y/n)\n";
while (quit != 'y') && (quit != 'Y')
(quit != 'n') && (quit != 'N') {
if (std::cin << 'y') {
std::cout << "Please wait\n";
} else (std::cin << 'n') {
Close(); // calls Close() function
}
[...] I also get a
parse error before 'quit' in the while loop just before the bitwise
'and' operator.

The entire condition for the while loop has to be parenthesized:
while ((quit != 'y') && (quit != 'Y') &&
(quit != 'n') && (quit != 'N')) // ...

(You still need to initialize "quit" first though. Hint: there's
no way for you to do that and have the whole function still make
sense. You should remove all of the lines with "quit" in them.)

Regarding "if (std::cin << 'y')": you can't do it like that, I'm
afraid. The following is legal.

// ...
char c;
if (std::cin >> c)
{
if (c == 'y')
{
std::cout << "Please wait\n";
}
else if (c == 'n')
{
Close (); // The comment "calls Close() function"
// is worse than useless.
}
else
{
// Deal with other possible inputs.
}
}
else
{
// Deal with an input problem.
}
// ...

Buster: I put the IO code you suggested above into my program, and
works fine, thanks. One tiny problem. The gcc says that I can't call
the function Close() because I have used it before it was defined.
It's defined right after Startup(). Here is the Close() function:

inline Close(void) {
std::cout << "Goodbye.\n";
}

So do I need to put this function before the Startup() function? I
thought it didn't really matter....

My Kung Fu is stronger.
neo88

Ok, here is the function right now:

inline int Startup(void) {
char quit; // cahnged this from short to char
std::cout << "Welcome to the Agent system Neo\n"; // IO stuff
std::cout << "Do you wish to continue(y/n)?\n";
while ((quit != 'y') && (quit != 'Y') && // added the Boolean
connector here
(quit != 'n') && (quit != 'N')) {
char c;
if (std::cin >> c)
{
if (c == 'y')
{
std::cout << "Please wait\\n";
}
else if (c == 'n')
{
Close(); // The comment "calls Close() function"
// is worse than useless.
}
else
{

// Deal with other possible inputs.
}
}
else
{
// Deal with an input problem.
}
// ...
}
//if (std::cin << 'y') {
//std::cout << "Please wait...\n";
//} else (std::cin << 'n') {
// Close(); calls Close() function
//}

return 0; // clean up

}

And here are the compiler errors:

Neo2.cpp:122: `quit' undeclared (first use this function)
Neo2.cpp:122: (Each undeclared identifier is reported only once for
each function it appears in.)
Neo2.cpp:133: `Close' undeclared (first use this function)
Neo2.cpp: In function `int Close()':
Neo2.cpp:158: `int Close()' used prior to declaration

How do I declare Close()? It worked fine in the code I had before....
BTW that is commented in there just so you can see it. How do I
declare quit? Doesn't the line
char quit;
do that? If not than I am very confused on what a declaration is....
But on the bright side, after I fix this, the function shouldn't
return any more errors :)

May the Source be with you.
neo88
 
B

Buster

neo88 said:
Ok, here is the function right now:

inline int Startup(void) {
char quit; // cahnged this from short to char
std::cout << "Welcome to the Agent system Neo\n"; // IO stuff
std::cout << "Do you wish to continue(y/n)?\n";

The loop you have below is totally wrong. You should delete it
and start again.

(i) The variable 'quit' has not been initialized. Its value is
indeterminate. (What did you think the value of 'quit' was going
to be?)

(ii) The code I supplied doesn't fit inside the while loop. It was
meant to stand alone. If you need to keep getting a single character
from standard input just change "if (std::cin >> c)" to
"while (std::cin >> c)". However, it would be less confusing to the
user if you read a line at a time, like this:

std::string s;
while (std::getline (s))
{
if (s == "y") // do something;
else if (s == "n") // do something else;
}
// If we reach here, the stream is no longer good.

while ((quit != 'y') && (quit != 'Y') && // added the Boolean
connector here
(quit != 'n') && (quit != 'N')) {
char c;
if (std::cin >> c)
{
if (c == 'y')
{
std::cout << "Please wait\\n";
}
else if (c == 'n')
{
Close(); // The comment "calls Close() function"
// is worse than useless.
}
else
{

// Deal with other possible inputs.
}
}
else
{
// Deal with an input problem.
}
// ...
}
//if (std::cin << 'y') {
//std::cout << "Please wait...\n";
//} else (std::cin << 'n') {
// Close(); calls Close() function
//}

return 0; // clean up

}

And here are the compiler errors:

Neo2.cpp:122: `quit' undeclared (first use this function)
Neo2.cpp:122: (Each undeclared identifier is reported only once for
each function it appears in.)

This error is wrong. Either there's a compiler bug, or these messages
did not come from compiling that function.
Neo2.cpp:133: `Close' undeclared (first use this function)
Neo2.cpp: In function `int Close()':
Neo2.cpp:158: `int Close()' used prior to declaration

These errors are correct. Read them, then fix it.
How do I declare Close()? It worked fine in the code I had before....
BTW that is commented in there just so you can see it. How do I
declare quit? Doesn't the line
char quit;
do that?

Yes it does.
If not than I am very confused on what a declaration is....
But on the bright side, after I fix this, the function shouldn't
return any more errors :)

I'm sorry if my tone seems a little harsh. If you ask me to I'll just
write the function for you, but I get the impression you want to figure
it out yourself. Good luck.
 
N

neo88

Buster said:
The loop you have below is totally wrong. You should delete it
and start again.

(i) The variable 'quit' has not been initialized. Its value is
indeterminate. (What did you think the value of 'quit' was going
to be?)

(ii) The code I supplied doesn't fit inside the while loop. It was
meant to stand alone. If you need to keep getting a single character
from standard input just change "if (std::cin >> c)" to
"while (std::cin >> c)". However, it would be less confusing to the
user if you read a line at a time, like this:

std::string s;
while (std::getline (s))
{
if (s == "y") // do something;
else if (s == "n") // do something else;
}
// If we reach here, the stream is no longer good.



This error is wrong. Either there's a compiler bug, or these messages
did not come from compiling that function.


These errors are correct. Read them, then fix it.


Yes it does.


I'm sorry if my tone seems a little harsh. If you ask me to I'll just
write the function for you, but I get the impression you want to figure
it out yourself. Good luck.

Hey, if you want to write the function, please do. I have time
requirement on this thing that is running out fast. I would like to
figure this out, cause I think I'm really close here.... However, this
program needs to get done. So give it a go Buster. In the meantime,
I've got a few things I'd like to try....

May the Source be with you.
neo88
 
B

Buster

#include <ostream>
#include <iostream>
#include <string>
#include <locale>
#include <cstdlib>

int Close (); // This is a forward declaration.
// Its type must match your definition.

int Startup ()
{
std::cout << "Welcome!\n";
std::cout << "Do you wish to continue? (y/n)\n";

std::string line;
bool quit;
while (std::getline (line, std::cin))
{
if (s.size () == 1)
{
if (std::tolower (s [0], std::locale ()) == 'y')
{
quit = false;
break;
}
else if (std::tolower (s [0], std::locale ()) == 'n')
{
quit = true;
break;
}
}
std::cout << "Please answer 'y' or 'n'.\n";
}

if (std::cin)
{
// Can you verify that 'quit' is always initialized by now?
if (quit) Close ();
else std::cout << "Please wait.\n";
}
else
{
std::cerr << "Stream error.\n";
Close ();
}

return std::rand ();
}
 
N

neo88

Buster said:
#include <ostream>
#include <iostream>
#include <string>
#include <locale>
#include <cstdlib>

int Close (); // This is a forward declaration.
// Its type must match your definition.

int Startup ()
{
std::cout << "Welcome!\n";
std::cout << "Do you wish to continue? (y/n)\n";

std::string line;
bool quit;
while (std::getline (line, std::cin))
{
if (s.size () == 1)
{
if (std::tolower (s [0], std::locale ()) == 'y')
{
quit = false;
break;
}
else if (std::tolower (s [0], std::locale ()) == 'n')
{
quit = true;
break;
}
}
std::cout << "Please answer 'y' or 'n'.\n";
}

if (std::cin)
{
// Can you verify that 'quit' is always initialized by now?
if (quit) Close ();
else std::cout << "Please wait.\n";
}
else
{
std::cerr << "Stream error.\n";
Close ();
}

return std::rand ();
}

Thanks Buster :)

May the Source be with you.
neo88
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top