cmo
cmo

Reputation: 66

c++ errors. conceptual problems with this pointer

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.

Upvotes: 0

Views: 164

Answers (1)

M.M
M.M

Reputation: 141544

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.

Upvotes: 1

Related Questions