-2
#include <iostream>
#include <string> 
#include <sstream>
using namespace std;
class Bird{
  public:
    int A;
    Bird(int Y){A = Y;}
    int retrieve(){return A;}
} ;
int main(){
Bird * C  =new Bird(6);
cout<< C.retrieve()<<endl;
return 0;
}

I can't access the object's retrieve() method which is pointed by C pointer(the object is pointed by C). Is there any way to do this . Please let me know.I use vscode V1.29.1

  • 2
    Instead of `C.retrieve()`, try `C->retrieve()`. – Blaze Nov 20 '18 at 15:09
  • @Blaze It didn't work out :( – Keshan akalanka Nov 20 '18 at 15:11
  • 2
    @Nipunachandimal "_It didn't work out_" Why? Did you receive an error? What error was it? – Algirdas Preidžius Nov 20 '18 at 15:12
  • @AlgirdasPreidžius I'm using vscode. It showed me this.g++: error: pointer: No such file or directory g++: error: _error.cpp: No such file or directory g++: error: _error: No such file or directory [Done] exited with code=1 in 0.062 seconds – Keshan akalanka Nov 20 '18 at 15:13
  • Sure seems to work out [here](http://coliru.stacked-crooked.com/a/ef5c53e2131ebd1d). You seem to just have a configuration problem. – StoryTeller - Unslander Monica Nov 20 '18 at 15:13
  • 1
    This is ridiculous. This is something that will be covered in an introductory text. Trying to learn C++ by asking questions about each language feature in turn won't be fun for anyone. – Useless Nov 20 '18 at 15:13
  • c++ is difficult to self-teach. You should read [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to get you started. It's not practical to learn c++ by trial-and-error or by asking questions about the basic topics. – François Andrieux Nov 20 '18 at 15:14

1 Answers1

2

You are instantiating an Bird instance on the heap and store a pointer to that object in a variable named C. Pointers must be dereferenced before accessing data members or member functions, i.e.

std::cout << C->retrieve() << "\n";

// or, as @PeteBecker has pointed out in the comments
std::cout << (*C).retrieve() << "\n";

Also, don't forget to

delete C;

or even better: use the <memory> header and std::make_unique, which frees you from the necessity to manually clean up the pointer.

#include <memory>

auto C = std::make_unique<Bird>(6);

std::cout<< C->retrieve() << "\n";
lubgr
  • 37,368
  • 3
  • 66
  • 117
  • 1
    Re: "store a reference to that object in a pointer" -- **reference** has a specific meaning in C++, and this is not it. The code stores the **address** of that object in the pointer. +1. – Pete Becker Nov 20 '18 at 15:14
  • 1
    And just for completeness, `(*C).retrieve()` also works. `->` is much clearer. – Pete Becker Nov 20 '18 at 15:15
  • This didn't work for me . ERROR IS " g++: error: pointer: No such file or directory g++: error: _error.cpp: No such file or directory g++: error: _error: No such file or directory" – Keshan akalanka Nov 20 '18 at 15:16
  • @Nipunachandimal It sounds like your problem has nothing to do with your code. Your project file or environment is not properly configured. – François Andrieux Nov 20 '18 at 15:16
  • 1
    @PeteBecker Thanks for the suggestion, I fixed it. – lubgr Nov 20 '18 at 15:18
  • @FrançoisAndrieux you are correct . When I compile it using codeblocks the error gone away . Thanks for your kind full support !! – Keshan akalanka Nov 20 '18 at 15:20