2017-02-11 2 views
-1

Wie greife ich auf eine pubic-Member-Funktion außerhalb von main zu? Wenn ich die Funktion direkt über den Punktoperator anrufe, wird "Scope error" angezeigt. Es besagt, dass der Objektname in diesem Bereich nicht deklariert wurde. Hier sind die Schnipsel meines Codes.Wie verwendet man ein Objekt außerhalb der Hauptfunktion in C++?

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; 
     } 
}; 

Innerhalb der externen Funktion (kein Mitglied Funktion) I als

den Funktionsaufruf gab
cout<<l[1].qt(); 

Es hält nur diesen Fehler knallen. Und ja, ich bin ein Newbie! Ich denke, es zeigt sich für mich von meinem Code. Danke im Voraus!

EDIT- Nicht sicher, welchen Teil Sie fragen. Hier ist es also. Mein ganzer Code:

#include <iostream> 
#include <cstring> 
#include <iomanip> 
//#include <string> 
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<<name<<endl<<"Processor-Intel Core i"<<cpu<<" ,RAM-"<<ram<<" GB, "<<gpu<<" GB Graphics, "<<size<<" inches HD display"<<endl; 
      cout<<"Price Rs."<<price<<endl; 
     } 
     void tab() 
     { 
      cout<<setw(1)<<id<<setw(22)<<name<<setw(17)<<"Intel Core i"<<cpu<<setw(7)<<ram<<" GB"<<setw(6)<<gpu<<" GB"<<setw(10)<<size<<" inches"; 
     } 
     int qt() 
     { 
      return qty; 
     } 
     void add() 
     { 
      cout<<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<<"Welcome to Walmart- The online electronic supermarket"<<endl; 
    cout<<"Walmart is a one-stop destination for all your digital needs"<<endl; 
    cout<<"Please enter your name"<<endl; 
    cin>>name; 
    cout<<"Welcome "<<name<<" to our brand new e-store"; 
    cout<<endl<<"We have laptops,tablets and desktops available in our store currently"<<endl; 
    cout<<"To go to the laptop store, press 1"<<endl<<"To go to the tablet store, press 2"<<endl<<"To go to the desktop store, press 3"; 
    cin>>ip1; 
    switch(ip1) 
    { 
    case 1: 
     cout<<"Press 1 for a tabulated view. Else press 2 for individual view"; 
     cin>>ip2; 
     switch(ip2) 
     { 
     case 1: 
      cout<<"Sl. no."<<setw(7)<<" Model Name"<<setw(20)<<"Processor"<<setw(13)<<"RAM"<<setw(14)<<"Graphics"<<setw(15)<<"Screen Size"<<endl; 
      for(i=1;i<13;i++) 
      { 
       l[i].tab(); 
       cout<<endl; 
      } 
      break; 
     case 2: 
      cout<<endl<<"Here's a list of all the models available with us"<<endl; 
      for(i=1;i<13;i++) 
      { 
      cout<<i<<") "; 
      l[i].disp(); 
      cout<<endl; 
      } 
      break; 
     } 
     cout<<"Are you interested in any of the above listed models? (Y/N)"<<endl; 
     cin>>ip3; 
     if(ip3=='N') 
     { 
      cout<<"Sorry!Please re visit the store soon for new models.Coming soon!"; 
      break; 
     } 
     id=get_id(); 
     cout<<"Please enter the quantity ("<<l[id].qt()<<" available in stock)"<<endl; 
     cin>>cart[count][1]; 
     if(cart[count][1]<=l[id].qt()) 
     cout<<"Item added to cart"; 
     else 
     cout<<"Error. Please recheck the quantity"; 
    } 

    return 0; 
} 

int get_id() 
{ 
    cout<<"Please enter the item id of the model that you are interested in"<<endl; 
    cin>>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; 
} 
+0

http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list –

+0

Bitte poste ein komplettes Beispiel. Zeigen Sie Ihre Haupt- und die andere Funktion an. Wir sollten in der Lage sein, den Code zu kompilieren und den genau gleichen Fehler zu erhalten. –

+0

Wahrscheinlich müssen Sie einen Parameter von der Hauptfunktion an die andere übergeben. –

Antwort

0

Als Ihr add Funktion ist eine Member-Funktion, die Sie nicht qt() von nennen müssen, wie Sie

void add() 
{ 
    cout<<l[2].qt();  
} 

Änderung es

void add() 
{ 
    cout<<qt();  
} 
+0

Also muss ich das Objekt als Argument übergeben? – Kathir

+0

können Sie das auch tun, und Sie können auch ein Objekt innerhalb Ihrer Nichtmitgliedsfunktion erstellen – Yousaf

+0

Könnten Sie bitte erarbeiten? Vielleicht ein Beispiel geben? Ich habe dich nicht ganz verstanden. Ich bin ein Lerner. – Kathir

0

haben Scope Error bedeutet, dass Ihre Deklaration an der falschen Stelle ist. Überprüfen Sie, wo Sie das Objekt deklariert haben.

+0

Es scheint keinen Fehler zu geben. Ich habe den ganzen Code hochgeladen. Erneut prüfen – Kathir

0

Variable l ist ausschließlich in der Funktion main, erklärt und somit ist es nicht „sichtbar“ außerhalb des Geltungsbereichs main.

Ihre Aussage cout<<l[2].qt() ist Teil einer Memberfunktion add der Klasse laptop und somit nicht in den Anwendungsbereich von main:

void add() 
    { 
     cout<<l[2].qt(); 
    } 

Sie wahrscheinlich bedeuten:

void add() 
    { 
     cout << this->qt(); 
    } 
+0

Kann ich den Scope-Resolution-Operator verwenden und ihn möglicherweise auch innerhalb dieser Funktion sichtbar machen? – Kathir

+0

Der Scope-Auflösungsoperator '::' dient zum Auflösen von Scope-Ambiguität, kann aber keine Variable "sichtbar" machen, wenn sie außerhalb des Gültigkeitsbereichs liegt. Sie müssten "l" eine globale Variable machen, um sie in den Bereich der Elementfunktion 'add' zu bringen. –

+0

Okay, dann gehen die Antworten. Es gibt 2 Wege, wie ich jetzt fortfahren kann 1) mache es zu einer Mitgliedsfunktion 2) überlasse das Objekt der Außenfunktion. Hab ich recht? – Kathir

Verwandte Themen