How does ifstream’s eof() work?

-1 is get‘s way of saying you’ve reached the end of file. Compare it using the std::char_traits<char>::eof() (or std::istream::traits_type::eof()) – avoid -1, it’s a magic number. (Although the other one is a bit verbose – you can always just call istream::eof) The EOF flag is only set once a read tries to read past the end of the file. If I … Read more

C ++ error: a expected initializer before [function name]

The return type for stringThing must be either void or string, not both. You also must include <string>, if you want to use string. Since you want to output the return value of stringThing() in main, I guess it should be But then, you must also return a string from your function for example. Your parameter std::string &teststring won’t work with your const char* argument. So either declare … Read more

Why doesn’t C++ have a garbage collector?

Implicit garbage collection could have been added in, but it just didn’t make the cut. Probably due to not just implementation complications, but also due to people not being able to come to a general consensus fast enough. A quote from Bjarne Stroustrup himself: I had hoped that a garbage collector which could be optionally … Read more

How to convert a single char into an int [duplicate]

You can utilize the fact that the character encodings for digits are all in order from 48 (for ‘0’) to 57 (for ‘9’). This holds true for ASCII, UTF-x and practically all other encodings (see comments below for more on this). Therefore the integer value for any digit is the digit minus ‘0’ (or 48). … Read more

Why am I getting “undefined reference to main”

Your compiler’s command line contains -I -c sequence. This -I option “swallows” your -c option. -I requires an additional argument, which is an include directory name. You failed to supply that argument, which is why -I assumes that -c that follows it is the directory name. So that -I consumes that -c. The compiler never sees that -c. Without -c it assumes that you want to compile and link your program. Since Gladius.cpp does not have main in it, … Read more

In C++, what is a virtual base class?

Virtual base classes, used in virtual inheritance, is a way of preventing multiple “instances” of a given class appearing in an inheritance hierarchy when using multiple inheritance. Consider the following scenario: The above class hierarchy results in the “dreaded diamond” which looks like this: An instance of D will be made up of B, which … Read more

Debug assertion failed. C++ vector subscript out of range

Regardless of how do you index the pushbacks your vector contains 10 elements indexed from 0 (0, 1, …, 9). So in your second loop v[j] is invalid, when j is 10. This will fix the error: In general it’s better to think about indexes as 0 based, so I suggest you change also your first loop to this: Also, to access the elements of a … Read more

Use of “this” keyword in C++

Yes, it is not required and is usually omitted. It might be required for accessing variables after they have been overridden in the scope though: Also, this: It is pretty bad style; if you need an initializer with the same name use this notation: More info here: https://en.cppreference.com/w/cpp/language/initializer_list

What does “<<" and ">>” mean in C++ for cout/cin?

It depends on how you overload it for you class. In case of std::cout, << is used to write to standard output. >> is not overloaded for std::cout. So std::cout >> x would give compilation error. In case of std::cin,>> is used to read from standard input. << is not overloaded for std::cin. So std::cin << x would give compilation error. For your custom class, you can overload << or >>, or both, … Read more

Undefined reference to vtable

The GCC FAQ has an entry on it: The solution is to ensure that all virtual methods that are not pure are defined. Note that a destructor must be defined even if it is declared pure-virtual [class.dtor]/7. Therefore, you need to provide a definition for the virtual destructor:

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)