2016-07-31 11 views
0

Ich möchte lernen, wie Klassenobjekte in der Klasse verwendet werden, insbesondere wie Argumente zwischen den Objekten übergeben werden. Ich versuche, ein Beispielobjekt Calc zu erstellen, das zwei Methoden Calc.sum und Calc.sub bereitstellen sollte. Es sollte ein anderes Objekt Plus als eine Mathe-Engine verwenden. Die Plus.add Funktion kompiliert und funktioniert gut. Aber ich weiß nicht, wie mehrere Instanzen von plus in der calc zu initiieren. Der Code:Klasse, die mehrere Instanzen einer anderen Klasse verwendet

class Plus{      // This class works well 
    public: 
     Plus(int);    // Structure 
     int add(int);   // Public method (function) 
     int myVar;    // Public property. Just to hold a value. 
    private: 
     int _init;    // Class-level private variable 
}; 

Plus::Plus(int init){   // Constructor 
    _init = init; 
} 

int Plus::add(int p){   // Method add 
    return _init + p; 
} 

/***************************************************************************/ 

class Calc{ 
    public: 
     Calc(int);    // Structure 
     int sum(int);   // Method sum 
     int sub(int);   // Method sub 
     int myVar;    // Public property 
    private: 
     Plus positive(int);  // Class-level private object definition ? 
     Plus negative(int);  // This is probably wrong ?? 
}; 

Calc::Calc(int init){   // Constructor (also wrong...) 
    Plus positive(init);  // Create object "positive" and pass the initial value 
    Plus negative(-init);  // Create object "negative" and pass the initial value 
} 

int Calc::sum(int n){ 
    return positive.add(n); 
} 

int Calc::sub(int n){ 
    return negative.add(n); 
} 

/***************************************************************************/ 

Plus two(2);  // Create class object two 
Calc five(5);  // Create class object five 

void setup(){ 
    Serial.begin(115200); 

    Serial.print("two.add(3) = "); 
    Serial.println(two.add(3));   // Calling instance of class Plus 
    two.myVar = 100; 
    Serial.println(two.myVar); 

    Serial.print("five.sum(3) = "); 
    Serial.println(five.sum(3));  // Calling instance of class Calc 

    Serial.print("five.sub(3) = "); 
    Serial.println(five.sub(3));  // Calling instance of class Calc 
} 

void loop(){} 

Mein Beispiel von diesem Artikel inspiriert: http://arduinoetcetera.blogspot.cz/2011/01/classes-within-classes-initialiser.html aber der Code ist für eine Instanz nur

1) Wie mehrere Instanzen von Plus innerhalb Calc

2) zu erklären ist die Terminologie (Kommentare) richtig?

Antwort

1

Ihr Problem liegt hier

Calc::Calc(int init){   // Constructor (also wrong...) 
    Plus positive(init);  // Create object "positive" and pass the initial value 
    Plus negative(-init);  // Create object "negative" and pass the initial value 
} 

Auf diese Weise sind Sie als Mitglieder lokale Variablen mit dem gleichen Namen zu erstellen: es hat keine Wirkung.

Sollte

Calc::Calc(int init): positive(init),negative(-init) 
{ 
} 

diese Weise können Sie Ihre Mitglieder initialisieren, auch wenn Ihre Mitglieder nicht über einen Standardkonstruktor verfügen.

Oh, und das ist auch falsch, in der Definition von Calc

Plus positive(int);  // Class-level private object definition ? 
    Plus negative(int);  // This is probably wrong ?? 

ersetzen durch

Plus positive;  // Class-level private object definition 
    Plus negative; 
+0

Es nicht kompiliert. Da muss etwas anderes nicht stimmen. Entschuldigung, ich habe den ganzen Tag lang nach Lösung gegoogelt und bin vielleicht müde. – Combinatix

+1

Ich fand einen weiteren Fehler durch "manuelle Kompilierung von Hand" :), siehe meine Bearbeitung. Beim nächsten Mal bitte Kompilierungsfehler. "Es kompiliert nicht" ist einfach nicht genug. –

Verwandte Themen