0
#include<iostream>
using namespace std;



class item
{
  int itemno;
  int itemprice;

public :
  static int total;
  void getprice();
  void printprice();
  friend void calculate(item a);
};
int item :: total=0;
void item :: getprice()
{
  cout<<"Print item number \n";
  cin>>itemno;
  cout<<"Print item price \n";
  cin>>itemprice;
  total++;
}

void item :: printprice()
{
  cout<<"item "<<itemno<<" itemprice "<<itemprice<<endl;
  cout<<total<<"\n";
}


void calculate(item a)
{

  total+=a.itemprice;

}


int main()
{
  item array[5];
  int i;
  for(i=0;i<2;i++)
  {
    array[i].getprice();
    calculate(array[i]);
  }
  for(i=0;i<2;i++)
  {
    array[i].printprice();
  }

}

see the above programme show error cos i cannot use static variable total in friend function properly........ how can i do this

2
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by lookforlohith
0

The compiler doesn't know where to look for total, so it looks for local variables and then global variables. If you want to access the static member, you have to qualify it with the class it's declared in:

item::total += a.itemprice;

Alternatively you can use the object to get to it as well:

a.total += a.itemprice;
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.