c++

Using stirling's factorial formula

I'm trying to write a code that calculates the approximate factorial of a number using Stirling's formula. Here's the line that calculates it: appFact = pow(exp, -num) * pow(num, num) * sqrt(2 * num * PI); The error comes in at pow(exp, -num) with pow underlined in red. IntelliSense: no instance of overloaded function "pow" matches ...

[ICU4C] NumberFormat/DecimalFormat treats certain floating-point values as longs instead of doubles

NumberFormat/DecimalFormat doesn't seem to parse strings with the "#.0" format (where # is any number) as a double. The following code illustrates this: #include <cstdio> #include <iostream> #include <unicode/decimfmt.h> #include <unicode/numfmt.h> #include <unicode/unistr.h> #include <unicode/ustream.h> int main() { UErrorCode sta...

Erasing from a std::vector while doing a for each?

The proper way to iterate is to use iterators. However, I think by erasing, the iterator is invalidated. Basically what I want to do is: for(iterator it = begin; it != end; ++it) { if(it->somecondition() ) { erase it } } How could I do this without v[i] method? Thanks struct RemoveTimedEvent { bool operator()(...

C++: Trouble with parsing XML file (MXML library)

I am having an issue with the following code. It uses the Mini-XML library to extract elements from an xml file. It compiles and runs fine, except that it is unable to get the last "radius element," returning NULL instead (it crashes if I don't check to see if subnode is not NULL). //Start loading <ssys> elements mxml_node_t *node; //...

Is there any way to write a class such that no class can be inherited from it ?

Hi, From link http://www.coolinterview.com/interview/10842/ Is there any way to write a class such that no class can be inherited from it ? From suggestions in the above link, i tried below code class A { A(){} ~A(){} A(const A&); A& operator=(const A&); }; class B: public A { }; The above code doesn't produce any e...

Cross dependencies without forward declaring all used functions?

I have class A (in A.h) which depends on class B in (B.h) and vice versa. Forward declaring the used functions works, but this means I have to update everywhere where I forward declared those functions in the future, ex, if I remove, or change argument in those functions they must all be updated to reflect change. I don't feel this is go...

Initialize costly value in map with minimum lock contention (C++)

Suppose we have a map that is shared between multiple threads. It represents a node in some hierarchical structure (say, a directory in a file system) that is stored on disk. Constructing a Value is expensive both in time and in memory. This is the classic 'initialize on demand' problem, with a twist: we're initializing values in a map w...

Overloading in C++

I am not sure if I am doing the overload correctly. ...\point.h(42) : error C2061: syntax error : identifier 'Vec3' Point operator +(Vec3 a) const; Here is my .h file: #include <fstream> #include <iostream> #include "vec3.h" using std::ofstream; using std::ifstream; using std::cout; using std::cin; usi...

Should unimplemented base class functions be 0'd?

which of the following is better practice? class Foo { virtual void unimplementedFunc() = 0; }; Or class Foo { virtual void unimplementedFunc(); }; //in cpp void Foo::unimplementedFunc() { //not implemented in base } Thanks ...

c++ Increment operator.

Possible Duplicate: Difference between i++ and ++i in a loop? Is there a difference between i++ and ++i ? ...

C++ program instruction

I have written out the program, however once I tried compileing it a get syntax errors and cannot fine where the syntax errors are. Would you know why my program won't compile? It should calculate number of second sound travels in difference gases, information given by user. include <iostream> #include <fstream> using namespace std; ...

glutTimerFunc issues

I am using a timer function to animate, but I am having issues when I place it in the Renderer class. void Renderer::animate(int value) { glutTimerFunc(TIMERMSECS, animate, 0); } Error: \renderer.cpp(242) : error C3867: 'Renderer::animate': function call missing argument list; use '&Renderer::animate' to create a pointer to member ...

"file not found " error while creating a static library in c using gcc - linux

I have a file hello.c #include<stdio.h> void hello() { printf("Hello!\n"); } A header hello.h #ifndef _hello_ #define _hello_ void hello(); #endif main.c #include<stdio.h> #include "hello.h" int main() { hello(); return 0; } I am currently in the folder /home/user/name/programs I am trying to build a static libra...

C++ struct array copy

Guys, I want to copy elements of a struct array to another by using memcpy. I believe this is miserably causing my program to fail for some reason. Also how can I free the memory in the end ? struct FaultCodes { string troubleFound; string causeCode; string actionCode; string paymentCode; string suppCode; u_int16_t multipli...

In xcode when including cmath get error: '::acos' has not been declared, etc

I get the following errors when trying to build a small and simple project that includes <cmath> in Xcode: cmath: '*' has not been declared '::acos' has not been declared In file included from /Xcode4/Projects/libraryLAFMath/Classes/libraryLAFMath.cp In file included from /Xcode4/Projects/libraryLAFMath/Classes/libraryLAFMath.h '::acos'...

Static member object initialization failure

I have a static library with the following code: h file: class Foo { public: Foo() { a = 4; } int a; }; class Bar { public: static const Foo foo; }; cpp file: const Bar::foo = Foo(); My problem is that Bar::foo does not get initialized with a=4 until some time after main(). Before then a=0. I'm trying to ...

How can a pointer be implemented except storing an address?

Recently I answered another question asking for questions every decent C++ programmer should be able to answer. My suggestion was Q: How does a pointer point to an object? A: The pointer stores the address of that object. but user R.. disagrees with the A I propose to the Q - he says that The correct answer would be "it's implementati...

Why declaration/definition must both be in source file for template class in c++?

Anyone can elaborate the reason? ...

How many header files are there in c++ standard?

In C89 there're 15 header files: <assert.h> <locale.h> <stddef.h> <ctype.h> <math.h> <stdio.h> <errno.h> <setjmp.h> <stdlib.h> <float.h> <signal.h> <string.h> <limits.h> <stdarg.h> <time.h> What about the c++ standard? ...

Using TinyXML over a byte stream instead of file

Hello All, Is it possible to use TinyXML over a byte stream instead of a file? Consider this code snippet: TiXmlDocument doc("abc.xml"); if (!doc.LoadFile()) return; TiXmlHandle hDoc(&doc); The above code snippet takes a file as input. How can I modify the code so that it accepts a byte stream? A sample code snippet would be great!...