【发布时间】:2016-08-25 17:51:45
【问题描述】:
该程序应该跟踪 4 个实验室内使用过的和空的计算机。每个实验室都有不同数量的计算机站,所以我不得不使用锯齿状数组,我不知道我是否正确实现了它们,因为这是我第一次使用指针和向量。当我尝试编译时,出现以下错误:
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 vector
station2.
标签: c++ pointers vector jagged-arrays