cbinder
cbinder

Reputation: 2506

Getting error while compiling a simple class program

I am getting the error while compiling the following simple C++ class program .

Error : 'ptr_code' undeclared (first use this function)

#include<iostream>

using namespace std;

class company
{
  public:

  int code;
  int *ptr_code;

  company(int i)
  {
          ++count;
          code=i ;
          ptr_code = &code; 
  } 

};

int main()
{
company c(10);
company *ptr_c = &c;
cout<<<<"\n";
cout<<"\nCompany codes : \n"<<ptr_c->*ptr_code<<"\n"<<ptr_c->code<<"\n";
system("pause");
return 0;
}

Please help me out in the same while the variable ptr_code is declared as integer pointer with public scope specifier , while the ptr_c->code is working fine. Thanks in advance.

Upvotes: 1

Views: 109

Answers (3)

P.P
P.P

Reputation: 121357

Change ptr_c->*ptr_code to *ptr_c->ptr_code

And

cout<<<<"\n";

to

cout<<"\n";

You are using a variable count in constructor company which is not part of the class, neither it's declared. So it's going to give you an error.

Upvotes: 3

AProgrammer
AProgrammer

Reputation: 52274

ptr_c->*ptr_code should be *ptr_c->ptr_code. ptr_c->*ptr_code would be valid if ptr_code was a variable of type pointer to company data member.

Upvotes: 3

keety
keety

Reputation: 17441

use *ptr_c->ptr_code instead of ptr_c->*ptr_code look into operator precedence

Upvotes: 1

Related Questions