Strings in C++

Started by
14 comments, last by Evil Steve 16 years, 3 months ago
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.
Advertisement
What doesn't work? Are you getting a compiler error? If so, what's the error?
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).
Quote:Original post by Evil Steve
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).


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

Thanks for your help, though.
Quote:Original post by rossmills
It 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; ?
Sounds like you need to do what Evil Steve said -- #include <string> and make sure the type is "std::string" not "string"
Quote:Original post by Evil Steve
Quote:Original post by rossmills
It 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? :-)
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.
"When you die, if you get a choice between going to regular heaven or pie heaven, choose pie heaven. It might be a trick, but if it's not, mmmmmmm, boy."
How to Ask Questions the Smart Way.
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
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.

This topic is closed to new replies.

Advertisement