What is the use of *this in combine function given below

Sales_data& Sales_data::combine(const Sales_data &rhs) { units_sold += rhs.units_sold; // add the members of rhs into revenue += rhs.revenue; // the members of ”this” object return *this; // return the object on which the function was called } I saw above method somewhere, and I am confused why *this pointer is being returned? even though without… Read More What is the use of *this in combine function given below

Interfacing with C library, why does this not work (strcpy, std::string, char *)?

I am writing a program that needs to interface with an external C library. I have to pass a char* parameter to a function, to get a name of a data set. Essentially the situation look like below #include <string> #include <iostream> #include <string.h> void give_name(char* name) { const char* name_test = "test"; strcpy(name, name_test);… Read More Interfacing with C library, why does this not work (strcpy, std::string, char *)?

Reassigning std::bind using auto: which compiler gets it right?

MSVC compiles the following code, while GCC and Clang don’t. #include <iostream> #include <functional> class Class { public: void display() { std::cout << "display" << std::endl; } void store() { std::cout << "store" << std::endl; } }; int main() { Class instance; // std::function<void(void)> f; // this will work auto f = std::bind(&Class::display, instance); f();… Read More Reassigning std::bind using auto: which compiler gets it right?

Error redefinition of static class member and method in C++ linked to multiple file

I have an header file header: #ifndef __DATABASE_HELPER_H__ #define __DATABASE_HELPER_H__ class DatabaseHelper{ public: static QJsonDocument* database; DatabaseHelper(); static Card* selectJSonCard(std::string cardCode); static void testFunctions(); static bool isLeader(std::string cardCode); }; #endif QJsonDocument* DatabaseHelper::database = &(QJsonDocument()); void DatabaseHelper::testFunctions(){ std::cout << "test" << std::endl; } //and so on for the others Now I need it to be included… Read More Error redefinition of static class member and method in C++ linked to multiple file

Why can't the using keyword be used for a forward declaration when defining a function?

Given the following header and source, in which a simple function GiveMeATwo is forward declared in the header test.hpp, within the namespace SomeNamespace, why does a conflict occur when trying to bring the function GiveMeATwo into scope via using SomeNamespace::GiveMeATwo, prior to defining GiveMeATwo? test.hpp #ifndef _TEST_HEADER_ #define _TEST_HEADER_ namespace SomeNamespace { int GiveMeATwo(); }… Read More Why can't the using keyword be used for a forward declaration when defining a function?

Warning in visual studio in simple sfinae code

I am writing a simple code for learning sfinae. However, it generates a warning only in Visual Studio (not when using g++). The code is: #include <iostream> #include <type_traits> template <class T, typename std::enable_if<std::is_integral<T>::value, nullptr_t>::type = nullptr> void f(T t) { std::cout << "Integer" << std::endl; return; } template <class T, typename std::enable_if<!std::is_integral<T>::value, nullptr_t>::type =… Read More Warning in visual studio in simple sfinae code

How to initialize the array-like member variable in the constructor?

How to initialize the array-like member variable? The visual studio code says: no matching function for call to ‘Node::Node()’ gcc line 12 col 9 const int N = 100; struct Node { int val, ch[2]; /** void init(int _val) { this->val = _val, this->ch[0] = this->ch[1] = 0; }*/ Node (int _val): val(_val) { this->ch[0]… Read More How to initialize the array-like member variable in the constructor?

Getting first 5 even numbers element from vector after reverse const iteration

I have this code below that generates 15 random integers and uses them to initialize my vector, intVec. What I’m trying to do here is to iterate through the vector in reverse order and only print out the first 5 even numbers encountered while iterating. I tried using the erase method to just print the… Read More Getting first 5 even numbers element from vector after reverse const iteration