Skip to main content
gamedev.net
🔒 Locked

problem with std::string

Started by jake_Ghost Dec 19, 2004 at 2:45 PM 14 replies 2.3k views
Original Post
jake_Ghost
jake_Ghost
i got a compile error with this code at the if statement

int BMP::FindTexture(std::string Filename)
{
	int i;
	for (i = 0;i < TexCount; i++)
	{
		if (Filename == BMPTexture.FileName) // std::string works with operator==.  char*'s do not.
		{
			return i;
		}
	}
	return -1;
}

--------------------Configuration: Maia - Win32 Debug-------------------- Compiling... BMP.cpp D:\Shooter\Maia\BMP.cpp(114) : error C2678: binary '==' : no operator defined which takes a left-hand operand of type 'class std::basic_string,class std::allocator >' (or there is no acceptable conversion) Error executing cl.exe. BMP.obj - 1 error(s), 0 warning(s) [Edited by - Fruny on December 19, 2004 3:09:30 PM]
JimPrice
JimPrice
OK - first things first - closing tags aren't _source, it's /source.

Secondly - the error message is telling you that there is no comparison defined for Filename and BMPTexture.FileName.

So - what is BMPTexture? Is it an array of objects containing a public std::string called Filename? If you could show the definition it would help.

Next : if you're passing a parameter like this, you should use:

int BMP::FindTexture(const std::string& Filename) const

It's a const parameter, as you don't alter it. Passing by reference means no temporaries are created. And it's a const function (the trailing const) as it doesn't alter the BMP object.

Jim.
jake_Ghost
jake_Ghost
theres the dec. for my structure
struct BMPTex {  GLuint imageDataNear;  GLuint imageDataLin;  GLuint imageDataMip;  std::string FileName;};


i think that it might be a problem with the struct. i dont think it is workin rite. when i type BMPTexture. i dont see a list of the variables in the struct.
JimPrice
JimPrice
Does BMP contain an array / std::vector of BMPTex objects? Can you show the declaration for BMP as well.

Thanks,
Jim.
jake_Ghost
jake_Ghost
#include <iostream>#include <vector>struct BMPTex {  GLuint imageDataNear;  GLuint imageDataLin;  GLuint imageDataMip;  std::string FileName;};class BMP{public:  // constructor  BMP();  BMP(int TexCount);  // deconstructor  ~BMP();  // public functions  BOOL LoadGLTextures (std::string Filename, int FileNum);  void GenTex (int FileNum);  void BindNear (int FileNum);  void BindLin (int FileNum);  void BindMip (int FileNum);  int FindTexture (const std::string& Filename) const;  void SetTextureName (int FileNum, std::string fileName) {    BMPTexture[FileNum].FileName = fileName;  }  private:  std::vector<BMPTex> BMPTexture;  int TexCount;};
Enigma
Enigma
Have you included the header?

Enigma
jake_Ghost
jake_Ghost
heh no lol thx it cmopiles now !!
jake_Ghost
jake_Ghost
shit lol it doesnt compile cause its having some troubles with the std::string

AUX_RGBImageRec *LoadBMP(std::string Filename)				// Loads A Bitmap Image{	FILE *File=NULL;									// File Handle	//if (Filename)										// Make Sure A Filename Was Given	//{	//	return NULL;									// If Not Return NULL	//}	File=fopen(Filename,"r");							// Check To See If The File Exists	if (File)											// Does The File Exist?	{		fclose(File);									// Close The Handle		return auxDIBImageLoad(Filename);				// Load The Bitmap And Return A Pointer	}		return NULL;										// If Load Failed Return NULL}


--------------------Configuration: Maia - Win32 Debug--------------------
Compiling...
BMP.cpp
D:\Shooter\Maia\BMP.cpp(20) : error C2440: 'type cast' : cannot convert from 'class std::basic_string,class std::allocator >' to 'char'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
D:\Shooter\Maia\BMP.cpp(24) : error C2664: 'auxDIBImageLoadA' : cannot convert parameter 1 from 'class std::basic_string,class std::allocator >' to 'const char *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Error executing cl.exe.

Maia.exe - 2 error(s), 0 warning(s)
Enigma
Enigma
std::string has no implicit conversion to C-style char*. Instead it provides a c_str() member function which returns a C-style const char*. Use this member function to convert your std::string to a const char* which can be passed to fopen. You might also want to look into iostreams, which are C++'s typesafe classes for input/output.

Enigma
JimPrice
JimPrice
Sorry for disappearing off!

This next one is because there is no implicit conversion from string to char*, and fopen wants a char* input.

If you use Filename.c_str() instead of Filename, it turns it into a char array.

You might have the same thing with AuxDibImageLoad.
Jim.

Edit : darn, beaten again!
jake_Ghost
jake_Ghost
nice!! it works now lol. Just one thing.

im not sure how this works exactly

std::vector BMPTexture;

if i were to use that like BMPTexture[7].FileName, would that work? because i will need an array of those but currently it isnt an array rite? So do i have to declare it as an array or use the "new" statment to make an array of it at runtime?

thx jake
Enigma
Enigma
You can either resize it (using the resize member function) to the size you require or you can push_back or insert items into the vector, in which case it will automatically resize itself to accomodate them.

Enigma
jake_Ghost
jake_Ghost
so how do i use the resize and push_back members ?
Krisc
Krisc
They work like this

BMPTexture.push_back(instance of class that vector was made out of)

you would use

BMPTex t;

BMPTexture.push_back(t);

You can then sift through the vector with normal array indecis or use an iterator:
BMPTexture[0] or...
vector::iterator p;
jake_Ghost
jake_Ghost
thx man you've been a great help

jake

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.