Monday

Solutions to C++ error: request for member 'variableName' in '(LinkedList*)this', which is of pointer type 'LinkedList*' (maybe you meant to use '->' ?)

What is the error of the following program?

#include <iostream>
using namespace std;

template <typename T>
class LinkedList
{
private:
    string variableName = "";
public:
    void setVariableName(string variableName)
    {
        this.variableName = variableName;
        cout << "You cannot let the \"this\" pointer "
             << "to use the dot operator \".\", "
             << "change to the right arrow operator instead" << endl;
    }
}; // put semicolon or else error: expected ';' after class definition

int main(int argc, char* argv[])
{
    LinkedList<int> linkedList;

    linkedList.setVariableName("variableName");

    return 0;
}

Solutions:
take note that the dot operator or "." cannot be used for
the "this" pointer

warning: In instantiation of 'void LinkedList<T>::setVariableName(std::__cxx11::string)
[with T = int; std::__cxx11::string = std::__cxx11::basic_string<char>]':
warning continued: required from here
error: request for member 'variableName' in '(LinkedList<int>*)this',
which is of pointer type 'LinkedList<int>*' (maybe you meant to use '->' ?)

error: request for member 'variableName' in '(LinkedList<int>*)this',
which is of pointer type 'LinkedList<int>*' (maybe you meant to use '->' ?)
Solution:
dot operator error at the "this" pointer
you cannot use the dot operator or "." for the "this" pointer,
change to the right arrow operator or ->

No comments:

If a hater attacked your age and not the goodness of you

Whether young or old, I've always been known what endures. I've known the very idea of people that were all created equal and deserv...