C++ Postfix calculator using stacks
When you have a number – you are pushing on its ASCII value. You need to push on its value so change to
When you have a number – you are pushing on its ASCII value. You need to push on its value so change to
No there is no byte data type in C++. However you could always include the bitset header from the standard library and create a typedef for byte: NB: Given that WinDef.h defines BYTE for windows code, you may want to use something other than BYTE if your intending to target Windows. Edit: In response to … Read more
Both vector and string are in the std namespace so you should add it to the declaration of member variables of those types. Change the code to: EDIT: (thanks to Kerrek SB): also you can not define a vector of Node as a member of Node. Instead use a vector of pointers to node like so: std::vector<Node*> vecini[];
You don’t have to do anything. The map will be in ascending order according to the values of the key. Internally, the map performs a comparison between keys to order its elements. By default, it uses std::less<KEY>, which is equivalent to bool operator<(int, int) for integers. For user defined types, you have to options: Implement a bool operator<(const MyType&, … Read more
The problem is the two return 0; statements in your function. The function returns a std::string, which has no constructors that accept an int as input. But, it does have a constructor that accepts a const char * pointer, which 0 is implicitly convertible to. However, constructing a std::string with a null char * pointer is undefined behavior, and your implementation has chosen to throw a std::logic_error exception that … Read more
You can use the random functionality included within the additions to the standard library (TR1). Or you can use the same old technique that works in plain C:
I created a vector of vertex pointers and now I want to print them out. I tried to print them out by dereferencing the pointers, but that didn’t work and I got Error: indirection requires pointer operand (‘int’ invalid). I’ve printed out pointers before, but I have never run into this error. Any help would be … Read more
You should not call your destructor explicitly. When you create your object on the stack (like you did) all you need is: When you create your object on the heap, you kinda need to delete your class before its destructor is called and memory is freed: (Failing to call delete on this last example will result in … Read more
You shouldn’t use strcpy() to copy a std::string, only use it for C-Style strings. If you want to copy a to b then just use the = operator.
Heres one trick I’ve heard of people using. I’ve never seen it in the wild though. And it only applies to C because C++ has RAII to do this more idiomatically.