473,387 Members | 1,464 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,387 software developers and data experts.

C++ Primer ex 5.18

it does not run and even does not even give me any clue to the problem in
its output :(

/* C++ Primer - 4/e
* chapter 5 - Expressions

* exercise 5.18
* STATEMENT
* write a programme that defines a vector of pointers to strings.
read the vector, printing each string and its corresponding size.

*/

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

int main()
{
std::vector<std::string*psvec;
std::string input_string;
std::string* ps;

while(std::cin >input_string)
{
*ps = input_string;
psvec.push_back(ps++);
}

/* printing the strings pointed by the pointers in the vector*/
for(std::vector<std::string*>::const_iterator iter = psvec.begin();
iter != psvec.end(); ++iter)
{
std::cout << "string: " << **iter
<< " size: " << (*iter).size() /* error is here */
<< std::endl;
/* double-defrenced operator because it points to a pointer
rather than a value */
}

/* i was thinking of using "std::copy" from <algorithmand
"ostream_iterator" from <sstreamfrom but was not able to
understand the peculiar mechanism of both of them */

return 0;
}

/* OUTPUT
/home/arnuld/programming/cpp $ g++ -ansi -pedantic -Wall -Wextra

ex_05-18.cpp ex_05-18.cpp: In function ‘int main()’: ex_05-18.cpp:32:
error: request for member ‘size’ in ‘iter.

__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with
_Iterator = std::string* const*, _Container = std::vector<std::string*,
std::allocator<std::string*]()’, which is of non-class type
‘std::string* const’

/home/arnuld/programming/cpp $

*/

--
-- http://arnuld.blogspot.com

Jul 25 '07 #1
5 1983
On Jul 25, 9:34 am, arnuld <geek.arn...@gmail.comwrote:
it does not run and even does not even give me any clue to the problem in
its output :(
See code markups (without benefit of compiler, but I hope correct !)
>
int main()
{
std::vector<std::string*psvec;
std::string input_string;
// WARNING: ps is uninitialised here ...
std::string* ps;

while(std::cin >input_string)
{
// DISASTER IMMINENT: dereferencing uninitialised pointer
// You need to use dynamically created std::strings in here instead
*ps = input_string;
psvec.push_back(ps++);
}

/* printing the strings pointed by the pointers in the vector*/
for(std::vector<std::string*>::const_iterator iter = psvec.begin();
iter != psvec.end(); ++iter)
{
std::cout << "string: " << **iter
// PROBLEM: Type of *iter is const std::string *, so you need (*iter)-
>size()
<< " size: " << (*iter).size() /* error is here */
<< std::endl;
}

return 0;

}
Jul 25 '07 #2
On Jul 25, 10:34 am, arnuld <geek.arn...@gmail.comwrote:
it does not run and even does not even give me any clue to the problem in
its output :(
/* C++ Primer - 4/e
* chapter 5 - Expressions

* exercise 5.18
* STATEMENT
* write a programme that defines a vector of pointers to strings.
read the vector, printing each string and its corresponding size.
*/
#include <iostream>
#include <vector>
#include <string>
int main()
{
std::vector<std::string*psvec;
std::string input_string;
std::string* ps;
while(std::cin >input_string)
{
*ps = input_string;
What does ps point to here? Somehow, you're going to have to
get memory to which the pointer can point.
psvec.push_back(ps++);
}
/* printing the strings pointed by the pointers in the vector*/
for(std::vector<std::string*>::const_iterator iter = psvec.begin();
iter != psvec.end(); ++iter)
{
std::cout << "string: " << **iter
<< " size: " << (*iter).size() /* error is here */
What is the type of *iter?

C++ has a moderately strict type system. Whatever *iter
returns, it should have the value type of the array. What type
does the array contain? Is the . operator legal on such types?
What operators might be legal?
<< std::endl;
/* double-defrenced operator because it points to a pointer
rather than a value */
}
/* i was thinking of using "std::copy" from <algorithmand
"ostream_iterator" from <sstreamfrom but was not able to
understand the peculiar mechanism of both of them */
You can't use it if you want to output the size as well. (Well,
actually you can, but doing so would require some funny
business, and is probably not a good idea.)
return 0;
}
--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 25 '07 #3
tom
On Jul 25, 4:34 pm, arnuld <geek.arn...@gmail.comwrote:
it does not run and even does not even give me any clue to the problem in
its output :(

/* C++ Primer - 4/e
* chapter 5 - Expressions

* exercise 5.18
* STATEMENT
* write a programme that defines a vector of pointers to strings.
read the vector, printing each string and its corresponding size.

*/

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

int main()
{
std::vector<std::string*psvec;
std::string input_string;
std::string* ps;

while(std::cin >input_string)
{
*ps = input_string;
psvec.push_back(ps++);
}

/* printing the strings pointed by the pointers in the vector*/
for(std::vector<std::string*>::const_iterator iter = psvec.begin();
iter != psvec.end(); ++iter)
{
std::cout << "string: " << **iter
<< " size: " << (*iter).size() /* error is here */
<< std::endl;
/* double-defrenced operator because it points to a pointer
rather than a value */
}

/* i was thinking of using "std::copy" from <algorithmand
"ostream_iterator" from <sstreamfrom but was not able to
understand the peculiar mechanism of both of them */

return 0;

}

/* OUTPUT
/home/arnuld/programming/cpp $ g++ -ansi -pedantic -Wall -Wextra

ex_05-18.cpp ex_05-18.cpp: In function 'int main()': ex_05-18.cpp:32:
error: request for member 'size' in 'iter.

__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with
_Iterator = std::string* const*, _Container = std::vector<std::string*,
std::allocator<std::string*]()', which is of non-class type
'std::string* const'

/home/arnuld/programming/cpp $

*/

--
--http://arnuld.blogspot.com
fixed your program and post it below:

int main()
{
std::vector<std::string*psvec;
//std::string input_string;
//std::string* ps;
std::string *pstring(NULL);
pstring = new string;
// modification: fix run-time error here to allocate space to store
input string
while(std::cin >*pstring)
{
psvec.push_back(pstring);
pstring = new string;
}
delete pstring;
/* printing the strings pointed by the pointers in the vector*/
for(std::vector<std::string*>::const_iterator iter = psvec.begin();
iter != psvec.end(); ++iter)
{
std::cout << "string: " << **iter
<< " size: " << (*iter)->size() /* error is here */ //
modification: made here to get it compilied
<< std::endl;
/* double-defrenced operator because it points to a pointer
rather than a value */
}

//modification: clean up dynamically allocated memory
while(psvec.size()>0)
{
std::vector<std::string *>::iterator iter = psvec.begin();
delete *iter;
psvec.erase(iter);
}

/* i was thinking of using "std::copy" from <algorithmand
"ostream_iterator" from <sstreamfrom but was not able to
understand the peculiar mechanism of both of them */
return 0;

}

Jul 25 '07 #4
In article <pa****************************@gmail.com>,
ge*********@gmail.com says...

[ ... ]
#include <iostream>
#include <vector>
#include <string>

int main()
{
std::vector<std::string*psvec;
std::string input_string;
std::string* ps;
As has already been pointed out, this has type 'std::string *', but it's
not really a pointer to a string -- it's just an unitialized pointer.
while(std::cin >input_string)
{
*ps = input_string;
This really needs to allocate a new string and copy the input_string
into the new string, something like:

ps = new string(input_string);

As it stands right now, it's _trying_ to take wherever the uninitialized
pointer happens to point at, and treat it as if there was a string
there. If you're lucky, it'll point to somewhere that's not readable or
writable, so your program will die immediately when you try to do that
-- but chances of being that lucky aren't very high. The alternative is
that it sort of seems to work for a while, overwriting memory that
doesn't belong to it. That inevitably leads to eventual problems, but
it's hard to predict when they'll become visible -- Murphy's law being
what it is, you'll usually find out about it by the program dying a
horrible death just as you're showing it to the person who's in the best
position to fire you...
/* printing the strings pointed by the pointers in the vector*/
for(std::vector<std::string*>::const_iterator iter = psvec.begin();
iter != psvec.end(); ++iter)
{
std::cout << "string: " << **iter
<< " size: " << (*iter).size() /* error is here */
<< std::endl;
/* double-defrenced operator because it points to a pointer
rather than a value */
}

/* i was thinking of using "std::copy" from <algorithmand
"ostream_iterator" from <sstreamfrom but was not able to
understand the peculiar mechanism of both of them */
They're going to be pretty tough to apply in a case like this -- for the
most part, the standard containers and algorithms expect to work with
value-like objects, not with pointers. By value-like objects, I mean
things that it's free to copy, assign, etc. There's an implicit
assumption that such operations are also relatively cheap.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 25 '07 #5

arnuld <ge*********@gmail.comwrote in message...
>
/* i was thinking of using "std::copy" from <algorithmand
"ostream_iterator" from <sstreamfrom but was not able to
understand the peculiar mechanism of both of them */

----- Original Message -----
From: James Kanze <ja*********@gmail.com>
Newsgroups: comp.lang.c++
Sent: Tuesday, July 24, 2007 2:38 AM
Subject: Re: any improvements for this programme

On Jul 23, 6:15 pm, arnuld <geek.arn...@gmail.comwrote:
...[SNIPPED]........
yes it works. dinot know that "ostream_iterator" is defined in
"<sstream>" header.
The <sstreamheader is for the ostringstream.

ostream_iterator is defined in <iterator>.

(But any standard header is allowed to include any other,
and <iteratoris likely included in a header you've already included.)

END ----- Original Message -----
Did you forget?

--
Bob R
POVrookie
Jul 25 '07 #6

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

Similar topics

7
by: Sandman | last post by:
Could anyone give me a tip about a good primer on object oriented php programming - why I should use it, the benefits, the drawbacks, the bugs, the glory? And, should I upgrade to php5 before...
1
by: Charles L | last post by:
Does anyone know where I can find errata for Stan Lippman's 'C++ Primer 2nd Edition'? Charles Leng
1
by: hugo | last post by:
what is L&L ,people or book?
5
by: hajime | last post by:
I purchased this book: C++ Primer, 4th Edition ISBN: 0201721481 in Australia. The right side of the last three lines on page 231 are not legible. A stain shows a piece of paper was on that part...
7
by: Lycan. Mao.. | last post by:
Hello, I am a newbie in C++ and I'm in trouble in choosing books, I hope some one who can give me some tips. I'm already know C and a little about Scheme, C#, Python, Lua and so on, and now I want...
2
by: W. Watson | last post by:
Is there a primer out there on these two items? I have the Python tutorial, but would like either a Tkinter tutorial/primer to supplement it, or a primer/tutorial that addresses both. Maybe there's...
20
by: arnuld | last post by:
I get an error, can't find what is the problem: /* C++ Primer - 4/e * * Chapter 8, exercise 8.3 * STATEMENT * write a function that takes and returns an istream&. the function should read...
2
by: xianwei | last post by:
First, typedef struct pair { Node *parent; Node *child; } Pair; static Pair SeekItem(cosnt Item *pI, const Tree *pTree) { Pair look;
1
by: Kveldulv | last post by:
Hi all, here is the code: http://pastebin.com/m6e74d36b I'm stuck at highfink constructor, last line before #endif. As parameters, I have reference to one parent class and int member of another...
0
by: cincerite | last post by:
Hello , guys , I'm reading C++ Primer 3rd edition recently.I tried to download the errata of it from Stan Lippman's Home Page:http:// staff.develop.com/slip/ ,but it says:We're Sorry, we could not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...

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.