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

“Multiple definition of” C++ compiler error

Why do you #include lines.cpp in ThreeD.cpp? This is very unusual. Your makefile wants lines.o, so you’re going to compile lines.cpp. Anything defined in lines.cpp will be in lines.o and also in ThreeD.o. There is an intriguing comment in lines.cpp: I think the instructor wants you to break lines.cpp into a .h and a .cpp. … 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: