473,624 Members | 2,258 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2126
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************ *********@k70g2 000cwa.googlegr oups.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<cla ss 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************ **********@m73g 2000cwd.googleg roups.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2494
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
3133
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 other right now. Here is my code: template <typename T> class Array { public: Array& operator()(const T& v) {
7
1883
by: Bruce Sam | last post by:
Is this a pointer array of function?If I was wrong,what does it means?
1
2354
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
5689
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 vector
13
2064
by: arnuld | last post by:
this is the code: ------------------------------------------------------------------------- #include <iostream> #include <string> #include <vector> struct Pair { std::string name;
4
7084
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 Expression:vector subscript out of range.
0
8233
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8170
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8619
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8334
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8474
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7158
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6108
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2604
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1784
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.