tags:

views:

184

answers:

6

I was studying in the famous schaums outline c++ programming book, and i saw something called list initialization - i didn't know that before.

I made a code according to the book, but it raised a lot of compiler errors. I was asking my self where the problem is, so i copied the code from the book, and guess what - it didn't work!

Here's the code:

class Rational {
        public:
                Rational(int n=0, int d=1) : num(n), den(d) { }
                int numerator0 const { return num; }
                int denominator0 const { return den; }

        private:
                int num, den;

};

main()
{
        Rational x(22, 7);
        cout << x.numerator() << "/" << x.denominator() << endl;

}

Can you tell me what is wrong here ? Thank you in advance. P.S. Written by "pros" yeah right...

A: 

I don't know this book, but at least:

main()

should become:

int main()


Also,

 // now the same as `Rational` methods.
 cout << x.numerator0() << "/" << x.denominator0() << endl;
AraK
+9  A: 
            int numerator0 const { return num; }
            int denominator0 const { return den; }

Notice the '0' (zero) cahracter instead of parenthesis ( ).

            int numerator() const { return num; }
            int denominator() const { return den; }
arul
Looks like OCR fail :)
Johannes Schaub - litb
yep, one more reason for buying real books :>
arul
Somebody fire the editor, or give the author a real word processor.
Martin York
+3  A: 

Looks like a typo here

int numerator0 const { return num; }

should be

int numerator() const { return num; }

Don't know if the typo is in the book or your pasted code.

oh, and it's called "initialization list" not "list initialization"

Glen
It's the book - adobe is not displaying everything well i guess...
gujo
+6  A: 
  1. You are missing include iostreams and using directives
  2. numerator and denominator signatures are not valid

This works ok:

#include <iostream>
using std::cout;
using std::endl;
class Rational {
    public:
            Rational(int n=0, int d=1) : num(n), den(d) { }
            int numerator() const { return num; }
            int denominator() const { return den; }

    private:
            int num, den;

};

main()
{
    Rational x(22, 7);
    cout << x.numerator() << "/" << x.denominator() << endl;

}
Arkaitz Jimenez
don't you mean `int main()`?
rlbond
+2  A: 

Here's a better version.

#include <iostream>
using std::cout;
using std::endl;
// I'm assuming your code has the #include's and using's and you just 
// omitted them.

class Rational {
    public:
        Rational(int n=0, int d=1) : num(n), den(d) { }
        // "num(n), den(d)" is an initialization list; I think that's what
        // Schaum's is talking about here.

        int numerator() const { return num; }
        int denominator() const { return den; }
        // numerator and denominator are methods, so they need parenthesis
        // like any other function call.  0 on the end must have been a typo.

    private:
        int num, den;
        // This is okay, but many style guides recommend naming your member
        // variables differently to set them apart from other variables.
        // For example:
        //   int mNum, mDen;
        //   int num_, den_;
};

// main needs a return type.  For the sake of completness, I usually include
// the argc and argv parameters as well.  C++ lets you omit a parameter's
// name if that parameter isn't used; this silences "unused parameter"
// warnings in your compiler.
int main(int, char**)
{
    Rational x(22, 7);
    cout << x.numerator() << "/" << x.denominator() << endl;
    return 0;   // You really ought to have a return value.
}
Josh Kelley
main() doesn't need an explicit return statement. If you leave it out, 0 will be returned.
Lucas
I did not realize that; must be a habit from my C days. Thanks.
Josh Kelley
Some compilers will complain if you don't return an int. For portability it's nice to include the return.
patros
+3  A: 

When you mess up () with 0, this means you typed the code without even the most basic knowledge of what it means. (And that's true even if the code was in the book like this.) That means you understood too little to already have progressed that far.

Does the book really use cout and endl (rather than std::cout and std::endl) without even a note about it? And does it really fail to provide the necessary include directive for those two identifiers? And does it really define the main() function without a return type (int main())?

If the answer to these questions is "yes", throw it away. Yes, I am very serious. Even if it really is famous (which I doubt, since I, being interested in C++, have never heard about it). If it gets this wrong, then it's famous crap at best.

In this answer I just listed a few very good C++ books.

sbi
The very first example in the book explains these things, and says they're assumed to be a part of every example. From what I can see, all examples do declare main as int main(). Looks like a case of someone trying to run before they can walk.
patros
@patros: I see. So the error is not in the book, but in front of the book. `:)`
sbi