c++

Why does C++ behave this way?

#include<stdio.h> class A { public: int a;}; class B: public A { int c; int d; }; int main() { A* pA = new B[10]; B* pB = new B[10]; printf("\n%d", pA->a); pA++; printf("\n%d", pA->a); // prints junk value printf("\n\n%d", pB->a); pB++; printf("\n%d", pB->a); return 0; } The second pr...

Windows Threading: beginthread or QueueUserWorkItem (C++)

Hi Folks, I am wondering whether to use beginthread or QueueUserWorkItem for threaded methods in C++. What are the differences between the two APIs and in what context are they better suited? Thanks, BTW, I have read this question http://stackoverflow.com/questions/331536/windows-threading-beginthread-vs-beginthreadex-vs-createthread...

C++ Beginner - Simple block of code crashing, reason unknown.

Hello everyone, Here's a block of code I'm having trouble with. string Game::tradeRandomPieces(Player & player) { string hand = player.getHand(); string piecesRemoved; size_t index; //Program crashes while calculating numberOfPiecesToTrade... size_t numberOfPiecesToTrade = rand() % hand.size() + 1 for (; numberOfPiecesToTra...

Is there a way to limit an integer value to a certain range without branching?

Just out of curiosity. If I have something like: if(x < 0) x = 0; if(x > some_maximum) x = some_maximum; return x; Is there a way to not branch? This is c++. Addendum: I mean no branch instructions in the assembly. It's a MIPS architecture. ...

Cast vector<T> to vector<const T>

I have a member variable of type vector<T> (where is T is a custom class, but it could be int as well.) I have a function from which I want to return a pointer to this vector, but I don't want the caller to be able to change the vector or it's items. So I want the return type to be const vector<const T>* None of the casting methods I tr...

How to return array of C++ objects from a PHP extension

I need to have my PHP extension return an array of objects, but I can't seem to figure out how to do this. I have a Graph object written in C++. Graph.getNodes() returns a std::map<int, Node*>. Here's the code I have currently: struct node_object { zend_object std; Node *node; }; zend_class_entry *node_ce; then PH...

Undefined reference to `xlCreateBookCA' C++

Hey, I'm trying to use the Libxl library in my eclipse c/c++ project. Right now I'm using the minGW compiler in eclipse. With this following code: Book* book = xlCreateBook(); I get this error: /src/xls2csv.cpp:22: undefined reference to `xlCreateBookCA' Any help would be greatly appreciated :) ...

SWT-like GUI toolkit for C

Hello, do you know any cross-platform gui toolkit like swt for C (using default widgets in each operating system = right pics on eclipse.org/swt) ? There is an implementation of swt for D language called DWT but I need it for C or C++. Thanks. ...

How to support comparisons for QVariant objects containing a custom type?

According to the Qt documentation, QVariant::operator== does not work as one might expect if the variant contains a custom type: bool QVariant::operator== ( const QVariant & v ) const Compares this QVariant with v and returns true if they are equal; otherwise returns false. In the case of custom types, their equalnes...

What is a cross-platform way to get the current directory?

I need a cross-platform way to get the current working directory (yes, getcwd does what I want). I thought this might do the trick: #ifdef _WIN32 #include <direct.h> #define getcwd _getcwd // stupid MSFT "deprecation" warning #elif #include <unistd.h> #endif #include <string> #include <iostream> using namespace std; int mai...

shielding #include within namespace { } block?

Edit: I know that method 1 is essentially invalid and will probably use method 2, but I'm looking for the best hack or a better solution to mitigate rampant, mutable namespace proliferation. I have multiple class or method definitions in one namespace that have different dependencies, and would like to use the fewest namespace blocks or...

lambda+for_each+delete on STL containers

I'm trying to get a simple delete every pointer in my vector/list/... function written with an ultra cool lambda function. Mind you, I don't know c**p about those things :) template <typename T> void delete_clear(T const& cont) { for_each(T.begin(), T.end(), [](???){ ???->delete() } ); } I have no clue what to fill in for the ???'...

Segmentation fault in std function std::_Rb_tree_rebalance_for_erase ()

(Note to any future readers: The error, unsurprisingly, is in my code and not std::_Rb_tree_rebalance_for_erase () ) I'm somewhat new to programming and am unsure how to deal with a segmentation fault that appears to be coming from a std function. I hope I'm doing something stupid (i.e., misusing a container), because I have no idea how...

What should be done in place of ddeexec for MDI apps?

We have been using ddeexec registry entries to handle opening a design from Explorer. MSDN (about 2/3 way into article) indicates that ddeexec is deprecated, and applications should use IDropTarget instead. What is unclear to me is how that this actually is supposed to work.. e.g. If I have Foo.exe, which is intended to handle .foo fi...

Simple average calculation

I'm trying to write program calculating average of given numbers stored in an array. Amount of numbers should be not more than 100, and user should input them until a !int variable is given : #include <iostream> #include <conio.h> using namespace std; double average(int tab[], int i){ int sum=0; for(int j=0; j<i; ++j){ ...

How return a std::string from C's "getcwd" function

Sorry to keep hammering on this, but I'm trying to learn :). Is this any good? And yes, I care about memory leaks. I can't find a decent way of preallocating the char*, because there simply seems to be no cross-platform way. const string getcwd() { char* a_cwd = getcwd(NULL,0); string s_cwd(a_cwd); free(a_cwd); return s_...

Point to point linear gradient?

I want to make an application that can generate point to point gradient (like Photoshop does). I'm familiar with how to generate an up to down gradient but not point to point. How is this conceptually done. Thanks ...

Need Help finding an appropriate task assignment algorithm for a college project involving coordinating in the field workforce through PDAs

I am a long time lurker here and have found over time many answers regarding jquery and web development topics so I decided to ask a question of my own. This time I have to create a c++ project for college which should help manage the workflow of a company providing all kinds of services through in the field teams. The ideas I have so ...

Boost Binary Endian parser not working?

I am studying how to use boost spirit Qi binary endian parser. I write a small test parser program according to here and basics examples, but it doesn't work proper. It gave me the msg:"Error:no match". Here is my code. #include "boost/spirit/include/qi.hpp" #include "boost/spirit/include/phoenix_core.hpp" #include "boo...

Compiling a C++ application on Windows 7, but execute it on Win2003 Server

I have a C++ application (quite complex, multiple projects) in Visual Studio 2008, that produces a single dll. Recently I switched to Windows 7, but had previously been compiling under Windows XP. Suddenly the dll in question cannot be loaded by another application, i.e. on a machine running Windows 2003 Server. I've been trying variou...