c++

What's the cleanest way to fail gracefully when a file can't be opened in C++?

The program requires the file to run, but if for any myriad number of reasons it can't be found or isn't readable, etc - what's the cleanest way to fail out of the program? ...

C++ Homework, stuck with some edge cases and unusual input

I have a C++ assignment to complete over the next couple of weeks. The task is to read a line of input containing 3 scores between 0 and 89, and read a name from the same line. Convert those scores to marks and give each student a grade depending on their marks. My first cut has done all of this (albeit in a messy way) and now I am...

How do I specialize a static member of a template class on a templated type?

Say I have the following class: template<class T> struct A { static int value; }; template<class T> int A<T>::value = 0; I can specialize A::value on a concrete type without problems: struct B { }; template<> int A<B>::value = 1; I'd like to specialize A::value on a template type, I tried the following: template<class T> str...

c++ references array

Hello, I wounder how i can make this code work? #include <iostream> using namespace std; void writeTable(int (&tab)[],int x){ for(int i=0;i<x;i++){ cout << "Enter value " << i+1 <<endl; cin >> tab[i] ; } } int main(void){ int howMany; cout << "How many elemets" << endl; cin >> howMany; int tab...

Are >? or <? legitimate operators in any C++ dialect?

I ran across the following lines of C++ code in a file (non-contiguous lines) that gcc 4.2.1 won't accept: int frame = blk <? mBlkCnt-1; mInsCnt = blk <? mBlkCnt; mInsCnt = mInsCnt+1 <? mBlkCnt; const int to_read = (mFileSz-byte_off) <? mBlkSz; Both <? and >? are used in various places in the code. They appear to be a shorthand for as...

Is volatile required here?

EDITED and refined my question after Johannes's valuable answer bool b = true; volatile bool vb = true; void f1() { } void f2() { b = false; } void(* volatile pf)() = &f1; //a volatile pointer to function int main() { //different threads start here, some of which may change pf while(b && vb) { pf(); } } So...

Which one of these two ways of writing this code would be more suited?

I've got a code that as you can see I can write in either of two following ways, the matter is the only difference is since in second function the parameter is declared as non-constant I can use that instead of declaring a new variable(num1 in first function) but I' curious which one would be more suited if there would be any difference ...

c++ interesting function define

Hi all, i have examined code of RDcode. And i come across a defined function and i dont understand. Can you help me to this code. template <typename T> class argless { public: argless(const T& c) : container(c) {}; // i dont understant this line. bool operator() (unsigned int v1,unsigned int v2){ return container[...

Calculating the spread of the hash function for a hashmap which uses chaining

Hello everyone, I am writing a generic hash map in C++ which uses chaining to deal with collisions. Say if I have a hash map with 11 buckets, and I insert 8 items. The hash function will distribute it as follows: bucket[0] = empty bucket[1] = 2 elements bucket[2] = empty bucket[3] = 1 element bucket[4] = 1 element bucket[5] = 3 eleme...

Xoreax Incredibuild alternatives ?

Hi guys, Does anyone knows if there are some alternatives for the Xoreax Incredibuild, this tool is powerful but also too expensive for me. I have project written in c/c++ with about three hundred thousand lines of code and using VS2005 to compile my code is just awfully slow. Thanks to everyone. ...

Fastest way to run a program in a 64 bit environment?

It's been a couple of decades since I've done any programming. As a matter of fact the last time I programmed was in an MS-DOS environment before Windows came out. I've had this programming idea that I have wanted to try for a few years now and I thought I would give it a try. The amount of calculations are enormous. Consequently I want ...

Strings and character encoding in C++

I read a few posts about best practices for strings and character encoding in C++, but I am struggling a bit with finding a general purpose approach that seems to me reasonably simple and correct. Could I ask for comments on the following? I'm inclined to use UTF-8 and UTF-32, and to define something like: typedef std::string string8;...

How to compile dynamic library for a JNI application on linux?

I'm using Ubuntu 10.10 So that's what I did. Hello.java: class Hello { public native void sayHello(); static { System.loadLibrary("hellolib"); } public static void main(String[] args){ Hello h = new Hello(); h.sayHello(); } } Then I ran the follwing commands: dierre@...

cast Derived*const to Base*const

Edit - Put the question into context a bit more. Given: struct Base { ... }; struct Derived : public Base { ... }; class Alice { Alice(Base *const _a); ... }; class Bob : public Alice { Bob(Derived *const _a); ... }; When I try to implement Bob::Bob(Derived *const _d) : Alice(static_cast<Base*const>(_d)) { } ...

Wrote to a file using std::wofstream. The file remained empty...

I wrote the following program using VS2008: #include <fstream> int main() { std::wofstream fout("myfile"); fout << L"Հայաստան Россия Österreich Ελλάδα भारत" << std::endl; } When I tried to compile it the IDE asked me whether I wanted to save my source file in unicode, I said "yes, please". Then I run the program, and myfile ap...

C++ Inheritance Question

In the file test.cpp, I have this: template <typename T> class A { public: A(int a){}; virtual ~A(); private: }; class B : public A<int> { public: B(int a):A(a){}; virtual ~B(); private: }; int main() { return 0; } When I compile it, I get this: jason@jason-linux:~/Documents/ECLibrary$ g++ -g -Wall -Wextra -pedantic-err...

How to get the exact position of an element in a set?

I have a std::set<std::string> and I want to know the exact position of the element in the set after the insertion. I tried with std::distance but without any luck: #include <iostream> #include <string> #include <set> #include <iterator> using namespace std; int main (int argc, char const *argv[]) { string array[] = { "zero", "o...

How can I make sure all of my c++ boost threads finish before the program exits?

I know I can call thread.join() to force a thread to complete before the current thread can proceed. However, my program has a bunch of files that are read into memory, modified, and then flushed to disk. Each flush is being done in a separate thread so that the current thread can continue while the contents are flushed to disk. I could...

Which programming language/framework shall I choose for this project?

Hi, I have two projects ahead and I'm pretty new at Ruby and Ruby on Rails. It's real fun! =) Besides, I count myself fluent in C++ and Java (and JavaEE), but have very very few experience with C#.NET and Ruby. Also, I'm pretty eager to learn Ruby, but don't really want to waste time to develop skills on C# - which is pretty similar to ...

I'm having troubles understanding msdn's documentation about _beginthreadex and _endthreadex

I'm reading the documentation about _beginthreadex and _endthreadex but there are a few things I don't understand. Note that the documentation keeps documenting the "extended" and normal functions at the same time, but I'm not using _beginthread and _endthread; only their extended versions. You can call _endthread or _endthreade...