0

I'm getting errors in my code with the this pointer in particular. Here are a portion of the errors:

Prefix.cpp: In member function ‘bool Prefix::isRegistered(int) const’:
Prefix.cpp:47:20: error: invalid types ‘int[int]’ for array subscript
Prefix.cpp: In member function ‘bool Prefix::isRegistered(int, const char*)’:
Prefix.cpp:68:15: error: request for member ‘area’ in ‘this’, which is of non-class type ‘Prefix* const’
Prefix.cpp: In function ‘int minNoDigits(int)’:
Prefix.cpp:98:35: error: invalid use of ‘this’ in non-member function
Prefix.cpp:100:10: error: invalid use of ‘this’ in non-member function
Prefix.cpp:103:18: error: invalid use of ‘this’ in non-member function
CODE.cpp: In member function ‘bool EAN::isRegistered(const Prefix&)’:
CODE.cpp:40:34: error: request for member ‘str’ in ‘this’, which is of non-class type ‘EAN* const’
CODE.cpp:62:35: error: request for member ‘str’ in ‘this’, which is of non-class type ‘EAN* const’
CODE.cpp:65:51: error: passing ‘const Prefix’ as ‘this’ argument of ‘bool Prefix::isRegistered(int, const char*)’ discards qualifiers [-fpermissive]
CODE.cpp:81:34: error: request for member ‘str’ in ‘this’, which is of non-class type ‘EAN* const’

The cpp file:

#include <string.h>
using namespace std;
#include "Prefix.h"



//Checks if the area element is valid.
bool Prefix::isRegistered(int area) const
{
   int index;
   bool found=false;

   //Search within the prefix range table to see if area element exists.

   for(index=0; !found && index < no; index++)
   {
      if(this.area[index] == area)
         found = true;
   }

   return found;
}

Thanks! Any help is much appreciated.

1
  • 1
    this is a pointer. Use this->xxxxx instead or omit using it completely as most of the time it's superfluous. Commented Jun 20, 2014 at 4:35

1 Answer 1

1

The following errors:

GS1Prefix.cpp: In member function ‘bool Prefix::isRegistered(int) const’:
GS1Prefix.cpp:47:20: error: invalid types ‘int[int]’ for array subscript
GS1Prefix.cpp: In member function ‘bool Prefix::isRegistered(int, const char*)’:
GS1Prefix.cpp:68:15: error: request for member ‘area’ in ‘this’, which is of non-class type ‘Prefix* const’

and the errors in EAN.cpp are because you wrote this.. That's a mistake. this is a pointer, so you should use this-> to access members. The . is for accessing members via an object (not a pointer).

The others errors are pretty clearly explained in the error message:

GS1Prefix.cpp: In function ‘int minNoDigits(int)’:
GS1Prefix.cpp:98:35: error: invalid use of ‘this’ in non-member function
GS1Prefix.cpp:100:10: error: invalid use of ‘this’ in non-member function
GS1Prefix.cpp:103:18: error: invalid use of ‘this’ in non-member function

this is only defined in member functions. It is a pointer to the object on which the member function is called. Since minNoDigits is a free function, there is no such object.

Sign up to request clarification or add additional context in comments.

1 Comment

Ahhh, can't believe I missed that. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.