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:

C++32 (or C++2e), C++ 2032, Cpp 2032, g++ 2032

  Subject: C++ 2032 3 Code example for ? 2 Approximate feature suggestions: - to complete C++29 or C++ 2029 1 Web links, references Wandbox,...