C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Problems with Accelerated C++ example
Goto page 1, 2  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
theo22
Guest





PostPosted: Thu Aug 21, 2003 10:59 am    Post subject: Problems with Accelerated C++ example Reply with quote



I've scoured the web looking for an answer to this but cannot find an
exact match and am to new to apply some of the detailed answers I
think might fix this.

I am trying to learn C++. Not much OO programming experience.
Bought
the book Accelerated C++. I'm stuck on pg.12 as I cannot get the
example code to work. Here is the code:
#include <iostream>
#include <string>

int main()
{
std::cout << "Please enter your first name: ";
std::string name;
std::cin >> name;

//Build the message that we intend to write
const std::string greeting = "Hello, " + name + "!";

//Build the second and fourth lines of the output
const std::string spaces(greeting.size(), ' ');
const std::string second = "* " + spaces + " *";

//Build the first and fifth lines of output
const std::string first(second.size(), "*");

//Write all
std::cout << std::endl;
std::cout << first << std::endl;
std::cout << second << std::endl;
std::cout << "* " << greeting << " *" << std::endl;
std::cout << second << std::endl;
std::cout << first << std::endl;

return 0;
}

Here is my error:
bash-2.05$ g++ pg12.cpp
1_1.cpp: In function `int main()':
1_1.cpp:19: invalid conversion from `const char*' to `char'
1_1.cpp:19: initializing argument 2 of `std::basic_string<_CharT,
_Traits, _Alloc>::basic_string(_Alloc::size_type, _CharT, const _Alloc&)
[with _CharT = char, _Traits = std::char_traits<char>, _Alloc =
std::allocator<char>]'

The error message does not make sense to me. Hopefully someday they
will. I'm using a Sparc Solaris 9 box. I loaded the Gnu gcc package
from SunFreeWare a little less than a year ago.

I plugged in this code in Microsoft VC++ 6 and Visual Studio.net 2003
and it worked fine. RATS! I really wanted to learn on my Sun box
but the code did not work there.

What's going on here? Did I miss something obvious? If I did I
apologize. This is so basic C++ I can't imagine why it is not
working. I read some posting about using c_str() because of some
conversion problem by pointing to something that is no longer there
but plugging that into my code without really knowing how or where to
use it did not work for me.

Please Help!!

Thank You.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Francis Glassborow
Guest





PostPosted: Fri Aug 22, 2003 1:14 am    Post subject: Re: Problems with Accelerated C++ example Reply with quote



In article <6f1e0d54.0308201652.12af8978 (AT) posting (DOT) google.com>, theo22
<theo_22 (AT) yahoo (DOT) com> writes
Quote:
const std::string first(second.size(), "*");

You miss-copied Andy's code. The original is:

const std::string first(second.size(), '*');

And if you really presented the same code to VC++ it also should have
rejected it.


--
Francis Glassborow ACCU
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Andrew Koenig
Guest





PostPosted: Fri Aug 22, 2003 9:30 am    Post subject: Re: Problems with Accelerated C++ example Reply with quote



Quote:
I am trying to learn C++. Not much OO programming experience.
Bought the book Accelerated C++. I'm stuck on pg.12 as I cannot
get the example code to work. Here is the code:

#include <iostream
#include
int main()
{
std::cout << "Please enter your first name: ";
std::string name;
std::cin >> name;

//Build the message that we intend to write
const std::string greeting = "Hello, " + name + "!";

//Build the second and fourth lines of the output
const std::string spaces(greeting.size(), ' ');
const std::string second = "* " + spaces + " *";

//Build the first and fifth lines of output
const std::string first(second.size(), "*");

//Write all
std::cout << std::endl;
std::cout << first << std::endl;
std::cout << second << std::endl;
std::cout << "* " << greeting << " *" << std::endl;
std::cout << second << std::endl;
std::cout << first << std::endl;

return 0;
}

The problem is in this line:

const std::string first(second.size(), "*");

It should be

const std::string first(second.size(), '*');

The explanation can be found near the top of page 15:

std::string z(n, c);
Defines z as a variable of type std::string that initially
contains n copies of the character c. Here, c must be a
char, not a string or a string literal.

Quote:
The error message does not make sense to me. Hopefully someday they
will. I'm using a Sparc Solaris 9 box. I loaded the Gnu gcc package
from SunFreeWare a little less than a year ago.

Reasonable.

Quote:
What's going on here? Did I miss something obvious? If I did I
apologize. This is so basic C++ I can't imagine why it is not
working. I read some posting about using c_str() because of some
conversion problem by pointing to something that is no longer
there but plugging that into my code without really knowing how or
where to use it did not work for me.

Yes, it's basic. The difference between single and double quotes in C++
is absolutely fundamental, so now is a fine time to get used to it :-)





Regards,


Andrew Koenig


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ben Hutchings
Guest





PostPosted: Fri Aug 22, 2003 9:31 am    Post subject: Re: Problems with Accelerated C++ example Reply with quote

In article <6f1e0d54.0308201652.12af8978 (AT) posting (DOT) google.com>, theo22 wrote:
Quote:
I've scoured the web looking for an answer to this but cannot find an
exact match and am to new to apply some of the detailed answers I
think might fix this.

I am trying to learn C++. Not much OO programming experience.
Bought
the book Accelerated C++. I'm stuck on pg.12 as I cannot get the
example code to work. Here is the code:
snip
//Build the first and fifth lines of output
const std::string first(second.size(), "*");
snip
Here is my error:
bash-2.05$ g++ pg12.cpp
1_1.cpp: In function `int main()':
1_1.cpp:19: invalid conversion from `const char*' to `char'
1_1.cpp:19: initializing argument 2 of `std::basic_string<_CharT,
_Traits, _Alloc>::basic_string(_Alloc::size_type, _CharT, const _Alloc&)
[with _CharT = char, _Traits = std::char_traits<char>, _Alloc =
std::allocator<char>]'

The error message does not make sense to me. Hopefully someday they
will.
snip


Names of classes and functions instantiated from templates can be very hard
to read. The above could be more readably written as:

1_1.cpp:19: initializing argument 2 of `std::string::string(
std::size_t, char, const std::allocator&)'

So you see that the second argument to the constructor after a size_t
should be a char not a const char *. You should have typed '*' rather
than "*".

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
John Potter
Guest





PostPosted: Fri Aug 22, 2003 9:35 am    Post subject: Re: Problems with Accelerated C++ example Reply with quote

On 21 Aug 2003 06:59:28 -0400, [email]theo_22 (AT) yahoo (DOT) com[/email] (theo22) wrote:

A typo in entering the code from the book. In C++, ' ' is a space
character and " " is an array of characters called a C-string.

Quote:
//Build the second and fourth lines of the output
const std::string spaces(greeting.size(), ' ');

This line is correct because it uses a character, ' '.

Quote:
//Build the first and fifth lines of output
const std::string first(second.size(), "*");

This line is an error because it uses a C-string, "*".
Change it back to '*' as in the book and likely in the
VC code that compiled.

John

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Carsten Hansen
Guest





PostPosted: Fri Aug 22, 2003 9:37 am    Post subject: Re: Problems with Accelerated C++ example Reply with quote


"theo22" <theo_22 (AT) yahoo (DOT) com> wrote

Quote:
I've scoured the web looking for an answer to this but cannot find an
exact match and am to new to apply some of the detailed answers I
think might fix this.

I am trying to learn C++. Not much OO programming experience.
Bought
the book Accelerated C++. I'm stuck on pg.12 as I cannot get the
example code to work. Here is the code:
#include <iostream
#include
int main()
{
std::cout << "Please enter your first name: ";
std::string name;
std::cin >> name;

//Build the message that we intend to write
const std::string greeting = "Hello, " + name + "!";

//Build the second and fourth lines of the output
const std::string spaces(greeting.size(), ' ');
const std::string second = "* " + spaces + " *";

//Build the first and fifth lines of output
const std::string first(second.size(), "*");


Here is my error:
bash-2.05$ g++ pg12.cpp
1_1.cpp: In function `int main()':
1_1.cpp:19: invalid conversion from `const char*' to `char'


Look at the last line of the snipped program. The second argument should be
a single char. You are passing a C-string (const char*).
Change the line to
const std::string first(second.size(), '*');


Carsten Hansen


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Tim Sharrock
Guest





PostPosted: Fri Aug 22, 2003 9:45 am    Post subject: Re: Problems with Accelerated C++ example Reply with quote

On 21 Aug 2003 06:59:28 -0400, [email]theo_22 (AT) yahoo (DOT) com[/email] (theo22) wrote:
Quote:
I am trying to learn C++. Not much OO programming experience.
Bought
the book Accelerated C++. I'm stuck on pg.12 as I cannot get the
example code to work. Here is the code:
[snipped to relevant line]
//Build the first and fifth lines of output
const std::string first(second.size(), "*");

Here is my error:
bash-2.05$ g++ pg12.cpp
1_1.cpp: In function `int main()':
1_1.cpp:19: invalid conversion from `const char*' to `char'

You have used the wrong sort of quotes in line 19, it should be
Quote:
const std::string first(second.size(), '*');

This string constructor wants a number, and then a character
and constructs a string with that many copies of the character.

You gave it "*" - which is a string literal (which is the
`const char*' in the error message, rather than '*' which would
the char mentioned.

Tim




--
Tim Sharrock (tim (AT) sharrock (DOT) org.uk)

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
David Bradley
Guest





PostPosted: Fri Aug 22, 2003 9:47 am    Post subject: Re: Problems with Accelerated C++ example Reply with quote

theo22 wrote:

Quote:
//Build the first and fifth lines of output
const std::string first(second.size(), "*");

"*" should be '*'. This form of the constructor takes a size, then a
single character not a character array.

David Bradley

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Dhruv
Guest





PostPosted: Fri Aug 22, 2003 9:51 am    Post subject: Re: Problems with Accelerated C++ example Reply with quote

On Thu, 21 Aug 2003 06:59:28 -0400, theo22 wrote:
[snip]......

Quote:
const std::string first(second.size(), "*");

I guess this should be:

const std::string first(second.size(), '*');


Quote:

Here is my error:
bash-2.05$ g++ pg12.cpp
1_1.cpp: In function `int main()':
1_1.cpp:19: invalid conversion from `const char*' to `char'
1_1.cpp:19: initializing argument 2 of `std::basic_string<_CharT,
_Traits, _Alloc>::basic_string(_Alloc::size_type, _CharT, const _Alloc&)
[with _CharT = char, _Traits = std::char_traits<char>, _Alloc =
std::allocator<char>]'

HTH,
-Dhruv.







[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Bo Persson
Guest





PostPosted: Fri Aug 22, 2003 9:52 am    Post subject: Re: Problems with Accelerated C++ example Reply with quote


"theo22" <theo_22 (AT) yahoo (DOT) com> skrev i meddelandet
news:6f1e0d54.0308201652.12af8978 (AT) posting (DOT) google.com...
Quote:
I've scoured the web looking for an answer to this but cannot find
an
exact match and am to new to apply some of the detailed answers I
think might fix this.

I am trying to learn C++. Not much OO programming experience.
Bought
the book Accelerated C++.

A good book. Congratulations!

Quote:
I'm stuck on pg.12 as I cannot get the
example code to work. Here is the code:
#include <iostream
#include
int main()
{
std::cout << "Please enter your first name: ";
std::string name;
std::cin >> name;

//Build the message that we intend to write
const std::string greeting = "Hello, " + name + "!";

//Build the second and fourth lines of the output
const std::string spaces(greeting.size(), ' ');
const std::string second = "* " + spaces + " *";

//Build the first and fifth lines of output
const std::string first(second.size(), "*");

The problem is here. This constructor takes a character literal, which
uses single quotes '*'. See page 14.

Quote:

//Write all
std::cout << std::endl;
std::cout << first << std::endl;
std::cout << second << std::endl;
std::cout << "* " << greeting << " *" << std::endl;
std::cout << second << std::endl;
std::cout << first << std::endl;

return 0;
}


I plugged in this code in Microsoft VC++ 6 and Visual Studio.net
2003
and it worked fine. RATS! I really wanted to learn on my Sun box
but the code did not work there.

It shouldn't have, unless you corrected the typo there.

Quote:

What's going on here? Did I miss something obvious? If I did I
apologize. This is so basic C++ I can't imagine why it is not
working. I read some posting about using c_str() because of some
conversion problem by pointing to something that is no longer
there
but plugging that into my code without really knowing how or where
to
use it did not work for me.

You will learn that later in the book.



Bo Persson

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
John Ericson
Guest





PostPosted: Fri Aug 22, 2003 9:53 am    Post subject: Re: Problems with Accelerated C++ example Reply with quote

"theo22" <theo_22 (AT) yahoo (DOT) com> wrote

<snip>
Quote:
const std::string spaces(greeting.size(), ' ');
snip
const std::string first(second.size(), "*");

const std::string first(second.size(), '*');

<snip>
- -
HTH, JE


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Steven E. Harris
Guest





PostPosted: Fri Aug 22, 2003 9:54 am    Post subject: Re: Problems with Accelerated C++ example Reply with quote

[email]theo_22 (AT) yahoo (DOT) com[/email] (theo22) writes:

Quote:
//Build the first and fifth lines of output
const std::string first(second.size(), "*");
^^^


Make that asterix /string/ into an asterix /character/:

const std::string first(second.size(), '*');

See the difference? The string constructor taking a size_type first
expects the second argument to be of type value_type, or char in your
case.

--
Steven E. Harris :: [email]seharris (AT) raytheon (DOT) com[/email]
Raytheon :: http://www.raytheon.com

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ron
Guest





PostPosted: Fri Aug 22, 2003 10:02 am    Post subject: Re: Problems with Accelerated C++ example Reply with quote

Quote:
const std::string first(second.size(), "*");

^
Error xyzzy: formal argument must be type char, not char *
Detail: Use single quotes!

-- Ron

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Alexandre Cossette
Guest





PostPosted: Fri Aug 22, 2003 12:40 pm    Post subject: Re: Problems with Accelerated C++ example Reply with quote

[email]theo_22 (AT) yahoo (DOT) com[/email] (theo22) wrote
Quote:
I'm stuck on pg.12 as I cannot get the example code to work.
Here is the code:
#include <iostream
#include
int main()
{
// ...

// Here is line 19:
Quote:
const std::string first(second.size(), "*");
// ...
}

Here is my error:
bash-2.05$ g++ pg12.cpp
1_1.cpp: In function `int main()':
1_1.cpp:19: invalid conversion from `const char*' to `char'
1_1.cpp:19: initializing argument 2 of `std::basic_string<_CharT,
_Traits, _Alloc>::basic_string(_Alloc::size_type, _CharT, const _Alloc&)
[with _CharT = char, _Traits = std::char_traits<char>, _Alloc =
std::allocator<char>]'

The problem that makes the error message hard to understand come from
this name (with the arguments) :
std::basic_string<_char, _std::char_traits _std::allocator<char> >

It is the real name of std::string. If you replace it, the error
message is simpler:

1_1.cpp: In function `int main()':
1_1.cpp:19: invalid conversion from `const char*' to `char'
1_1.cpp:19: initializing argument 2 of
`std::string::string(_std::allocator<char>::size_type, char, const
_std::allocator<char>&)

std::string::string is the name of a constructor for std::string. It
takes 3 parameters : a count, a character, and an allocator. We don't
care about the allocator since there is a default parameter for it. (I
will also make _std::allocator<char>::size_type shorter to make it
easier to read.)

So far, here is the simplified error message:

1_1.cpp: In function `int main()':
1_1.cpp:19: invalid conversion from `const char*' to `char'
1_1.cpp:19: initializing argument 2 of
`std::string::string(size_type, char)

The line 19 is
const std::string first(second.size(), "*");

One important thing to know about C++ is that string literals enclosed
in double quotes ("*") are not the same as character literals enclosed
in single quotes ('*'). You need to write '*'.

Since C++ is built on top of C, "*" is not automatically of type
std::string. It is an array of char (char[]) and when you use it, it
becomes a pointer to the content of the array (char*). That why 'const
char*' appears in the error message instead of std::string.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
llewelly
Guest





PostPosted: Fri Aug 22, 2003 1:05 pm    Post subject: Re: Problems with Accelerated C++ example Reply with quote

[email]theo_22 (AT) yahoo (DOT) com[/email] (theo22) writes:
[snip]
Quote:
I am trying to learn C++. Not much OO programming experience.
Bought
the book Accelerated C++. I'm stuck on pg.12 as I cannot get the
example code to work. Here is the code:
#include <iostream
#include
int main()
{
std::cout << "Please enter your first name: ";
std::string name;
std::cin >> name;

//Build the message that we intend to write
const std::string greeting = "Hello, " + name + "!";

//Build the second and fourth lines of the output
const std::string spaces(greeting.size(), ' ');
const std::string second = "* " + spaces + " *";

//Build the first and fifth lines of output
const std::string first(second.size(), "*");

You need single quotes and not double quotes:

const std::string first(second.size(), '*');

'*' is a charecter literal; it describes a single
charecter. (Mnemonic: single quote: single character.)

"*" is a string literal; it describes a string of 0 or more
charecters. Technically, it's a 2-element array of const char,
containing { '*' , '' } . I don't know what your level of
experience is, but if you don't understand arrays, don't worry
about it; for now, think of "*" as a string literal; accelerated
c++ explains arrays in chapter 10, and you don't need to know
anything else about it until then.

Quote:

//Write all
std::cout << std::endl;
std::cout << first << std::endl;
std::cout << second << std::endl;
std::cout << "* " << greeting << " *" << std::endl;
std::cout << second << std::endl;
std::cout << first << std::endl;

return 0;
}

Here is my error:
bash-2.05$ g++ pg12.cpp
1_1.cpp: In function `int main()':
1_1.cpp:19: invalid conversion from `const char*' to `char'

In the short term, when you see 'const char*' in an error message,
think 'probably something to do with a string literal'.


Quote:
1_1.cpp:19: initializing argument 2 of `std::basic_string<_CharT,
_Traits, _Alloc>::basic_string(_Alloc::size_type, _CharT, const _Alloc&)
[with _CharT = char, _Traits = std::char_traits<char>, _Alloc =
std::allocator<char>]'

This hunk names the constructor the compiler tried to
use. 'basic_string' is the first part of it you need to learn to
recoginize. Wherever you see 'basic_string' in an error message,
think 'std::string' - unless you are using wstring, or some other
string. The stuff between <> and [] you may usually ignore,
because you will usually be using string, and those are always the
same from string. If for some reason you are mixing string,
wstring, and / or some other instantiation of basic_string, you'll
need to watch what's in the <> and [], but that is rare.

If the compiler was smart enough to use typedefs in its
messages, and leave out unused defaulted parameters, it would have
said: 'std::string::string(size_t, char)' instead. Recognizing
this is a matter of experience. It can be deduced from an
understanding of templates and what templates and typedefs are
provided by <string>, but by the time you learn enough to deduce
it, you'll be recognizing it automaticly via experience. Whenever

The next part you are concerned with is the parameters of the
constructor:
'(_Alloc::size_type, _CharT, const _Alloc&)'
'_Alloc::size_type' is some unsigned integral type. The only part
you need to recogize is the 'size_type' part - wherever you see
that, think 'unsigned integral type' . Think of _CharT as 'char' .
'_Alloc' you may ignore, as allocators are rarely used.

Quote:

The error message does not make sense to me. Hopefully someday they
will.

Unfortunately, one needs to understand templates to make sense of
these kinds of error messages in general; the above is specific to
error messages about std::string, and assumes other kinds of
string are not used.

Quote:
I'm using a Sparc Solaris 9 box. I loaded the Gnu gcc package
from SunFreeWare a little less than a year ago.

I plugged in this code in Microsoft VC++ 6 and Visual Studio.net 2003
and it worked fine.
[snip]


If so, they must have some extension.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.