Hello,
This program doesn't work and provides me the errror message "error
C2040: 'p' : 'std::string' differs in levels of indirection from
'const char *'":
#include <string>
int main()
{
const char* p = "fred";
std::string(p);
return 0;
}
This version DOES work:
#include <string>
int main()
{
const char* p = "fred";
std::string named(p);
return 0;
}
Can someone explain why the first program doesn't compile? Aren't I
just creating a temporary std::string object initialised from p?
Regards,
Pete 6 10559
On 19 Aug., 19:02, newbar...@gmail.com wrote:
Hello,
This program doesn't work and provides me the errror message "error
C2040: 'p' : 'std::string' differs in levels of indirection from
'const char *'":
#include <string>
int main()
{
* * * * const char* p = "fred";
* * * * std::string(p);
* * * * return 0;
}
This version DOES work:
#include <string>
int main()
{
* * * * const char* p = "fred";
* * * * std::string named(p);
* * * * return 0;
}
Can someone explain why the first program doesn't compile? Aren't I
just creating a temporary std::string object initialised from p?
No. std::string(p); is the same statement as std::string p; -
parenthesis are allowed in a declaration.
/Peter
peter koch wrote:
No. std::string(p); is the same statement as std::string p; -
parenthesis are allowed in a declaration.
You can get around the problem by writing: (std::string)(p);
On Aug 19, 1:02 pm, newbar...@gmail.com wrote:
Hello,
This program doesn't work and provides me the errror message "error
C2040: 'p' : 'std::string' differs in levels of indirection from
'const char *'":
#include <string>
int main()
{
const char* p = "fred";
std::string(p);
return 0;
}
This version DOES work:
#include <string>
int main()
{
const char* p = "fred";
std::string named(p);
return 0;
}
Can someone explain why the first program doesn't compile? Aren't I
just creating a temporary std::string object initialised from p?
Regards,
Pete
I think the problem is with the compiler since string class contains
a ctor that takes const char * argument.
On 20 Aug, 03:42, puzzlecracker <ironsel2...@gmail.comwrote:
On Aug 19, 1:02 pm, newbar...@gmail.com wrote:
Hello,
This program doesn't work and provides me the errror message "error
C2040: 'p' : 'std::string' differs in levels of indirection from
'const char *'":
#include <string>
int main()
{
* * * * const char* p = "fred";
* * * * std::string(p);
* * * * return 0;
}
This version DOES work:
#include <string>
int main()
{
* * * * const char* p = "fred";
* * * * std::string named(p);
* * * * return 0;
}
Can someone explain why the first program doesn't compile? Aren't I
just creating a temporary std::string object initialised from p?
Regards,
Pete
I think the problem is with the compiler since string class contains
a ctor that takes const char * argument.- Hide quoted text -
- Show quoted text -
I'm glad to hear that. I was wondering if there's something missing in
my knowledge of C++ in this area but as the following compiles, I
don't think so:
const char* p = "fred";
std::string s = std::string(p);
I'm using VC++ .NET 2003 V7.1.
puzzlecracker wrote:
On Aug 19, 1:02 pm, newbar...@gmail.com wrote:
>Hello,
This program doesn't work and provides me the errror message "error C2040: 'p' : 'std::string' differs in levels of indirection from 'const char *'":
#include <string>
int main() { const char* p = "fred"; std::string(p);
return 0;
}
I think the problem is with the compiler since string class contains
a ctor that takes const char * argument.
No, that is not correct.
Read Peter's answer carefully.
The first example is identical to
const char* p = "fred";
std::string p;
It's a simple multiple redeclaration error. The compiler error is less
than helpful.
--
Ian Collins.
On Aug 20, 5:11*am, Ian Collins <ian-n...@hotmail.comwrote:
puzzlecracker wrote:
On Aug 19, 1:02 pm, newbar...@gmail.com wrote:
Hello,
This program doesn't work and provides me the errror message "error
C2040: 'p' : 'std::string' differs in levels of indirection from
'const char *'":
#include <string>
int main()
{
* * * * const char* p = "fred";
* * * * std::string(p);
* * * * return 0;
}
I think the problem is with the compiler since string class contains
a ctor that takes const char * argument.
No, that is not correct.
Read Peter's answer carefully.
The first example is identical to
const char* p = "fred";
std::string p;
It's a simple multiple redeclaration error. *The compiler error is less
than helpful.
--
Ian Collins.
Thanks, I didn't notice that.... This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Angus Leeming |
last post by:
Hello,
Could someone explain to me why the Standard conveners chose to typedef
std::string rather than derive it from std::basic_string<char, ...>?
The result of course is that it is...
|
by: Giampiero Gabbiani |
last post by:
Hi to all,
Is there a simple way to implement a strip algorithm on std::string using
STL?
I'm sure that it's possible to implement it using some transform, but my
knowledge of STL is POOR.
...
|
by: Jim Langston |
last post by:
#include <string>
int main ()
{
std::string MyString = "Testing";
MyString = " " + MyString;
}
This works in Microsoft Visual C++ .net 2003
|
by: Nemok |
last post by:
Hi,
I am new to STD so I have some questions about std::string because I
want use it in one of my projects instead of CString.
1. Is memory set dinamicaly (like CString), can I define for...
|
by: Vincent RICHOMME |
last post by:
Hi,
I am currently implementing some basic classes from .NET into modern C++.
And I would like to know if someone would know a non mutable string class.
|
by: Pierre Barbier de Reuille |
last post by:
Hi,
I was wondering why there was no predefine operator<< puting a
std::string into a std::wostream. The strangest is that something
similar to this would work:
std::wostream&...
|
by: Jeffrey Walton |
last post by:
Hi All,
I've done a little homework (I've read responses to similar from P.J.
Plauger and Dietmar Kuehl), and wanted to verify with the Group. Below
is what I am performing (Stroustrup's...
|
by: Peter Olcott |
last post by:
Is there anyway of doing this besides making my own string from scratch?
union AnyType {
std::string String;
double Number;
};
|
by: Jacek Dziedzic |
last post by:
Hi!
I need a routine like:
std::string nth_word(const std::string &s, unsigned int n) {
// return n-th word from the string, n is 0-based
// if 's' contains too few words, return ""
//...
|
by: tech |
last post by:
Hi, I need a function to specify a match pattern including using
wildcard characters as below
to find chars in a std::string.
The match pattern can contain the wildcard characters "*" and "?",...
|
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...
|
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...
|
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...
|
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)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
| |