c++

How does one avoid accidentally redeclaring global constants in C++?

I have a template matrix class class defined in a header called "Matrix.h". Certain matrices are used repeatedly in my program. I thought that I would define these in the "Matrix.h" header file, like so: const Matrix<GLfloat> B_SPLINE_TO_BEZIER_MATRIX(4, 4, values); When I do this g++ complains that I redefined the constant in questi...

Compile error using cl.exe (Visual Studio 2008) for this cpp code

I'm getting compile error in this code #include<iostream> #include<cstdio> #include<string> using namespace std; void main(int argc,char *argv[]) { int i; for(i = 0;i<10;i++) fprintf(cout,"%d\n",i); fprintf(cout,"abc:\n"); string s; cin>>s; if(s == "resum...

protected inheritance vs. public inheritance and OO design

When do you use each inheritance? class Base{}; class Derived: protected Base{}; class Derived2: public Base{}; My case: I have class called Snapshot which only contains GetXXX methods. It is a light-weight classed used to store current state of the Value class. I also use it for recovery, keeping instances of this class long after ...

Send C++ string to C# string. Interop.

I am new to inter process communication and need some help. I want to be able to send a string from a C++ program to a C# program. My problem is that the resultant string is gibberish. Here is my code: Sending program (C++): void transmitState(char* myStr) { HWND hWnd = ::FindWindow(NULL, _T("myApp v.1.0")); if (hWnd) {...

Virtual behavior by storing pointers to member functions

Why is virtual behavior being prevented? class MyClass { //........ virtual double GetX(); virtual double GetSomethingElse(); virtual double GetT(); virtual double GetRR(); //........ }; class Processor { private: typedef double (MyClass::*MemFuncGetter)(); ...

Sending string (of characters) to active window

I wrote a simple program that reads the characters from external device (bar code scanner) from serial port (/dev/ttyS1) and feeds it to the currently active window (using XSendEvent). Program works fine on faster computers, but on slow ones the situation happens (very often) that characters don't get received in the same order they wer...

C++ question about setting class variables

Hello all, I'm not new to programming, but after working in Java I'm coming back to C++ and am a little confused about class variables that aren't pointers. Given the following code: #include <iostream> #include <map> using namespace std; class Foo { public: Foo() { bars[0] = new Bar; bars[0]-...

Passing a list of numbers to a function in C++ without building array first?

I'm trying to build a function that accepts an array in the following manner: int inCommon = findCommon({54,56,2,10}, 4); int findCommon(int nums[], int len){ for(int i=0; i<len; i++) cout<<nums[i]<<endl; return 1; } Note, that's not actually what my function does, but I do loop through the array. I'm just trying to determine if...

What does `vcall'{0,{flat}}' signify in below example ?

I am not sure if the question is already addressed. I was checking the one of the Stack overflow function and got this doubt. Lets check the code first: #include <string> #include <map> #include <iostream.h> class MyClass { public: virtual int Func() { return 0; } int Func2() { return 0; } }; cla...

What are the requirements for C++ template parameters?

If you are using a template in C++ that takes an integer value as a parameter, are there any requirements on an integer variable used as the parameter that are different than if the variable was used as a parameter in a function call? This is a follow-up to question here . I specifically want to address if there is a difference WRT v...

How do I call a function only once per value in C++?

I'm looking for an easy-to-use macro for calling a function only once for a specific value. For example: void foo( Object* obj ) { // Print out the name of each object only once DO_ONCE( obj, printf("This gets printed only once per object! %s\n",obj->GetName()) ); } Then Object obj1("obj1Name"),obj2("obj2Name"); foo(&obj1); foo...

c++ class with template cannot find its constructor

I have a problem I don't really understand. I have a class Node. template<class T> class node { protected: T _data; public: node(T data); }; This is in "node.h" file. In "node.cpp" file, there is this constructor: #include "node.h" template<class T> node<T>::node (T data) { _data = data; } While the compiler finds no ...

C++ - Constructor overloading - private and public

Hi All, Can you tell me why the following code is giving me the following error - call of overloaded "C(int)" is ambiguous I would think that since C(char x) is private, only the C(float) ctor is visible from outside and that should be called by converting int to float. But that's not the case. class C { C(char x) { } p...

base enum class inheritance

Is there a pattern where I can inherit enum from another enum in C++?? something like that: enum eBase { one=1, two, three }; enum eDerived: public Base { four=4, five, six }; ...

fast way to copy one vector into another

I prefer two ways: void copyVecFast(const vec<int>& original) { vector<int> newVec; newVec.reserve(original.size()); copy(original.begin(),original.end(),back_inserter(newVec)); } void copyVecFast(vec<int>& original) { vector<int> newVec; newVec.swap(original); } How do you do it? ...

What is the best Evaluation Kit for Learning Embedded C/C++ Development?

I am trying to improve my embedded C/C++ development on ARM architecture. I have recently moved from 68K development to ARM and wanted to use some of my spare time to dig into the platform and learn the best practices especially on developing for mobile platforms. Preferably 32bit architecture will be helpful with supporting development...

What is ** in C++?

I've seen some code, as well as some errors generated from my compiler that have a '**' token before the variable (eg **variablename unreferenced-- or something, I can't recall exactly offhand). I'm fairly certain this is related to pointers, if I had to guess it looks like it's trying to dereference twice. '**' is fairly ungoogleable....

C++ Pointers to Member Functions Inheritance

I have a need to be able to have a super class execute callbacks defined by a class that inherits from it. I am relatively new to C++ and from what I can tell it looks like the subject of member-function-pointers is a very murky area. I have seen answers to questions and random blog posts that discuss all sorts of things, but I am not s...

Visual Studio projects with multiple folders

Is there an easy way to use multiple folders in a project with Visual Studio? It has "filters" which look like folders, but it would be really nice to be able to make folders and insert files in them inside VS. Is there an add-in or secret option to enable this behavior? ...

Global Keyboard Hook in Linux?

How to write global keyboard hook in Ubuntu (Linux) (like Hook for Windows) In C/C++ or Python ...