I dont know how to explain very well, I have a class that works 100% fine..
The problem is that now I want access that class inside of other class, when I do it, things stop of working fine and gets very unstable( some few times works, but mostly times dont)..
Here is what I get:
Unhandled exception at 0x1049993d (msvcp90d.dll) in pnm.exe: 0xC0000005: Access violation reading location 0xfefeff02.

Now I will show the working methods where I use the ifstream, this works just fine if I use the object declared directly on main:

bool CarregaPNM::OpenPNM( char userstring[100] ){
	//this was on constructor

		for(unsigned short int i=0; i<100; i++){
			_filename[i] = NULL;//clean the array(precaution)
		}

		//copy the user inputed string to the class private string:
		strcpy_s(_filename, (const char*)userstring);



	//

		//if users tries to open another file without closing the file first, clear it:
		if( fileclosed==false)readPNM.clear();

		//open the file with the specified name:
		readPNM.open( (const char*)_filename, ios::binary);
		
		if( readPNM.is_open() ){

			fileclosed = false;//file is not closed(is open)


			//gets width and height of the image----------------
			ReturnPNM_header();

			img_pix = new PIXELS[width*height];//set the pixel matrix with the appropriated size

			LoadPixels();//read the pixel map

			return (true);//sucsses

		}else	return (false);//failed
}//F OpenPNM
void CarregaPNM::ReturnPNM_header(){

	int countspaces=0;

	char skipcomment[200];

	char comm;

	int auxpos=0;


	for( int i=0; countspaces<4; i++ ){//until 3 spaces

		readPNM>>comm;

		if(comm != '#'){//if is not a comment


			int i;

			switch (countspaces){
				case 0: 
					readPNM.unget();
					readPNM >> MV[0] >> MV[1];

					countspaces++;

					break;
				case 1:{
					char strnum[10];
					strnum[0] = comm;
					i=1;
					while((comm=readPNM.get()) != ' '){
						strnum[i++] = comm;
					}
					strnum[i] = '\0';
					width = atoi(strnum);

					countspaces++;

					   }
					break;


				case 2:{
					char strnum2[10];
					strnum2[0] = comm;
					i=1;
					while((comm=readPNM.get()) != '\n'){
						strnum2[i++] = comm;
					}
					strnum2[i] = '\0';
					height = atoi(strnum2);

					countspaces++;

					   }
					break;


				case 3:
					if( MV[1] == '1' || MV[1] == '4' ){//if is PBM, theres no MPV
						countspaces++;
						break;
					}

					readPNM.unget();
					readPNM >> MPV ;

					
					countspaces++;
					break;
			}//F switch countspaces

		}//F if nao for comentario
		else{
			readPNM.getline(skipcomment, 200);//if is a comment, skip the line

		}

	}//F for countspaces <=3


}

The error happens at the ReturnPNM_header(), what happens is that I get very weird values from ifstream, like it arent reading the right memory stuff, so when it takes at :

while((comm=readPNM.get()) != ' '){
						strnum[i++] = comm;
					}

It never find a space, it do forever until access violation...
Remember again, that it works FINE when I use directly on main..But I dont know what happens here, since it just get there if the file is open...Im very lost..

Now Im showing where Im using when get the error:

class InterfacePNM{
private:
...
	CarregaPNM meuPNM;

public:

	InterfacePNM();//constructor

	bool Init_SDL();
	bool LoadInterfaceImages();

	void Handle_SDL_Events();
	void UpdateKeyChanges();
	void UpdateChanges();

	bool _Open_();
	//void _Save_();
	//void _Filter_();


	void MainLoop();

};//F Interface PNM
bool InterfacePNM::_Open_(){

	char inputfilename[100];
	cin >> inputfilename;

	if( meuPNM.OpenPNM( inputfilename ) == false ) return false;

	B_open = false;

	return true;

}

My guess is that Im doing some Class management shit...

Im attaching all the code and the image files if anyone want to test, Im also attaching SDL lib and dll since Im using it in that program..

you did't include "F:\SDL-1.2.13\include\SDL.h in that zip file.

FiltroPNM::ApplyPixMap() has a lot of conversion problems -- converting int to float.

media = ( F_img_pix[i].rgb[0] + F_img_pix[i].rgb[1] + F_img_pix[i].rgb[2]) * 0.33333;

				F_img_pix[i].rgb[0] = media;//transform red
				F_img_pix[i].rgb[1] = media;//transform green
				F_img_pix[i].rgb[2] = media;//transform blue

All those lines produce warnings which need to be fixed. And there are lots more similar problems all throughout that *.cpp code.

You declare yet another local i in if alternative of CarregaPNM::ReturnPNM_header. This declaration hides variable i - loop counter. It's a very strange and most probably erroneous declaration. It seems the program logic is corrupped...

Apropos, avoid illusions: this evil fstream knows nothing about your classes and no such beast as a class management in C++...

You declare yet another local i in if alternative of CarregaPNM::ReturnPNM_header. This declaration hides variable i - loop counter. It's a very strange and most probably erroneous declaration. It seems the program logic is corrupped...

Apropos, avoid illusions: this evil fstream knows nothing about your classes and no such beast as a class management in C++...

I dont get it..I though it was ok because the scope( and it was, since it never give me any problem..)
I just changed it to 'j' and the same problem happens..

you did't include "F:\SDL-1.2.13\include\SDL.h in that zip file.

FiltroPNM::ApplyPixMap() has a lot of conversion problems -- converting int to float.

media = ( F_img_pix[i].rgb[0] + F_img_pix[i].rgb[1] + F_img_pix[i].rgb[2]) * 0.33333;

				F_img_pix[i].rgb[0] = media;//transform red
				F_img_pix[i].rgb[1] = media;//transform green
				F_img_pix[i].rgb[2] = media;//transform blue

All those lines produce warnings which need to be fixed. And there are lots more similar problems all throughout that *.cpp code.

I forgot the '.h' files..sorry.I guess I have to upload all sdl .h files rigth? Im attaching all sdl files..


Yeah, Im getting +- 50 warnings about float conversion, I thought it was ok cause I need that conversion..Whats the best way to make this?
By the way this Filter class works ok too, and Im not using it at now..

"Apropos, avoid illusions: this evil fstream knows nothing about your classes and no such beast as a class management in C++..."

I meant something like including the classes in a wrong way, so something like overwriting the ifstream readPNM could be happening..I dont know..

This article has been dead for over six months. Start a new discussion instead.