【问题标题】:C++ Vector Trouble. Computer lab administrator programC++ 矢量问题。计算机实验室管理员程序
【发布时间】: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


【解决方案1】:

要修复编译错误,请尝试以下操作:

在第 6 行注释掉您的 labs 的 C 数组声明,因为根据您使用的平台,这可能会导致重新定义错误。在 Windows 上,labs 的此定义与在 math.h 中找到的 labs(long _X) 的函数定义冲突,这是 Microsoft 提供的库头。

接下来,将station2的typedef改成如下:

typedef std::vector<int> station2;

这应该可以解决编译问题。

祝你程序的其余部分好运。

【讨论】:

    猜你喜欢
    • 2019-03-02
    • 2021-11-05
    • 1970-01-01
    • 1970-01-01
    • 2011-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-10
    相关资源
    最近更新 更多