Stroustrup 3.7.1 - "vector"

A

arnuld

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?
 
R

Rolf Magnus

arnuld said:
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?
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

arnuld said:
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.
 
A

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> "
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
 
B

BobR

arnuld wrote in message
NO but nearly something same like you did:

First, let's put things in a more accepted order:
#include <iostream>
#include <string>

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


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
 
A

Alf P. Steinbach

* 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

arnuld

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.
 
B

BobR

arnuld wrote in message ...
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..
 
A

arnuld

BobR said:
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
 
B

BobR

arnuld wrote in message
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).
EXACTLY my opinion.


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

i think Stroustrup follows the same way.

Great minds think alike! Heh heh heh. said:
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.
 
A

arnuld

BobR said:
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>
:)


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?
 
B

BobR

arnuld wrote in message ...
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.)
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! said:
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.
 

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,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top