Public Group

# Strings in C++

This topic is 3995 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am attempting to produce a variable that is a string in C++ A number of places online suggest that it can be used in the following way:
class person
{
public:
string name;
int age;
};
However, this does not seem to work.

##### Share on other sites
What doesn't work? Are you getting a compiler error? If so, what's the error?

##### Share on other sites
Ok. Do you actually have a question?

What do you mean "It doesn't seem to work"? Does the program crash? Does it fail to compile? Does it set the building on fire?

Are you #including <string> ? Also, you should be using std::string, not string (Or put using std::string; above your class declaration, although that's bad to do in headers).

##### Share on other sites
Quote:
 Original post by Evil SteveOk. Do you actually have a question?What do you mean "It doesn't seem to work"? Does the program crash? Does it fail to compile? Does it set the building on fire?Are you #including ? Also, you should be using std::string, not string (Or put using std::string; above your class declaration, although that's bad to do in headers).

It failed to compile and seemed to think that the end of line should be after "string".

##### Share on other sites
Quote:
 Original post by rossmillsIt failed to compile and seemed to think that the end of line should be after "string".
What was the exact error message? It sounds like you're either not including the string header (Strings are classes, not basic types in C++), or you're not using the std namespace properly, so it can't find the string class.

Are you including the string header file, and are you using namespace std; or using std::string; ?

##### Share on other sites
Sounds like you need to do what Evil Steve said -- #include <string> and make sure the type is "std::string" not "string"

##### Share on other sites
Quote:
Original post by Evil Steve
Quote:
 Original post by rossmillsIt failed to compile and seemed to think that the end of line should be after "string".
What was the exact error message? It sounds like you're either not including the string header (Strings are classes, not basic types in C++), or you're not using the std namespace properly, so it can't find the string class.

Are you including the string header file, and are you using namespace std; or using std::string; ?

The problem before was that I was not using the header file. I am using std::string now, and it works on creation of the string, but when I call on it at another section it does not like it:

std::string spritemap;spritemap = "sonic_left.png";	D3DXCreateTextureFromFileEx(d3ddev,								// the device pointer                                spritemap,							// the file name                                D3DX_DEFAULT,						// default width                                D3DX_DEFAULT,						// default height                                D3DX_DEFAULT,						// no mip mapping                                NULL,								// regular usage                                D3DFMT_A8R8G8B8,					// 32-bit pixels with alpha                                D3DPOOL_MANAGED,					// typical memory handling                                D3DX_DEFAULT,						// no filtering                                D3DX_DEFAULT,						// no mip filtering                                D3DCOLOR_XRGB(255, 255, 255),		// white color key                                NULL,								// no image info struct                                NULL,								// not using 256 colors                                &sonic_left);						// load to sprite

Error message:
error C2664: 'D3DXCreateTextureFromFileExW' : cannot convert parameter 2 from 'std::wstring' to 'LPCWSTR'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

I've searched on this forum, but the solutions suggested go completely over my head. Could someone please break it down, perhaps in small words? :-)

##### Share on other sites
Read the error message. The function D3DXCreateTextureFromFileEx does not take in a std::string as the second parameter, it takes in a LPCWSTR, which is basically just a raw wchar* string (old C style string). Luckily, std::string has a method that returns the raw pointer. So just pass spritemap.c_str() as the second parameter instead of spritemap.

##### Share on other sites
If it is thinking that the end of the line should be after std::string you might have forgotten a semicolon at the end of a line of code, also make sure you include strings

#include <string>

and either type std::sring or before main type

using namespace std

##### Share on other sites
Try spritemap.c_str() and if that does not work you may have to change the type of spritemap from std::string to std::wstring.

1. 1
2. 2
Rutin
20
3. 3
khawk
17
4. 4
A4L
14
5. 5

• 12
• 16
• 26
• 10
• 44
• ### Forum Statistics

• Total Topics
633759
• Total Posts
3013720
×