473,327 Members | 1,979 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Stroustrup 3.7.1 - "vector"

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
11 2094
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
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
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

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
* 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
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

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

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: bucher | last post by:
Hi, I want to push a const auto_ptr into a vector, but the compile reports errors. Below is the code. class Folder; class Result; class Results { public: int size(){return _Items.size();}
6
by: Mattias Brändström | last post by:
Hello all! I am trying to write code that allows me to initialise one of my classes inline (with a vector like structure). Inline might not be the best term to use here but I can't think of any...
7
by: Bruce Sam | last post by:
Is this a pointer array of function?If I was wrong,what does it means?
1
by: G Fernandes | last post by:
Hi, can someone tell me what the following words mean as per C/clc: 1) token 2) token sequence 3) scalar variable 4) vector
4
by: arnuld | last post by:
i wrote a programme to create a vector of 5 elements (0 to 4), here is the code & output: #include <iostream> #include <vector> int main() { std::vector<intivec; // dynamically create a...
13
by: arnuld | last post by:
this is the code: ------------------------------------------------------------------------- #include <iostream> #include <string> #include <vector> struct Pair { std::string name;
4
by: Han | last post by:
when I exe my project in vs.net2005,I got the error following: Debug Assertion Failed! Program:........ File:c:\program files\microsoft visual studio 8\vc\include\vector Line:756 ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.