#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

Recommended Answers

All 2 Replies

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;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.