0
このプログラムは、4つのラボ内の使用済みおよび空のコンピュータを追跡することになっています。各ラボにはさまざまなコンピュータステーションがありますので、ギザギザの配列を使用しなければなりませんでした。ポインターやベクトルで作業するのは初めてのので、正しく実装したかどうかはわかりません。私がコンパイルしようとすると、私は次のエラーを取得:C++ベクタの問題。コンピュータラボ管理者プログラム
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];
だから私は何をしないのですか?
#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;
}
ありがとうございます。コンパイルエラーを修正するには
typedef int * station2をtypedefベクター station2に置き換えてみます。 –
perencia