0

Ok It's prolly sth really basic but I give up. I just wanted to check answer for a question: Is compilator gonna protest?

cell->unregister(); cell.erase(); 

So I wrote a simplest script in c++ to check:

/* * main.cpp * * Created on: Aug 7, 2014 * Author: luke */ #include <iostream> #include <cstdlib> using namespace std; class abs { int a,b; public: void unregister(){ a = 0; b = 0; } void erase(){ a = 2; b = 0; } abs(int c,int d); }; abs::abs(int c,int d){ a = c; b = d; } int main () { abs obj(4, 2); obj->unregister(); obj.erase(); } 

The errors I get are:

Description Resource Path Location Type statement cannot resolve address of overloaded function main.cpp /testing line 32 C/C++ Problem

Description Resource Path Location Type Method 'unregister' could not be resolved main.cpp /testing line 34 Semantic Error

Description Resource Path Location Type Method 'erase' could not be resolved main.cpp /testing line 35 Semantic Error

Description Resource Path Location Type make: * [main.o] Error 1 testing C/C++ Problem

Description Resource Path Location Type expected ‘;’ before ‘obj’ main.cpp /testing line 32 C/C++ Problem

Description Resource Path Location Type ‘obj’ was not declared in this scope main.cpp /testing line 34 C/C++ Problem

3
  • Why ->unregister() and not .unregister() ? The -> syntax is not valid here. Commented Aug 7, 2014 at 22:06
  • obj->unregister() is not correct because obj is not a pointer. You should replace with obj.unregister(). Is this the problem ? Commented Aug 7, 2014 at 22:08
  • yes, the compilator doth protest, and the errors you received are evidence of that. See it live on an online compiler for yet more evidence if you don't believe your machine. Commented Aug 7, 2014 at 22:16

7 Answers 7

2

You have two problems:

1) abs is a C standard function, you included it in your program when you #include <cstdlib>. Your class abs has a name conflicting with this function.

2) obj->unregister should be obj.unregister(), since obj is an object, not a pointer.


Solution:

  • Choose another name for your class abs
  • Or declare it in a namespace of yours.

Live demo

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

1 Comment

Yeah thanks mate ! I just typed random letters for class name, it didn't occur to me that it maybe reserved... Damn wasted quite a lot time on this one :P but I guess it's still a lesson for me :)
2

It should be obj.unregister(); as obj is an actual object not a pointer to an object.

Comments

2

Yes, the compiler should protest this line:

obj->unregister(); 

The '->' operator is used with pointers, and here it is not. It should be:

obj.unregister(); 

Comments

2

obj is not a pointer, so the -> operator won't work on it. Use . instead:

obj->unregister(); 

Comments

2

As you can see the compiler does protest. -> would not work here. obj->unregister(); is a shorthand for (*obj).unregister(); It works when the object is initialized on the heap, link this

abs1 * obj = new abs1(4, 2); obj->unregister(); // obj.unregister(); will not work obj->erase(); 

You initialized the object on stack; so -> will not work and only "." will work:

abs1 obj(4, 2); obj.unregister(); // obj->unregister(); will not work obj.erase(); 

Update: I updated the examples to correct the problem of abs using the same name as the system function, pointed out in other answers.

1 Comment

Yep, you are right. But the thing is my goal was to simply verify if obj->unregister(); will work or not. The problem was that I couldn't do it because I used the abs for class name, and couldn't initialize the object. So now that I changed class name to abc, it worked and I realized that the obj->unregister(); doesn't work :) so I have my answer . I know I could just google for it but I prefer to check things like this "manually", helps me remember better.
1

There is 2 problems in the main :

  1. access to a pointer
  2. abs is a C function then you need to indicate that it is a class (better to rename it)

This will fix both :

int main () { class abs obj(4, 2); obj.unregister(); obj.erase(); } 

Comments

1

The program has two errors. First of all it is a bad idea to name a class the same way as the name of a function. You included header <cstdlib> that contains the declaration of standard C function abs.This function name hides the class abs. To resolve the problem you have to use elaborated class name in statement

abs obj(4, 2); 

That is you have to write

class abs obj(4, 2); 

The second error is using member access operator -> instead of . That is you have to write

obj.unregister(); 

instead of

obj->unregister(); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.