Jump to content
  • Advertisement
Sign in to follow this  
rossmills

Strings in C++

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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; ?

Share this post


Link to post
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 this post


Link to post
Share on other sites
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? :-)

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!