strange integar runtime error

Started by
14 comments, last by Tradone 18 years ago
void IfPushPop(){			if ( truthTable[ truthTable.size()-2 ] )				truthTable[ truthTable.size()-1 ] = !truthTable[ truthTable.size() ];			else				truthTable[ truthTable.size()-1 ] = 0;		};


Yep, there's a bad index in there. You've got undefined behaviour there, which means that it can "seem to work" sometimes, too.

As noted, make use of .back(). If you need the element before that, you have the options that were provided, but again, be careful that the element is there. (Actually, you'll need at least one element in order to get .back().)

Also, C++ provides a real boolean type, called bool. Why not really use it (i.e. actually store booleans and compare them "naturally"; and refer to the values "true" and "false" when returning)?

Also, please get out of this habit of using weird prefixes like "If". Use namespaces instead. (And give your class a better name than "IfClass", which is effectively just calling it "Class").

Also, printing a class that way is not very flexible. The standard C++ idiom is something like this:

friend ostream& operator<< (ostream& os, const IfClass& ic) {  // I'll keep your name for now :s  for ( int i=0; i < truthTable.size(); i++ )    os << truthTable;  }  return os;}


Now calling code can do print the object "naturally" as if it were any built-in type: std::cout << myInstance << ", which allows for chaining" << std::endl;.
Advertisement
Quote:Original post by Zahlman
void IfPushPop(){			if ( truthTable[ truthTable.size()-2 ] )				truthTable[ truthTable.size()-1 ] = !truthTable[ truthTable.size() ];			else				truthTable[ truthTable.size()-1 ] = 0;		};



yea, that was a typo.
I fixed the bold text.
but still, I have a runtime error.

when I test out the function FunctionEvalIf(std::string)
and when I output the results of that function, I get the currect output,
I just can't pass that output into truthTable.IfPushBack( FunctionEvalIf(para_if) );
and when I truthTable.IfPushBack( some_number );
it runs fine, only when I put the results in the IfPushBack method I get a problem.
Quote:Original post by Zahlman

Also, C++ provides a real boolean type, called bool. Why not really use it (i.e. actually store booleans and compare them "naturally"; and refer to the values "true" and "false" when returning)?


I was using bool instead of int
until the runtime error, I was thinking maybe I was perhaps using the bool wrong? and instead of "true" and "false" I was using 1 and 0. is that fine?

edit: i changed it back to bool

[Edited by - Tradone on March 31, 2006 7:14:22 PM]
//inside IfClass.h#if !defined(IFCLASS)#define IFCLASS#include <vector>class IfClass{	public:		IfClass(){};		void IfPushBack( bool para_truth ){			bool truth=para_truth;			truthTable.push_back( truth );		};		void IfPushBack2( bool truth ){			std::cout << "(" << truth << ")";		};		void IfPopBack(){			truthTable.pop_back();		};		void IfPushPop(){			if ( truthTable.at(truthTable.size()-2) )				truthTable.at(truthTable.size()-1) = !truthTable.at(truthTable.size()-1);			else				truthTable.at(truthTable.size()-1) = 0;		};		bool IfCheck(){			if( truthTable.at(truthTable.size()-1) )				return 1;			return 0;		};		void IfPrint(){			for ( int i=0; i < truthTable.size(); i++ )				std::cout << truthTable.at(i);		}	//end public:	private:		std::vector<bool> truthTable;	//end private:};#endif//outside to the main.cpp	bool TempBool=FunctionEvalIf(para_if);	std::cout << TempBool;	truthTable.IfPushBack(TempBool);


Still the same runtime error. :(
I must be autistic or something, tell me directly what to do.

should I change all .size()-1's into .end() ?



seems like vector<bool> is a special type, i'm gonna start by looking at that.
I rewrote the script and it seems to work fine. <br><br>Thanks, it was probably a stupid error that has nothing to do with any of those, but I did fix the errors pointed out to me. Thanks!<br><br>oh, and I'm going to look through that conventional printing method using operator overloading.

This topic is closed to new replies.

Advertisement