title: How to use an object outside the main function in C++?
body: <p>How do I access a pubic member function from outside of main? When I directly call the function using the 'dot operator' it shows me "Scope error". It says that the object name was not declared under that scope. Here are the snippets of my code.</p>
  
  <pre><code>class laptop
  {
  private:
          int id,price,cpu,gpu,ram,size,qty;
          char name[30];
      public: 
          laptop() : id(0),price(0),cpu(0),gpu(0),ram(0),size(0) {}
          void add(int i,char n[],int p,int c,int r,int g,int s,int q)
          {
              id=i; std::strcpy(name,n); price=p; cpu=c; ram=r; gpu=g; size=s; qty=q;
          }
  int qt()
          {
              return qty;
          }
  };
  </code></pre>
  
  <p>Inside the external function(not a member function) I gave the function call as</p>
  
  <pre><code>cout&lt;&lt;l[1].qt();
  </code></pre>
  
  <p>It just keeps popping that error. And yes, I AM A NEWBIE! I guess it shows for itself from my code. Thanks in advance!</p>
  
  <p>EDIT- Not sure which part you are asking for. So here it is. My entire code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstring&gt;
  #include &lt;iomanip&gt;
  //#include &lt;string&gt;
  using namespace std;
  char name[30],ip3;
  int ip1,ip2,i,cart[10][2],temp,id;
  static int count=0;
  
  int get_id();
  
  class laptop
  {
      private:
          int id,price,cpu,gpu,ram,size,qty;
          char name[30];
      public: 
          laptop() : id(0),price(0),cpu(0),gpu(0),ram(0),size(0) {}
          void add(int i,char n[],int p,int c,int r,int g,int s,int q)
          {
              id=i; std::strcpy(name,n); price=p; cpu=c; ram=r; gpu=g; size=s; qty=q;
          }
          void disp()
          {
              cout&lt;&lt;name&lt;&lt;endl&lt;&lt;"Processor-Intel Core i"&lt;&lt;cpu&lt;&lt;" ,RAM-"&lt;&lt;ram&lt;&lt;" GB, "&lt;&lt;gpu&lt;&lt;" GB Graphics, "&lt;&lt;size&lt;&lt;" inches HD display"&lt;&lt;endl;
              cout&lt;&lt;"Price Rs."&lt;&lt;price&lt;&lt;endl;
          }
          void tab()
          {
              cout&lt;&lt;setw(1)&lt;&lt;id&lt;&lt;setw(22)&lt;&lt;name&lt;&lt;setw(17)&lt;&lt;"Intel Core i"&lt;&lt;cpu&lt;&lt;setw(7)&lt;&lt;ram&lt;&lt;" GB"&lt;&lt;setw(6)&lt;&lt;gpu&lt;&lt;" GB"&lt;&lt;setw(10)&lt;&lt;size&lt;&lt;" inches";
          }
          int qt()
          {
              return qty;
          }
          void add()
          {
              cout&lt;&lt;l[2].qt();
          }
  
  };
  /* run this program using the console pauser or add your own getch, system("pause") or input loop */
  
  int main() 
  {
      laptop l[15];
      l[1].add(1001,"HP Pavillion 530",30000,5,4,1,15,10);
      l[2].add(1002,"HP Pavillion 540",32000,5,6,1,15,11);
      l[3].add(1003,"HP Pavillion 730",37000,7,8,2,15,4);
      l[4].add(1004,"HP Pavillion 750",44000,7,16,2,17,10);
      l[5].add(1005,"HP Pavillion 750+",49000,7,32,4,17,14);
      l[6].add(1006,"Dell Vostro DV300",23000,3,4,1,13,2);
      l[7].add(1007,"Dell Vostro DV301",25000,3,4,1,14,9);
      l[8].add(1008,"Dell Vostro DV510",29000,5,4,1,15,18);
      l[9].add(1009,"Dell Vostro DV710",44000,7,8,2,15,4);
      l[10].add(1010,"Dell Vostro DV750",48000,7,16,2,17,3);
      l[11].add(1011,"Lenovo Y300",27000,3,8,1,13,9);
      l[12].add(1012,"Lenovo Y900",60000,7,16,4,17,2);
      cout&lt;&lt;"Welcome to Walmart- The online electronic supermarket"&lt;&lt;endl;
      cout&lt;&lt;"Walmart is a one-stop destination for all your digital needs"&lt;&lt;endl;
      cout&lt;&lt;"Please enter your name"&lt;&lt;endl;
      cin&gt;&gt;name;
      cout&lt;&lt;"Welcome "&lt;&lt;name&lt;&lt;" to our brand new e-store";
      cout&lt;&lt;endl&lt;&lt;"We have laptops,tablets and desktops available in our store currently"&lt;&lt;endl;
      cout&lt;&lt;"To go to the laptop store, press 1"&lt;&lt;endl&lt;&lt;"To go to the tablet store, press 2"&lt;&lt;endl&lt;&lt;"To go to the desktop store, press 3";
      cin&gt;&gt;ip1;
      switch(ip1)
      {
      case 1:
          cout&lt;&lt;"Press 1 for a tabulated view. Else press 2 for individual view";
          cin&gt;&gt;ip2;
          switch(ip2)
          {
          case 1:
              cout&lt;&lt;"Sl. no."&lt;&lt;setw(7)&lt;&lt;" Model Name"&lt;&lt;setw(20)&lt;&lt;"Processor"&lt;&lt;setw(13)&lt;&lt;"RAM"&lt;&lt;setw(14)&lt;&lt;"Graphics"&lt;&lt;setw(15)&lt;&lt;"Screen Size"&lt;&lt;endl;
              for(i=1;i&lt;13;i++)
              {
                  l[i].tab();
                  cout&lt;&lt;endl;
              }
              break;
          case 2:
              cout&lt;&lt;endl&lt;&lt;"Here's a list of all the models available with us"&lt;&lt;endl;
              for(i=1;i&lt;13;i++)
              {
              cout&lt;&lt;i&lt;&lt;") ";
              l[i].disp();
              cout&lt;&lt;endl;
              }
              break;
          }
          cout&lt;&lt;"Are you interested in any of the above listed models? (Y/N)"&lt;&lt;endl;
          cin&gt;&gt;ip3;
          if(ip3=='N')
          {
              cout&lt;&lt;"Sorry!Please re visit the store soon for new models.Coming soon!";
              break;
          }
          id=get_id();
          cout&lt;&lt;"Please enter the quantity ("&lt;&lt;l[id].qt()&lt;&lt;" available in stock)"&lt;&lt;endl;
          cin&gt;&gt;cart[count][1];
          if(cart[count][1]&lt;=l[id].qt())
          cout&lt;&lt;"Item added to cart";
          else
          cout&lt;&lt;"Error. Please recheck the quantity";
      }
  
      return 0;
  }
  
  int get_id()
  {
      cout&lt;&lt;"Please enter the item id of the model that you are interested in"&lt;&lt;endl;
      cin&gt;&gt;cart[count][0];
      temp=(cart[count][0])/1000;
      if(temp==1)
      return (cart[count][0])-1000;
      else if(temp==2)
      return (cart[count][0])-2000;
  }
  </code></pre>