Skip to main content
GameDev.net gamedev.net
🔒 Locked

std::string == NULL???

Started by ms291052 Jan 1, 2004 at 10:13 PM 25 replies 3.6k views
Original Post
ms291052
ms291052
For some odd reason my CBitmap class doesn''t seem to like the following line (m_strFileName is a std::string): cout << "Loading bitmap: " << m_strFileName.c_str() << endl; Everytime it throws a System.NullReferenceException on the first line that uses m_strFileName.c_str() (if i take out that line the error is on the line hBitmap = (HBITMAP)LoadImage(NULL, m_strFileName.c_str(), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION) Any ideas as to why or how to fix it? P.S. If it means anything sometimes I get errors(from my debugger) saying that it can''t find media file cbàààà~ or can''t find media file [white background, black dot(bullet) that i think is like ASCII char 2].
LordElectro
LordElectro
Does

cout << "Loading bitmap: " << m_strFileName << endl;

work?


BetaShare - Run Your Beta Right!
BetaShare - Run Your Beta Right!
ms291052
ms291052
CBitmap.cpp(66): error C2679: binary ''<<'' : no operator found which takes a right-hand operand of type ''std::string'' (or there is no acceptable conversion)
Leiarchy9
Leiarchy9
Maybe m_strFileName is empty? Maybe the file/bitmap doesn''t exist? Maybe you didn''t load the bitmap onto your application correctly?

--------
"Do you believe in Ghost?"
--------"Do you believe in Ghost?"
ms291052
ms291052
Well here''s all my constructors for my CBitmap class as well as the first bit of Init:

CBitmap::CBitmap()
{
...
m_strFileName = "";
}
CBitmap::CBitmap(char* strFileName)
{
...
m_strFileName = strFileName;
}
CBitmap::CBitmap(char* strFileName, D3DCOLOR ColorKey)
{
...
m_strFileName = strFileName;
}
CBitmap::CBitmap(char* strFileName, D3DCOLOR ColorKey, int x, int y)
{
...
m_strFileName = strFileName;
}
void CBitmap::Init()
{
HBITMAP hBitmap;
cout << "
Loading bitmap: " << m_strFileName << endl;
hBitmap = (HBITMAP)LoadImage(NULL, m_strFileName.c_str(), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
...
}


So i don''t think it could possibly not exist
Leiarchy9
Leiarchy9
Comparing a char* to a std::string could cause some problems........

--------
"Do you believe in Ghost?"
--------"Do you believe in Ghost?"
Madgap
Madgap
Are you mixing iostream.h and string? (Not sure if that would make a difference.)

You might try this quick check to see if the filename is valid:


if(m_strFileName.size() == 0)
{
cout << "Bad file name." << endl;
return;
}


[edited by - Madgap on January 1, 2004 11:48:10 PM]
-Madgap
ms291052
ms291052
Where am I comparing the two?
Nervo
Nervo
quote:
Original post by Leiarchy9
Comparing a char* to a std::string could cause some problems........

--------
"Do you believe in Ghost?"


He is taking the c_str() conversion of the string which should return a pointer to the char array.

If your string is empty then the pointer returned by c_str() should be null.
Well, R2D22U2..
UltimaX
UltimaX
To see if the string is empty add this:

if(m_strFileName.length() > 0)
{
cout << "Loading bitmap: " << m_strFileName.c_str() << endl;
hBitmap = (HBITMAP)LoadImage(NULL, m_strFileName.c_str(), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
}

Leiarchy9:
I don''t see where he is comparing it. m_strFileName.c_str() returns a const char* anyway.
Hull
Hull
Are you using vc.net?
"Self awareness is the interaction between 3 different parts of your brain. The Cog, the Left and the Right side of you brain..."
ms291052
ms291052
Added that and there was no extra output but the location of the error changed (pointed to somewhere in the std::string .cpp file under the length() function).
ms291052
ms291052
quote:
Original post by Hull
Are you using vc.net?



yes
UltimaX
UltimaX
Did you include the header like this:
#include <string>
using namespace std;

I don't know if it would make a difference or not?

EDIT:
I'm not sure then... I don't do .net yet.

-UltimaX-

"You wished for a white christmas... Now go shovel your wishes!"

[edited by - UltimaX on January 1, 2004 11:51:32 PM]
Nervo
Nervo
quote:
Original post by UltimaX
Did you include the header like this:
#include <string>
using namespace std;

I don''t know if it would make a difference or not?

EDIT:
I''m not sure then... I don''t do .net yet.

-UltimaX-

"You wished for a white christmas... Now go shovel your wishes!"

[edited by - UltimaX on January 1, 2004 11:51:32 PM]


You know what. I have a feeling it might be that
Well, R2D22U2..
ms291052
ms291052
#include <d3dx9.h>
#include <string>
#include <iostream>
#include "cbitmap.h"
#include "output.h"
#include "errors.h"
#define SAFE_RELEASE(p) if(p != NULL){(p)->Release();(p)=NULL;}
using namespace std;

Also, some it successfully loads a few files, then it comes to one (apparently with either no or some weird ascii name) and bombs out.
Hull
Hull
VC.NET compiler really messes up the old iostream libraries with the new ones, therefore you must make sure that you dont include any old iostream header files like: iostreams.h
instead always use iostreams

A warning is issued when compiling with the wrong iostream version like this: warning(xxxx): OLD_IOSTREAMS_ARE_DEPRECATED.

If you cannot resolve this problem anyway then I suggest you turn back to vc6.0 because I dont think there is a solution to your problem other than that the compiler handles your std libs inaccurate. Unless M$ has any hints or bugfixes.

I have the same problem porting my game engine to compile with VC.NET so if you DO find a solution, please post something here.

Otherwise dont use std iostreams or strings, write your own.

M$ really rawks, dont they........


[edited by - Hull on January 1, 2004 12:01:40 AM]
"Self awareness is the interaction between 3 different parts of your brain. The Cog, the Left and the Right side of you brain..."
ms291052
ms291052
I''m not using any old libs and I don''t have 6.0, just .NET .
ms291052
ms291052
After extensive commenting out of my CBitmap class I have it not fully functioning (well, not really doing anything at all) but my app runs again but now I'm getting the same exception at here:

if(strPath == NULL || strlen(strPath) > 990)
return;
m_FileName = new char[999];
strcpy(m_FileName, strPath); //This line

(strPath is a char* argument to the function and m_FileName is a member of my MESH class)

This is just rediculously strange

Edited to add that it would seem as though either these functions are being called without the constructors of their classes being called first, or my app is, for some reason, feeding bullshit into these functions (pardon the french).

[edited by - ms291052 on January 1, 2004 12:23:04 AM]
ms291052
ms291052
OK, I''ve decided that since I''ve been having these problems (as well as other oddities in the past, it seems as though the app is calling EVERY constructor/deconstructor with bogus data before it calls it for the first (real) time.

Topic Locked

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

Sign in to reply to this topic.