By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,979 Members | 960 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,979 IT Pros & Developers. It's quick & easy.

Stroustrup 3.7.1 - "vector"

P: n/a
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[i].name
<< " "
<< phone_book[i].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?

Nov 5 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a
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[i].name
<< " "
<< phone_book[i].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[i].name
<< " "
<< phone_book[i].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?

Nov 5 '06 #2

P: n/a
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
Nov 5 '06 #3

P: n/a
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[i].name
<< " "
<< phone_book[i].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 Tclass 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<Tis parent class of "class Vec".
what i dont understand is:

1.) why we are using /class<T>/ in "template" when we are using "<Tin
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

Nov 6 '06 #4

P: n/a

arnuld wrote in message
<11*********************@k70g2000cwa.googlegroups. com>...
>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 Tclass 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<Tis parent class of "class Vec".

what i dont understand is:

1.) why we are using /class<T>/ in "template" when we are using "<Tin
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/...ngInCPP2e.html

--
Bob R
POVrookie
Nov 6 '06 #5

P: n/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[i].name
<< " "
<< phone_book[i].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 Tclass 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<Tis parent class of "class Vec".
what i dont understand is:

1.) why we are using /class<T>/ in "template" when we are using "<Tin
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?
Nov 6 '06 #6

P: n/a
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/...ngInCPP2e.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.

Nov 6 '06 #7

P: n/a

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
Nov 6 '06 #8

P: n/a
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

Nov 7 '06 #9

P: n/a

arnuld wrote in message
<11**********************@m73g2000cwd.googlegroups .com>...
>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
Nov 7 '06 #10

P: n/a
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?

Nov 7 '06 #11

P: n/a

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
Nov 7 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.