Stroustrup 3.7.1 - "vector"

Discussion in 'C++' started by arnuld, Nov 5, 2006.

  1. arnuld

    arnuld Guest

    this is the code which runs without any trouble:

    -----------------------------------------------------
    #include <iostream>
    #include <string>
    #include <vector>

    struct Entry {
    std::string name;
    int e_num;
    };

    Entry phone_book[10];

    void print_entry(int i) {
    std::cout << phone_book.name
    << " "
    << phone_book.e_num
    << "\n";
    }


    int main() { }
    -----------------------------------------------------------

    now if i wrap the "whole-thing" above /main/ into a function or if i
    put the "whole-thing" above main into the /main/ i got this error:

    03_371.cpp: In function 'void c_style_code()':
    03_371.cpp:16: error: a function-definition is not allowed here before
    '{' token

    why exactly i got that?
    arnuld, Nov 5, 2006
    #1
    1. Advertising

  2. arnuld

    Rolf Magnus Guest

    arnuld wrote:

    > this is the code which runs without any trouble:
    >
    > -----------------------------------------------------
    > #include <iostream>
    > #include <string>
    > #include <vector>
    >
    > struct Entry {
    > std::string name;
    > int e_num;
    > };
    >
    > Entry phone_book[10];
    >
    > void print_entry(int i) {
    > std::cout << phone_book.name
    > << " "
    > << phone_book.e_num
    > << "\n";
    > }
    >
    >
    > int main() { }
    > -----------------------------------------------------------
    >
    > now if i wrap the "whole-thing" above /main/ into a function or if i
    > put the "whole-thing" above main into the /main/ i got this error:
    >
    > 03_371.cpp: In function 'void c_style_code()':
    > 03_371.cpp:16: error: a function-definition is not allowed here before
    > '{' token
    >
    > why exactly i got that?


    What do you mean by "wrap the whole-thing"? Something like this:

    int main()
    {
    #include <iostream>
    #include <string>
    #include <vector>

    struct Entry {
    std::string name;
    int e_num;
    };

    Entry phone_book[10];

    void print_entry(int i) {
    std::cout << phone_book.name
    << " "
    << phone_book.e_num
    << "\n";
    }

    }

    In this case, the problem is that you can't define functions within other
    functions, and you shouldn't use #include statemens within functions. Why
    exactly do you want to put it all inside a function?
    Rolf Magnus, Nov 5, 2006
    #2
    1. Advertising

  3. arnuld wrote:

    > now if i wrap the "whole-thing" above /main/ into a function or if i
    > put the "whole-thing" above main into the /main/ i got this error:


    Please post the "whole-code" that fails to compile.

    --
    Salu2
    =?ISO-8859-15?Q?Juli=E1n?= Albo, Nov 5, 2006
    #3
  4. arnuld

    arnuld Guest

    > What do you mean by "wrap the whole-thing"? Something like this:


    NO but nearly something same like you did:

    #include <iostream>
    #include <string>
    #include <vector>

    int main() {

    struct Entry {
    std::string name;
    int e_num;
    };

    Entry phone_book[10];

    void print_entry(int i) {
    std::cout << phone_book.name
    << " "
    << phone_book.e_num
    << "\n";
    }

    }

    > In this case, the problem is that you can't define functions within other
    > functions,


    OK.

    > and you shouldn't use #include statemens within functions.


    i never do.

    > Why exactly do you want to put it all inside a function?


    no special reason, i am a real-life coding newbie & also a C++ newbie,
    so i was experimenting with Stroustrup's code & was not able to find
    answers to some problems. i have 2 more problems, i will put one here
    regarding /range checking/ using a /template/:

    template<class T> class Vec : public vector<T> {
    public:
    Vec() : vector<T> () { }
    Vec(int s) : vector<T>(s) { }

    T& operator[] (int i) { return at(i); }
    const T& operator[] (int i) const { return at(i); }

    i understand these things in this code:

    1.) we are defining a new template here.
    2.) 1st 2 line in /public:/ tag are constructors.
    3.) last 2 lines in /public:/ tag are "member functions".
    4.) vector<T> is parent class of "class Vec".


    what i dont understand is:

    1.) why we are using /class<T>/ in "template" when we are using "<T> in
    vector<T> "
    2.) in last 2 lines we nearly do the same thing. the only difference is
    "const". why this? why not use only one line?

    -- arnuld
    arnuld.blogspot.com
    arnuld, Nov 6, 2006
    #4
  5. arnuld

    BobR Guest

    arnuld wrote in message
    <>...
    >> What do you mean by "wrap the whole-thing"? Something like this:

    >
    >NO but nearly something same like you did:
    >


    First, let's put things in a more accepted order:

    >#include <iostream>
    >#include <string>

    // >#include <vector> // not used
    >


    struct Entry {
    std::string name;
    int e_num;
    };

    Entry phone_book[ 10 ];

    void print_entry( int i ) {
    std::cout << phone_book[ i ].name
    << " " << phone_book[ i ].e_num << "\n";
    }


    >int main() {

    // now you need to fill the 'Entry's in the phone_book array
    phone_book[ 0 ].name = "Jack Sprat";
    phone_book[ 0 ].e_num = 12345;

    phone_book[ 1 ].name = "Jill Hill";
    phone_book[ 1 ].e_num = 54321;

    // now you can try to print one
    print_entry( 0 );

    // print_entry( 3 ); // BZZZzztt!. No! Empty.
    // print_entry( 10 ); // BZZZzztt!. Doesn't exist!

    >}


    // output: Jack Sprat 12345


    <snip>
    >no special reason, i am a real-life coding newbie & also a C++ newbie,
    >so i was experimenting with Stroustrup's code & was not able to find
    >answers to some problems. i have 2 more problems, i will put one here
    >regarding /range checking/ using a /template/:


    You don't even know a simple 'struct' or where to define a function, BUT, you
    want to understand a template (with inheritance of something that can have
    pitfalls)?!?
    ARE YOU NUTS?!?

    Don't use templates until you need to.
    (BTW, you are missing the bottom of the class.)

    >
    >template<class T> class Vec : public vector<T> {
    >public:
    > Vec() : vector<T> () { }
    > Vec(int s) : vector<T>(s) { }
    > T& operator[] (int i) { return at(i); }
    > const T& operator[] (int i) const { return at(i); }
    >
    >i understand these things in this code:
    >
    >1.) we are defining a new template here.
    >2.) 1st 2 line in /public:/ tag are constructors.
    >3.) last 2 lines in /public:/ tag are "member functions".
    >4.) vector<T> is parent class of "class Vec".
    >
    >what i dont understand is:
    >
    >1.) why we are using /class<T>/ in "template" when we are using "<T> in
    >vector<T> "
    >2.) in last 2 lines we nearly do the same thing. the only difference is
    >"const". why this? why not use only one line?
    >


    READ! "Thinking in C++" vol 2, "Templates in Depth"!!!
    Vol 1 has some on templates too?
    ....and the book(s) you have, of course.

    Get "Thinking in C++", 2nd ed. Volume 1(&2) by Bruce Eckel
    (available for free here. You can buy it in hardcopy too.):
    http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html

    --
    Bob R
    POVrookie
    BobR, Nov 6, 2006
    #5
  6. * arnuld:
    >> What do you mean by "wrap the whole-thing"? Something like this:

    >
    >
    > NO but nearly something same like you did:
    >
    > #include <iostream>
    > #include <string>
    > #include <vector>
    >
    > int main() {
    >
    > struct Entry {
    > std::string name;
    > int e_num;
    > };
    >
    > Entry phone_book[10];
    >
    > void print_entry(int i) {
    > std::cout << phone_book.name
    > << " "
    > << phone_book.e_num
    > << "\n";
    > }
    >
    > }
    >
    >> In this case, the problem is that you can't define functions within other
    >> functions,

    >
    > OK.
    >
    >> and you shouldn't use #include statemens within functions.

    >
    > i never do.
    >
    >> Why exactly do you want to put it all inside a function?

    >
    > no special reason, i am a real-life coding newbie & also a C++ newbie,
    > so i was experimenting with Stroustrup's code & was not able to find
    > answers to some problems. i have 2 more problems, i will put one here
    > regarding /range checking/ using a /template/:
    >
    > template<class T> class Vec : public vector<T> {
    > public:
    > Vec() : vector<T> () { }
    > Vec(int s) : vector<T>(s) { }
    >
    > T& operator[] (int i) { return at(i); }
    > const T& operator[] (int i) const { return at(i); }
    >
    > i understand these things in this code:
    >
    > 1.) we are defining a new template here.
    > 2.) 1st 2 line in /public:/ tag are constructors.
    > 3.) last 2 lines in /public:/ tag are "member functions".
    > 4.) vector<T> is parent class of "class Vec".
    >
    >
    > what i dont understand is:
    >
    > 1.) why we are using /class<T>/ in "template" when we are using "<T> in
    > vector<T> "


    Syntax. Instead of the word 'class' you can use the word 'typename',
    which means exactly the same inside a 'template' formal parameter list.
    In 'vector<T>' you're not defining what the formal template parameter
    is, but using it, same as you don't write '(int x)*5' when using a
    variable 'x' previously declared as 'int'.


    > 2.) in last 2 lines we nearly do the same thing. the only difference is
    > "const". why this? why not use only one line?


    The first operator[] can be called on a non-const Vec object, and then
    allows you to modify an element of that vector.

    The second operator[] can be called on any Vec object, including const
    ones, but therefore returns 'const T&', a reference to const, which
    prohibits you from modifying the element.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Nov 6, 2006
    #6
  7. arnuld

    arnuld Guest

    > You don't even know a simple 'struct' or where to define a function, BUT, you
    > want to understand a template (with inheritance of something that can have
    > pitfalls)?!?
    > ARE YOU NUTS?!?


    NO, i am not NUTS. i have just started to learn C++ from Stroustrup.

    > (BTW, you are missing the bottom of the class.)


    did not get that.


    > READ! "Thinking in C++" vol 2, "Templates in Depth"!!!
    > Vol 1 has some on templates too?
    > ...and the book(s) you have, of course.
    >
    > Get "Thinking in C++", 2nd ed. Volume 1(&2) by Bruce Eckel
    > (available for free here. You can buy it in hardcopy too.):
    > http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html


    i know. i have vol 1 on my desk & trust me Eckel asumes that you are a
    C programmer. i am not. i tried first 5 chapters & i came to know i
    need to do K&R2 before i go next & rather than doing that i picked up
    Stroustrup.
    arnuld, Nov 6, 2006
    #7
  8. arnuld

    BobR Guest

    arnuld wrote in message ...
    >> You don't even know a simple 'struct' or where to define a function, BUT,

    you
    >> want to understand a template (with inheritance of something that can have
    >> pitfalls)?!?
    >> ARE YOU NUTS?!?

    >
    >NO, i am not NUTS. i have just started to learn C++ from Stroustrup.


    Like I said before:
    Don't chase the rainbow looking for the pot of gold, just pick up the
    diamonds
    along the way.

    Meaning: take it one step at a time. Programming can't be learned in a day,
    'C++' can't be learned in a day (the book "Learn C++ in 24 hours" **has** to
    be a joke!!).

    Learn basics, then learn struct/class, then learn class design patterns
    (GOF), then learn templates, etc..

    --
    Bob R
    POVrookie
    BobR, Nov 6, 2006
    #8
  9. arnuld

    arnuld Guest

    BobR wrote:

    > Like I said before:
    > Don't chase the rainbow looking for the pot of gold, just pick up the
    > diamonds along the way.


    > Meaning: take it one step at a time. Programming can't be learned in a day,
    > 'C++' can't be learned in a day


    ok, 1 step at a time. i was just reading chapter 3 of Stroustrup where
    i hit the /template/. i was not reading beyond that chapter.

    > (the book "Learn C++ in 24 hours" **has** to be a joke!!).


    EXACTLY my opinion.

    > Learn basics, then learn struct/class, then learn class design patterns
    > (GOF), then learn templates, etc..


    so you say: basics -> struct -> class -.> GOF -> templates

    i think Stroustrup follows the same way.

    BTW, you want me to change the book OR you advised me to learn C first.
    NO, not as a prerequisite for C++ but because i lack on experience &
    C++ is huge & complex, C is small & simple & will be good for me as a
    beginner OR something else?

    thanks

    -- arnuld
    http:://arnuld.blogspot.com
    arnuld, Nov 7, 2006
    #9
  10. arnuld

    BobR Guest

    arnuld wrote in message
    <>...
    >BobR wrote:
    >> Like I said before:
    >> Don't chase the rainbow looking for the pot of gold, just pick up the
    >> diamonds along the way.
    >> Meaning: take it one step at a time. Programming can't be learned in a

    day,
    >> 'C++' can't be learned in a day

    >
    >ok, 1 step at a time. i was just reading chapter 3 of Stroustrup where
    >i hit the /template/. i was not reading beyond that chapter.


    Keep in mind that authors of programming books may need to use something that
    may not be *fully* explained until later in the book (Eckel does that).

    >
    >> (the book "Learn C++ in 24 hours" **has** to be a joke!!).

    >
    >EXACTLY my opinion.
    >
    >> Learn basics, then learn struct/class, then learn class design patterns
    >> (GOF), then learn templates, etc..

    >
    >so you say: basics -> struct -> class -.> GOF -> templates
    >
    >i think Stroustrup follows the same way.


    Great minds think alike! Heh heh heh. <G>

    >
    >BTW, you want me to change the book OR you advised me to learn C first.
    >NO, not as a prerequisite for C++ but because i lack on experience &
    >C++ is huge & complex, C is small & simple & will be good for me as a
    >beginner OR something else?


    Heck no!!
    I don't think you could find anyone more qualified to write about C++ than
    Mr. Stroustrup.
    I don't have his book(s) (I can't afford them), but I've read some of his
    stuff on the net. Good stuff!!

    I'm just saying don't stumble over a log, when you get a saw in the next
    chapter! <G>

    C is NOT so small & simple.
    Learning programming concepts is also not simple.

    Keep at it.

    --
    Bob R
    POVrookie
    BobR, Nov 7, 2006
    #10
  11. arnuld

    arnuld Guest

    > BobR wrote:

    > Keep in mind that authors of programming books may need to use something that
    > may not be *fully* explained until later in the book (Eckel does that).


    ok

    > Great minds think alike! Heh heh heh. <G>


    :)

    > >BTW, you want me to change the book OR you advised me to learn C first.
    > >NO, not as a prerequisite for C++ but because i lack on experience &
    > >C++ is huge & complex, C is small & simple & will be good for me as a
    > >beginner OR something else?

    >
    > Heck no!!
    > I don't think you could find anyone more qualified to write about C++ than
    > Mr. Stroustrup. I don't have his book(s) (I can't afford them), but I've read
    > some of his stuff on the net. Good stuff!!


    in India, it costs a little more than 6 USD but with cheap cover, cheap
    pages, cheap binding & cheap printing :-(

    > I'm just saying don't stumble over a log, when you get a saw in the next
    > chapter! <G>


    my English is not so good. so i assume you mean: "dont wander over some
    *piece* of text for long when you can see that *piece* is explained
    later."

    > C is NOT so small & simple.


    Hmmm... quite strange for me.

    > Learning programming concepts is also not simple.


    that is i expect

    > Keep at it.


    DONE. hey BoB, what does the "<G>" mean in your reply here?
    arnuld, Nov 7, 2006
    #11
  12. arnuld

    BobR Guest

    arnuld wrote in message ...
    >> BobR wrote:

    >
    >> >BTW, you want me to change the book OR you advised me to learn C first.
    >> >NO, not as a prerequisite for C++ but because i lack on experience &
    >> >C++ is huge & complex, C is small & simple & will be good for me as a
    >> >beginner OR something else?

    >>
    >> Heck no!!
    >> I don't think you could find anyone more qualified to write about C++ than
    >> Mr. Stroustrup. I don't have his book(s) (I can't afford them), but I've

    read
    >> some of his stuff on the net. Good stuff!!

    >
    >in India, it costs a little more than 6 USD but with cheap cover, cheap
    >pages, cheap binding & cheap printing :-(


    Wow, all I need to do is fly over there. Just think how much I'll save on the
    books!! :-}
    (average tech, book here is $50.us.)

    >
    >> I'm just saying don't stumble over a log, when you get a saw in the next
    >> chapter! <G>

    >
    >my English is not so good. so i assume you mean: "dont wander over some
    >*piece* of text for long when you can see that *piece* is explained
    >later."


    Yes. (..and your english is better than mine! I speak American! <G>).

    >
    >> Keep at it.

    >
    >DONE. hey BoB, what does the "<G>" mean in your reply here?


    The <G> == a big grin. <g> == a little grin. :) == a smile ( :-} == my
    smile).
    They are called Emoticons ( Emotion icons ). People on the net can't see your
    face, so, they came up with those. Google for "emoticons" if you want a whole
    list of them.

    --
    Bob R
    POVrookie
    BobR, Nov 7, 2006
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pmatos
    Replies:
    6
    Views:
    23,725
  2. Replies:
    8
    Views:
    1,890
    Csaba
    Feb 18, 2006
  3. arnuld
    Replies:
    13
    Views:
    500
    Default User
    Nov 8, 2006
  4. Javier
    Replies:
    2
    Views:
    542
    James Kanze
    Sep 4, 2007
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    346
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page