Defining a struct in flex error C++

Both vector and string are in the std namespace so you should add it to the declaration of member variables of those types. Change the code to: EDIT: (thanks to Kerrek SB): also you can not define a vector of Node as a member of Node. Instead use a vector of pointers to node like so: std::vector<Node*> vecini[];

How to ensure that a std::map is ordered?

You don’t have to do anything. The map will be in ascending order according to the values of the key. Internally, the map performs a comparison between keys to order its elements. By default, it uses std::less<KEY>, which is equivalent to bool operator<(int, int) for integers. For user defined types, you have to options: Implement a bool operator<(const MyType&, … Read more

How to fix ‘std::logic_error’ what(): basic_string::_M_construct null not valid error?

The problem is the two return 0; statements in your function. The function returns a std::string, which has no constructors that accept an int as input. But, it does have a constructor that accepts a const char * pointer, which 0 is implicitly convertible to. However, constructing a std::string with a null char * pointer is undefined behavior, and your implementation has chosen to throw a std::logic_error exception that … Read more

Random number c++ in some range

You can use the random functionality included within the additions to the standard library (TR1). Or you can use the same old technique that works in plain C:

How do I call the class’s destructor?

You should not call your destructor explicitly. When you create your object on the stack (like you did) all you need is: When you create your object on the heap, you kinda need to delete your class before its destructor is called and memory is freed: (Failing to call delete on this last example will result in … Read more

Examples of good gotos in C or C++

Heres one trick I’ve heard of people using. I’ve never seen it in the wild though. And it only applies to C because C++ has RAII to do this more idiomatically.

Sentinel while loop for C++

A “sentinel” in this context is a special value used to indicate the end of a sequence. The most common sentinel is \0 at the end of strings. A “sentinel while loop” would typically have the form:

Is “argv[0] = name-of-executable” an accepted standard or just a common convention?

Guesswork (even educated guesswork) is fun but you really need to go to the standards documents to be sure. For example, ISO C11 states (my emphasis): If the value of argc is greater than zero, the string pointed to by argv[0] represents the program name; argv[0][0] shall be the null character if the program name is not available from the host environment. … Read more