c++

External linkage

$3.5/2- "When a name has external linkage, the entity it denotes can be referred to by names from scopes of other translation units or from other scopes of the same translation unit." Should not the highlighted 'or' be 'and' or is it just a wrong day for me to interpret some English stuff? An additional question is if the...

Strange calculation

Hello, I can't understand why does my program do such calculations: #define PixelsPerMeter 40.0 // 40 pixels ~ 1 meter #define Meters2Pixels(meters) (float)meters*PixelsPerMeter #define Pixels2Meters(pixels) (float)pixels/PixelsPerMeter // The speed of free falling #define G Meters2Pixels(9.81) // ... float mHeight = 768; float _wind...

Semicolon in namespace. Necessary?

Hello, when working with namespace, I need to finish it with a semicolon? When I put a forward declaration of a class into a namespace, for example, many people doesn't include a semicolon but, it seems to be optional. Does semicolon add functionality or change the current functionality by adding or removing? Thanks. ...

complicated expression involving logical AND (&&)

void main(void) { int x,y,z; x=y=z=1; z = x && y && ++z;//is this fine? } I have lately started reading about sequence points stuffs but I cannot figure out whether the above sample of code is fine or not. I know the && operator introduces a sequence point so I am not very sure about the behavior of the expression z = x && y && ...

friend in operator == or << when should i use it?

Hi guys, i feel i have a bit of a "hole" in my understanding of friend usage: I have this class called "presentation" int the code there is a : if(present1==present2) both are type presentation.. what i did was bool operator==(const presentation& p) const; and in the answers: friend bool operator==(Presentation&, Presentation&);...

strange behavior of initialization in C++

I have two classes Base and Derived from it: class Base{ public: Base(int = 0); Base(Base&); Base& operator=(const Base&); protected: int protectedData; private: int baseData; }; /////////////DERIVED CLASS class Derived: public Base{ public: Derived(int = 0); Derived(Derived&); Derived(Base&); Derive...

Insert into 2D vector

Could you do the insert operation in one line along with allocating memory for internal vector? vector <vector<int>> myvector; int a[] = {0, 1, 2, 3, 4}; for (int index = 0; index < 2; index++) { myvector.push_back(vector<int>()); //allocate memory for internal vector myvector[index].insert(myvector[index].begin(),...

C++ Default copy consturctor

I understand compiler won't generate default copy ctor if copy ctor is declared private in a class. But can someone explain why compiler does that? What happens if copy ctor is declared protected? Would compiler provide default copy ctor? What happens if copy ctor is declared private but have a definition e.g. foo(const& obj){} ...

Why use exception instead of returning error code

Possible Duplicate: Exceptions or error codes Hi, I am looking for some guidelines on when to use return values v/s exceptions. Many thanks in advance. ...

How to interpret the callstack of visual studio?

> vcam.ax!CopyMediaType() + 0x49 bytes vcam.ax!CMediaType::Set() + 0x41 bytes vcam.ax!CMediaType::operator=() + 0x2f bytes vcam.ax!CVCamStream::SetFormat(_AMMediaType * pmt=0x00000000) Line 201 C++ FlashPlayer.exe!005641be() Can someone explain the above line by line? ...

Complicated code for obvious operations

Hi, Sometimes, mainly for optimization purposes, very simple operations are implemented as complicated and clumsy code. One example is this integer initialization function: void assign( int* arg ) { __asm__ __volatile__ ( "mov %%eax, %0" : "=m" (*arg)); } Then: int a; assign ( &a ); But actually I don't understand why is it w...

C++ reference in base class constructor

I have a logic in base class constructor. The result of the logic has to be captured in the derived class constructor in a temporary variable. Is there a way to do it? For example class Base { Base() { int temp_value = some_logic; } }; class Derived : public Base { Derived() { // need the temp value here.. } }; Thanks, Goku...

initializing struct object declared in class private in c++

Hi all, I am declaring my struct object inside my class private and i am initializing them using constructors but my style checker says that the member function of my struct type are not initialized. Can any one help me in this regards i will be very thankful to you. Below is my code please suggest some solution to this problem clas...

Mutual Exclusion and Semaphores

Hello. I am writing a program (for homework) that simulates a unisex bathroom. Only 4 people are allowed at a time and men and woman cannot enter if the other sex is already using the bathroom. My problem is with allowing a max of 4 people in the bathroom. As you can see from the output, only 1 person is getting into the restroom at ...

ISO C++ forbids declaration of 'Game' with no type - possible include problem?

compiler output: g++ -Wall -g main.cpp `sdl-config --cflags --libs` -lSDL_mixer In file included from Game.h:8, from main.cpp:1: DrawableObject.h:11: error: ISO C++ forbids declaration of ‘Game’ with no type DrawableObject.h:11: error: expected ‘;’ before ‘*’ token DrawableObject.h:13: error: expected ‘)’ before ‘*’ tok...

How can I use bit shifting to replace integer division

I understand how to do it for powers of 2 so that's not my question. For example, if i want to find 5% of a number using a bit shift instead of an integer divide, how would i calculate that? So instead of (x * 20 / 19), i could do (x * 100 >> 11). Now this isnt right but it's close and i arrived at it using trial and error. how would ...

Virtual tables when copying objects.

#include <iostream> #include <string> class A { public: A(int a) : _a(a) {} virtual ~A() {} virtual void f() const {std::cout << _a << std::endl;} private: int _a; }; class B : public A { public: B(int a, int b) : A(a), _b(b) {} virtual void f() const {std::cout << _b << std::endl;} private: int _b; }; int mai...

develop c++ without Xcode IDE

I want to develop c++ programs on mac os and I have installed Xcode with a bunch of frameworks. However I would like to write code without Xcode IDE but just write my own makefile and directly compile/link with gcc (shipped with Xcode). Take a opengl program as example I tried to compile it with the command gcc -I/usr/include/ ...

Algorithm or code for float to base 2 scientific notation (IEEE 32 bits) in C++?

I'm taking in a float as input, and then outputting its equivalent representation in base 2 scientific notation. This is in IEEE 32 bits with: 31 sign bit, 23-30 exponent (with 127 offset), 0-22 mantissa (with implicit leading 1). One of the conditions which I'm not exactly sure the meaning of is "Your mantissa should have the implicit ...

non density based Data clustering algorithm

Hi, I'm working on a cluster analysis program that takes a set of points S as an input and labels each point with that index of the cluster it belong to. I've implemented the DBScan and OPTICS algorithms and they both work as expected. However, the results of those algorithms can be very different depending on the initial values of MinP...