2016-04-17 5 views
0

Ich habe eine Struktur-Array verwenden, genannt Robot_parts [] für jede part_rect struct (PART_NUM, part_name, part_quantity, part_cost)Structure Arrays & Pointers

Und durch die Leere Anzeigefunktion, ich habe Robot_parts [] Array anzuzeigen ganz durch Zeiger, aber ich weiß nicht wie, und ich weiß nicht, wo man Robot_parts [] deklarieren und ob ich einen beliebigen Zahlenwert innerhalb der Klammern setzen muss.

Bisher habe ich:

#include <iostream> 
#include <string> 

using namespace std; 
void display(); 

struct part_rec 
{ 
    int part_num; 
    string part_name; 
    int part_quantity; 
    double part_cost; 
}; 

int main() 
{ 
    part_rec Robot_parts[ ] = { 
           {7789, "QTI", 4, 12.95}, 
           {1654, "bolt", 4, 0.34}, 
           {6931, "nut", 4, 0.25} 
                }; 
return 0; 
} 

void display() 
{ 
    cout<<Robot_parts[]<<endl<<endl; 
} 

Wenn ich auch noch ein paar andere Fehler gemacht, lassen Sie es mich wissen. Vielen Dank!

+0

'cout << Robot_parts [] << endl << endl; 'Dies wird nicht so funktionieren, wie Sie es erwarten, C++ hat keine Möglichkeit, ein Array direkt zu drucken. –

+0

Wie soll das funktionieren? 'Cout << Robot_parts [] << endl << endl;' ?? Und du nennst 'display()' nicht von 'main()' BTW. –

+0

Warum nicht einen C++ Container, z.B. 'vector', anstelle eines c-arrays? – 4386427

Antwort

0

Wie in einem Kommentar erwähnt, wäre es viel besser, einen C++ - Container wie einen std::vector oder zu verwenden.

Aber da Ihr Professor einen alten Stil-Array erfordert, könnten Sie wie der Code unten versuchen - die Kommentare zur Erklärung siehe:

#include <iostream> 
#include <string> 
#include <vector> 

using namespace std; 
struct part_rec 
{ 
    int part_num; 
    string part_name; 
    int part_quantity; 
    double part_cost; 
}; 

// You have to pass a pointer (to the array) and the size of the array 
// to the display function 
void display(part_rec* Robot_parts, int n); 


// Make a function so that you can "cout" your class directly using << 
// Note: Thanks to @BaumMitAugen who provided this comment and link: 
//  It makes use of the so called Operator Overloading - see: 
//  https://stackoverflow.com/questions/4421706/operator-overloading 
//  The link is also below the code section 
std::ostream &operator<<(std::ostream &os, part_rec const &m) 
{ 
    // Note - Only two members printed here - just add the rest your self 
    return os << m.part_num << " " << m.part_name; 
} 


int main() 
{ 
    part_rec Robot_parts[] { 
           {7789, "QTI", 4, 12.95}, 
           {1654, "bolt", 4, 0.34}, 
           {6931, "nut", 4, 0.25} 
          }; 
    display(Robot_parts, 3); 
    return 0; 
} 

void display(part_rec* Robot_parts, int n) 
{ 
    // Loop over all instances of your class in the array 
    for (int i = 0; i < n; ++i) 
    { 
     // Print your class 
     cout << Robot_parts[i] << endl; 
    } 
} 

Die von @BaumMitAugen empfohlene Link: Operator overloading

+1

Für die Anfänger, die diese Antwort lesen: Es nutzt das sogenannte [Operator Overloading] (https://stackoverflow.com/questions/4421706/operator-overloading) (empfohlen zu lesen). –

+0

Vielen Dank dafür! :) –