c++

Binary tree that stores partial sums: Name and existing implementations

Consider a sequence of n positive real numbers, (ai), and its partial sum sequence, (si). Given a number x ∊ (0, sn], we have to find i such that si−1 < x ≤ si. Also we want to be able to change one of the ai’s without having to update all partial sums. Both can be done in O(log n) time by using a binary tree with the ai’s as leaf node v...

Interrupt in C++

Hi, I am trying to understand interrupts and am looking for a simple code that uses interrupts. Could somebody please help me with it? ...

Should I use boost::ptr_vector<T> or vector<boost::shared_ptr<T> >?

I need a container of pointers. Would you recommend boost::ptr_vector<T> or std::vector<boost::shared_ptr<T> >? (Or something else?) If that's of interest, my actual data structure is relatively complicated (see here) and currently stores objects, not pointers, but i'd like to change that (using pointer containers), in order to get rid ...

How to resize a window using WM_* messages

Hi everybody! When a window handle is given, how can i exactly resize a window sending windows messages towards it? I've tried many things such as sendig a WM_SIZING Message to the window, but nothing worked(the way i did it). I don't like to use SetWindowPosition. Thanks in advance, David ...

GetFileSizeEx corrupts file handle

Currently I'm using GetFileSizeEx to track how large a log files gets before I write to it. We have limited space and anything if we try to create a file larger than 100 megabytes we stop logging data. The problem is for some reason GetFileSizeEx will corrupt the file handle that I am using. if( hFileHandle != INVALID_HANDLE_VALUE && hF...

Data structure that owns objects and returns IDs for them

I need a data structure that manages integer IDs for T objects (typically std::string). It should support getting the ID for some object and, vice versa, get the object for some ID: // if object not seen before: copies and stores object and returns // new ID, otherwise just returns its ID int Add(const T& obj); // if obj not found: ret...

Switch error:: cannot appear in a constant-expression

Hi Guys - this is a strange one... I am playing with some decompression algo. Instead of going through the char buffer[] and looping until a stop-bit in buffer[i] is found, I am trying use some bit-mask techniques but with chars. I have the following example: // In a *.h file const char ch = '\x81'; // To avoid Endianess unio...

Templated method with overloaded name

To be consistent with other classes in a library, my array class below has two read() methods. The first reads the entire array to an output iterator and returns an error code, the second reads a single value and returns that (using exceptions for errors). The problem I have is that if I call the second read(size_t idx) method with an ...

DLLImport c++ function with optional parameters

I am trying to import a function from an unmanaged code c++ dll into my c# application. The c++ prototype is int somefunction (int param1, int *param2 = NULL); How do I declare this in c# to take advantage of the optional nature of param2? The following code does not work. param2 gets initialized with garbage. DllImportAttribute("...

Is string::c_str() allowed to allocate anything on the heap?

If I need to get a NUL-terminated char array out of a std::string in a situation where I need to be sure nothing will be allocated, is it safe to use c_str to do so? For example, if I'm inside a destructor and I want to copy some data from a string into a pre-allocated, fixed-size buffer, can I use c_str and be sure it won't throw anythi...

Files reading differently in linux? C++

I'm a fairly new programmer, but I consider my google-fu quite competent and I've spent several hours searching. I've got a simple SDL application that reads from a binary file (2 bytes as a magic number, then 5 bytes per "tile") it then displays each tile in the buffer, the bytes decide the x,y,id,passability and such. So it's just lev...

How and when to properly use *this pointer and argument matching?

When I go thru the code written by collegue, in certain place, they use: this->doSomething(); //-->case A doSomething(); //-->case B In fact I'm not sure about the great usage of *this pointer...:( Another question with argument matching: obj.doOperation(); //-->case C (&obj)->doOperation(); //-->case D In fact both cases are per...

C++ inheritance, base methods hidden

I have a simple C++ base class, derived class example. // Base.hpp #pragma once class Base { public: virtual float getData(); virtual void setData(float a, float b); virtual void setData(float d); protected: float data; }; //Base.cpp #include "stdafx.h" #include "Base.hpp" float Base::getData() { return data; ...

Using QStyle to set global default minimum QPushButton height

Is it possible to define a default minimum height for a QPushButton through a custom application wide QStyle? I know this can be achieved using stylesheets, but I rather not use them because of performance reasons. ...

forward declaration generates incompatible type error

Hi I've been reading some on forward declarations, including in this forum. They all say that it saves us from including the header file, However the following code generates an error: #ifndef CLASSA_H_ #define CLASSA_H_ class B; class A { public: A(); ~A(); int getCount(); private: static int _count; int _num; ...

Why BOOST_AUTO_TEST_CASE is not working?

I'm doing something wrong but I do not know what. Here are my files: //main #include "stdafx.h" int _tmain(int argc, _TCHAR* argv[]) { return 0; } //MyFoo.h #pragma once #include "stdafx.h" class MyFoo { public: MyFoo(void){}; int multiplyByTwo(int value); ~MyFoo(void){}; }; //MyFoo.cpp #include "stdafx.h" #inclu...

C++ Detecting an implicit cast of 0 to a class

I am calling a function in a API that returns a Answer object. Under some conditions, the API will return EMPTY_answer, which is defined as such: #define EMPTY_answer ((Answer)0) of course, attempting to access a Answer variable or function from an EMPTY_answer object crashes the application. Trying to test for it using if(lAnswer ...

Mapping a string to a unique number?

Is there a nice bash one liner to map strings inside a file to a unique number? For instance, a a b b c c should be converted into 1 1 2 2 3 3 I am currently implementing it in C++ but a bash one-liner would be great. ...

Why does this dialog box close immediately after opening?

My issue is that I am trying to create a Opengl/Win32 application and I am unable to keep my dialog box open. It literally flashes as if someone pressed cancel on it RIGHT when it opened. I've looked around google and found a few others with this issue, but none of the solutions they posted have helped me, so I turn to the StackOverflow ...

why C# and C++ use _<variableName> coding convention?

Hi... I have seen too much C# and C++ code where the variable naming convention seems to ask programmers to write variable names using underscore before the text of the variable. e.gr. int? _countMoney; What's the rationale supporting that convention? ...