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

error C2040: 'std::string' differs in levels of indirection from'const char *'

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
Aug 19 '08 #1
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
Aug 19 '08 #2
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);
Aug 19 '08 #3
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.
Aug 20 '08 #4
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.

Aug 20 '08 #5
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.
Aug 20 '08 #6
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....
Aug 20 '08 #7

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

Similar topics

10
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...
2
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. ...
9
by: Jim Langston | last post by:
#include <string> int main () { std::string MyString = "Testing"; MyString = " " + MyString; } This works in Microsoft Visual C++ .net 2003
6
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...
12
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.
3
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&...
10
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...
84
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; };
11
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 "" //...
11
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 "?",...
0
isladogs
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...
1
isladogs
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...
0
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...
1
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)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
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....
0
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
0
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...
0
isladogs
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...

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.