Two Stupid, Beginner Questions

To begin, I have everything I need working, so no worries. Don't worry, most of my questions are related to, "Why does it do this?" I have one error related to the way I'm handling errors (which isn't with exceptions) but everything is totally under control! So to begin...

Hello again everyone! I was working through classes a bit more in-depth today and I had some questions I know are basic, but I feel there is some basic aspects I can't figure out on my own.

I was tasked by my book to create a library class with simple things like ISBN's, titles, etc. No biggie. I decided to create a header for my class, a source for implementation, and leave everything to main. Standard, but my first question is why putting my functions (I noticed it especially in my overload functions) in the class HEADER returns an error, but if I put it in the source (assume all the includes were there, because they were from the moment of creation) everything was just fine! Not questioning the language, but is it not possible to define in headers outside inline functions?

My second question - and you're going to really call me a noob - is why sometimes I can use 'x' but not "x"? Just for example:

1
2
3
4
5
6
7
//This is the CLASS declaration:
string ISBN;
//This is part of the declaration:
if (isbn.size() == 17)
     {
	if (isbn[3] != '-' && isbn[5] != '-' && isbn[8] != '-' &&
	isbn[15] != '-') cout << "Invalid format - try again:\n";


Don't try to run that fragment; it's just an example.

However, if I attempt to replace '-' with "-" I get a C2446 and C2040 compile error referencing char to int and my operand types. I ASSUMED (surely incorrectly) that as I was using a string as my temp (before making it official - for error checking purposes) it would be best to enclose it in double quotes for string literal. Can someone tell me what I'm missing here?

Thanks guys. Sorry I lack the ability to explain anything in brevity, and none of the questions are "serious," and I handled the errors rather quickly, but I'm a curious one.
is why sometimes I can use 'x' but not "x"
'x' is a char, just one byte.

"x" is a string that contains x. It's a null terminated string, so the sequence in memory is 'x' and 0. It's two bytes.

With code:
 
std::string name = "hello world";

name[0] is a char. So if you want to check if it's x, you write:
1
2
3
if (name[0] == 'x')
{
}

"hello world" is a null terminated string, and can be used to initialise std::string.
OK, so you're saying that by my using "x" it's like putting the \0 (I think it is? I saw it in Prata's Primer Plus book concerning arrays) but because I'm just looking for a '-' sign - ONE sign/character - I need '-'?

Or to put it another way:

If I wanted to search for "-X" as the end, I would reference the last and second to last subscripts in the string, and individually search for '-' and 'X'?

If that's right:

NOW I GET IT! (And thank you very much)
Topic archived. No new replies allowed.