473,795 Members | 3,151 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 10677
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...@gm ail.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.co mwrote:
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
8185
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 effectively impossible to forward declare std::string. (Yes I am aware that some libraries have a string_fwd.h header, but this is not portable.) That said, is there any real reason why I can't derive an otherwise empty
2
24206
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. Regards Giampiero
9
3302
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
11510
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 example string str1; as a class member and then add text to it. or do I have to specify it's length when defining? 2. How to convert from std::string to LPCSTR
12
3154
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
8368
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& operator<<(std::wostream& stream, std::string str) { stream << str.c_str(); return stream;
10
10141
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 Appendix D recommendation won't compile in Microsoft VC++ 6.0). My question is in reference to MultiByte Character Sets. Will this code perform as expected? I understand every problem has a simple and elegant solution that is wrong.
84
15906
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
2904
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 "" // 'words' are any sequences of non-whitespace characters // leading, trailing and multiple whitespace characters // should be ignored.
11
4846
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 "?", where "*" matches zero or more consecutive occurrences of any character and "?" matches a single occurrence of any character. Does boost or some other library have this capability? If boost does have this, do i need to include an entire
0
9672
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10164
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9042
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7540
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3727
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.