2016-04-30 7 views
0

Dieses Programm soll in 4 Labors den Überblick über gebrauchte und leere Computer behalten. Jedes Labor hat eine andere Anzahl von Computerstationen, daher musste ich gezackte Arrays verwenden, ich weiß nicht, ob ich sie richtig implementiert habe, da ich zum ersten Mal mit Zeigern und Vektoren arbeite. Wenn ich versuche zu kompilieren, bekomme ich folgende Fehler:C++ Vektorfehler. Computer-Labor-Administrator-Programm

labadmin.cpp: In function ‘int main()’: 
labadmin.cpp:56:35: error: cannot convert ‘std::vector<int>’ to ‘station2 {aka int*}’ in initialization 
     station2 store = labs[lab - 1]; 
           ^
labadmin.cpp:64:27: error: cannot convert ‘std::vector<int>’ to ‘station2 {aka int*}’ in initialization 
    station2 store = labs[x]; 
         ^
labadmin.cpp:89:30: error: cannot convert ‘std::vector<int>’ to ‘station2 {aka int*}’ in initialization 
     station2 store = labs[x]; 

Also was fehlt mir?

#include <iostream> 
#include <vector> 
using namespace std; 

const int AVAILABLE_LABS = 4; 
int *labs[AVAILABLE_LABS]; 
int capacity[AVAILABLE_LABS]; 
typedef int *station2; 

int main() 
{ 

    vector <int> labs[AVAILABLE_LABS]; 
    labs[0].push_back(1); labs[0].push_back(2); labs[0].push_back(3); 
    labs[0].push_back(4); labs[0].push_back(5); 

    labs[1].push_back(1); labs[1].push_back(2); labs[1].push_back(3); 
    labs[1].push_back(4); labs[1].push_back(5); labs[1].push_back(6); 

    labs[2].push_back(1); labs[2].push_back(2); labs[2].push_back(3); 
    labs[2].push_back(4); 

    labs[3].push_back(1); labs[3].push_back(2); labs[3].push_back(3); 

    int choice; 
    cout << "Choose a number according to the option you would like to execute.\n"; 
    cout << "0: Exit\n" << endl; 
    cout << "1: Log in/Log off\n" << endl; 
    cout << "2: Search\n" << endl; 
    cin >> choice; 

    int id, id2, lab, station; 

    switch(choice){ 
    case 0: 
    cout << "Goodbye.\n"; 
    break; 
    case 1: 
    cout << "LOG IN/LOG OFF\n"; 
    cout << "ID Number (if you wish to log off, enter 0)\n"; 
    cin >> id; 
    cout << "Enter lab number:\n"; 
    cin >> lab; 
    cout << "Enter computer station number:\n"; 
    cin >> station; 
    break; 
    case 2: 
    cout << "SEARCH\n"; 
    cout << "User id:\n"; 
    cout << id2; 
     break; 
    } 

    if (choice == 1) 
    { 
    station2 store = labs[lab - 1]; 
     store[station - 1] = id; 
    } 
    else if (choice == 2) 
    { 

     for(int x = 0; x < AVAILABLE_LABS; x++) 
    { 
     station2 store = labs[x]; 
     for(int y= 0; y < capacity[x]; y ++) 
     { 
      if(store[y] == id) 
     { 
      lab = x + 1; 
      station = y + 1; 
     } 
     } 
    } 
     if(lab != 0 && station != 0) 
    { 
     cout << "ID: " << id 
      << "At station: " << station 
      << "At lab: " << lab << endl; 
    } 
     else 
    { 
     cout << "No user logged in.\n"; 
    } 
    } 

    for(int x = 0; x < AVAILABLE_LABS; x++) 
    { 
     cout << "Lab " << x + 1 << ": "; 
     station2 store = labs[x]; 

     for(int y = 0; y < capacity[x]; y++) 
    { 
     cout << "Station" << y + 1 << ": "; 
     if(store[y] == 9) 
     { 
      cout << "EMPTY "; 
     } 
     else { 
     cout << store[y] << " "; 
     } 
    } 
     cout << endl; 
    } 

    return 0; 
} 

Vielen Dank im Voraus.

+1

Versuchen Sie, typedef int * station2 für typedef vector station2 zu ersetzen. – perencia

Antwort

0

die Kompilierungsfehler zu beheben, versuchen Sie Folgendes:

Kommentieren Sie Ihre C-Array-Deklaration von labs auf der Leitung 6, da, je nachdem, welche Plattform Sie sind, diese in einer Neudefinition Fehler führen kann. Unter Windows steht diese Definition von labs in Konflikt mit der Funktionsdefinition von labs(long _X), die in math.h zu finden ist. Dabei handelt es sich um einen von Microsoft bereitgestellten Bibliothekskopf.

Als nächstes ändern Sie die typedef von station2 auf die folgenden:

typedef std::vector<int> station2; 

, dass die Kompilierung Probleme beheben sollte.

Viel Glück mit dem Rest des Programms.