c++

Parsing C++ to generate unit test stubs

I've recently been trying to create units tests for some legacy code. I've been taking the approach of using the linker to show me which functions cause link errors, greping the source to find the definition and creating a stub from that. Is there an easier way? Is there some kind of C++ parser that can give me class definitions, in ...

How to convert (not neccessarily programatically) between Windows' wchar_t and GCC/Linux one?

Suppose I have this Windows wchar_t string: L"\x4f60\x597d" and L"\x00e4\x00a0\x597d" and would like to convert it (not neccessarily programatically; it will be a one-time thing) to GCC/Linux wchar_t format, which is UTF-32 AFAIK. How do I do it? (a general explanation would be nice, but example based on this concrete case would be...

How to split a string?

What's the most elegant way to split a string in C++? The string can be assumed to be composed of words separated by whitespace. (Note that I'm not interested in C string functions or that kind of character manipulation/access. Also, please give precedence to elegance over efficiency in your answer.) The best solution I have right now ...

Could you recommend any open source projects where the source is extensively tested with unit tests?

Programming is learned by writing programs. But code reading is said to be another good way of learning. I would like to improve my unit-testing skills by reading, examining real-world code. Could you recommend any open source projects where the source is extensively tested with unit tests? I'm interested in code written in C++. Thanks...

How do I sort a std::vector by the values of a different std::vector?

I have several std::vector, all of the same length. I want to sort one of these vectors, and apply the same transformation to all of the other vectors. Is there a neat way of doing this? (preferably using the STL or Boost)? Some of the vectors hold ints and some of them std::strings. Pseudo code: std::vector<int> Index = { 3, 1, 2 ...

When I calculate a large factorial, why do I get a negative number?

So, simple procedure, calculate a factorial number. Code is as follows. int calcFactorial(int num) { int total = 1; if (num == 0) { return 0; } for (num; num > 0; num--) { total *= num; } return total; } Now, this works fine and dandy (There are certainly quicker and more elegant solutions,...

error handling when taking user input

Hello everybody, I am a beginner in c++ and I have a small problem: my code displays a simple menu to the user providing three options: cout << "\nQuadratic equation: a*X^2 + b*X + c = 0 main menu: "; cout << "\n <r> Give new coefficients"; cout << "\n <c> Calculate equations solutions"; cout << "\n <t> Terminate the program"; ...

What advantages does C++ have over other languages for Qt development?

As well-known, C++ has steeper learning curve than most of the mainstream languages, which results in better performance . But, does using C++ over other languages[like Java,Ruby,Python] for Qt development have still (major) advantages,let's say about Qtopia? If any, what are they? ...

error-handling-when-taking-user-input part2

I made previously a question: http://stackoverflow.com/questions/236354/error-handling-when-taking-user-input and I made the suggested changes: char displayMainMenu() { char mainMenuChoice; cout << "\nQuadratic equation: a*X^2 + b*X + c = 0 main menu: "; cout << "\n <r> Give new coefficients"; cout << "\n <c> Calculate equations so...

What's an easy way to obtain the current svn revision in a c++ visual studio application

Is there any easy way to access the SVN repository revision number and store it in a c++ string in a c++ visual studio application? Thanks for your help in advance! ...

designing business msgs parser / rewriting from scratch

I take care of critical app in my project. It does stuff related to parsing business msgs (legacy standard), processing them and then storing some results in a DB (another apps picks that up). After more then a year of my work (I've other apps to look after as well) the app is finally stable. I've introduced strict TDD policy and I have ...

Should operator<< be implemented as a friend or as a member function?

That's basically the question, is there a "right" way to implemente operator<< ? Reading this I can see that something like: friend bool operator<<(obj const& lhs, obj const& rhs); Is prefered to something like ostream& operator<<(obj const& rhs); But I can't quite see why should I use one or the other. My personal case is: frie...

Problem with inheritance in C++

Here's my problem: I have a virtual method defined in a .h file that I want to call in a class that inherits from the base class. Sadly though, the method in the derived class doesn't get called. Is there a better way to implement what I'm trying to do? #ifndef ofxBASE_SND_OBJ #define ofxBASE_SND_OBJ #include "ofConstants.h...

C++ Nested classes driving me crazy...

i am trying to compile this very simple piece of code class myList { public: std::vector<std::string> vec; class Items { public: void Add(std::string str) { myList::vec.push_back(str); }; }items; }; int main() { myList newList; newList.items.Add("A"); } what can i do to make this work ...

Where do you find templates useful?

At my workplace, we tend to use iostream, string, vector, map, and the odd algorithm or two. We haven't actually found many situations where template techniques were a best solution to a problem. What I am looking for here are ideas, and optionally sample code that shows how you used a template technique to create a new solution to a ...

Does "std::size_t" make sense in C++?

In some code I've inherited, I see frequent use of size_t with the std namespace qualifier. For example: std::size_t n = sizeof( long ); It compiles and runs fine, of course. But it seems like bad practice to me (perhaps carried over from C?). Isn't it true that size_t is built into C++ and therefore in the global namespace? Is a ...

What are the coolest examples of metaprogramming that you've seen in C++?

What are the coolest examples of metaprogramming that you've seen in C++? What are some practical uses of metaprogramming that you've seen in C++? ...

Learning the source code's filename at compile time

I'm using GCC; __FILE__ returns the current source file's entire path and name. Is there a way to get just the file's name and not its whole path too (at compile time)? Is it possible to do this in a portable way? Can template meta programming be applied to strings? I am using this in an error logging macro. I really do not want my sour...

Is it possible to write a function like next_permutation but that only permutes r values, instead of n?

std::next_permutation (and std::prev_permutation) permute all values in the range [first, last) given for a total of n! permutations (assuming that all elements are unique). is it possible to write a function like this: template<class Iter> bool next_permutation(Iter first, Iter last, Iter choice_last); That permutes the elements in ...

User-defined literals in C++0x, a much needed addition or making C++ even more bloated?

C++0x will introduce user-defined literals which will allow the introduction of new literal syntax based on existing literals (int, hex, string, float) so that any type will be able to have a literal presentation. Examples: // imaginary numbers std::complex<double> operator "i"(double d) // cooked form { return std::complex<double...