Connecting Tech Pros Worldwide Forums | Help | Site Map

C++ Primer ex 5.18

arnuld
Guest
 
Posts: n/a
#1: Jul 25 '07
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


tragomaskhalos
Guest
 
Posts: n/a
#2: Jul 25 '07

re: C++ Primer ex 5.18


On Jul 25, 9:34 am, arnuld <geek.arn...@gmail.comwrote:
Quote:
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 !)
Quote:
>
int main()
{
std::vector<std::string*psvec;
std::string input_string;
// WARNING: ps is uninitialised here ...
Quote:
std::string* ps;
>
while(std::cin >input_string)
{
// DISASTER IMMINENT: dereferencing uninitialised pointer
// You need to use dynamically created std::strings in here instead
Quote:
*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)-
Quote:
>size()
<< " size: " << (*iter).size() /* error is here */
<< std::endl;
}
>
return 0;
>
}
>
James Kanze
Guest
 
Posts: n/a
#3: Jul 25 '07

re: C++ Primer ex 5.18


On Jul 25, 10:34 am, arnuld <geek.arn...@gmail.comwrote:
Quote:
it does not run and even does not even give me any clue to the problem in
its output :(
Quote:
/* 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.
*/
Quote:
#include <iostream>
#include <vector>
#include <string>
Quote:
int main()
{
std::vector<std::string*psvec;
std::string input_string;
std::string* ps;
Quote:
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.
Quote:
psvec.push_back(ps++);
}
Quote:
/* 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?
Quote:
<< std::endl;
/* double-defrenced operator because it points to a pointer
rather than a value */
}
Quote:
/* 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.)
Quote:
return 0;
}
--
James Kanze (GABI Software) email:james.kanze@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

tom
Guest
 
Posts: n/a
#4: Jul 25 '07

re: C++ Primer ex 5.18


On Jul 25, 4:34 pm, arnuld <geek.arn...@gmail.comwrote:
Quote:
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;

}

Jerry Coffin
Guest
 
Posts: n/a
#5: Jul 25 '07

re: C++ Primer ex 5.18


In article <pan.2007.07.25.08.34.02.890128@gmail.com>,
geek.arnuld@gmail.com says...

[ ... ]
Quote:
#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.
Quote:
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...
Quote:
/* 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.
BobR
Guest
 
Posts: n/a
#6: Jul 25 '07

re: C++ Primer ex 5.18



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

----- Original Message -----
From: James Kanze <james.kanze@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:
Quote:
Quote:
Quote:
...[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


Closed Thread