-1

我在编写的 C++ 队列中实现模板时遇到了一些麻烦。如果你们能帮我解决一些编译器问题,我会喜欢的!

这是我的编译器输出:

在 a7_main.cpp:3:0 中包含的文件中:Queue.h:在成员函数 TYPE Queue::pushAndPop(TYPE) 中:

Queue.h:33:25:警告:x 的名称查找已更改 [默认启用]

Queue.h:27:28: 警告:在 ISO 标准规则下匹配这个 x [默认启用]

Queue.h:30:11:警告:在旧规则下匹配这个 x [默认启用]

Queue.h:在全局范围内:

Queue.h:45:23:错误:无效使用没有参数列表的模板名称队列

Queue.h:在复制构造函数 Queue::Queue(Queue&) [with TYPE = V2, Queue = Queue]:

a7_main.cpp:56:19:从这里实例化

Queue.h:41:5: 错误:没有匹配函数调用 Queue::pushAndPop(int)

Queue.h:41:5: 注意:候选人是:

Queue.h:27:28:注意:TYPE Queue::pushAndPop(TYPE) [with TYPE = V2]

Queue.h:27:28:注意:没有已知的参数 1 从 int 到 V2 的转换

我试图理解这些警告和错误,但这让我陷入了困境。这是我的代码:

#if !defined QUEUE_SIZE
#define QUEUE_SIZE 30
#endif
using namespace std;

template <class TYPE> class Queue
{
 private:
  TYPE *array;
 public:
  Queue(Queue& other);
  Queue();
  ~Queue();
  Queue& operator=(Queue other);
  TYPE pushAndPop(TYPE x);
};

template <class TYPE> Queue<TYPE>::Queue()
{
  array=new TYPE[size];
}

template <class TYPE> Queue<TYPE>::~Queue()
{
  delete [] array;
}

template <class TYPE> TYPE Queue<TYPE>::pushAndPop(TYPE x)
{
  TYPE item = array[0];
  for(int x = 0; x<QUEUE_SIZE-1; x++){
    array[x]= array[x+1];
  }
  array[QUEUE_SIZE-1] = x;
  return item;
}

template <class TYPE> Queue<TYPE>:: Queue(Queue& other)
{
  int x;
  for(x=0; x<QUEUE_SIZE;x++){
    array[x] = other.pushAndPop(0);
  }
}

template <class TYPE> Queue& Queue<TYPE>:: operator=(Queue other)
{
  int x;
  for(x=0; x<QUEUE_SIZE;x++){
    array[x] = other.pushAndPop(0);
  }
}

编辑:这是调用代码:

#include <ncurses.h>
#include <unistd.h>
#include "Queue.h"

//----------------------------------------------------

#define START_ROW (20)
#define START_COL (20)
#define SLEEP_MICROSECONDS ((1.0 / 20.0) * 1000000.0)

//----------------------------------------------------

// Simple storage for a row, column pair.
struct V2 {
  int row, col;
};

//----------------------------------------------------

// Some of this ncurses code is, unfortunately, copied from
// the man page without full understanding.
void configureNcurses()
{
  initscr();
  cbreak();
  noecho();
  nonl();
  intrflush(stdscr, FALSE);
  keypad(stdscr, TRUE);
  nodelay(stdscr, TRUE);
  curs_set(0);
  start_color();
  init_pair(1, COLOR_RED, COLOR_BLACK);
  init_pair(2, COLOR_GREEN, COLOR_BLACK);
  init_pair(3, COLOR_YELLOW, COLOR_BLACK);
  init_pair(4, COLOR_BLUE, COLOR_BLACK);
  init_pair(5, COLOR_MAGENTA, COLOR_BLACK);
  init_pair(6, COLOR_CYAN, COLOR_BLACK);
  init_pair(7, COLOR_WHITE, COLOR_BLACK);
}

//----------------------------------------------------

// Draw centipede with sequence of foreground colors, or with
// background color, depending on "erase" flag.
// (Pass centipede by value to exercise copy constructor.)
void drawCentipede(Queue<V2> centipede, bool erase)
{
  int drawCharacter;
  int colorNumber;
  V2 currentPosition, dummyPosition;
  Queue<V2> centipedeCopy;

  // Make a copy of centipede to be consumed during drawing.
  // (Exercises assignment operator.)
  centipedeCopy = centipede;

  // Prepare to draw or erase, as requested.
  if (erase)
    drawCharacter = ' ';
  else
    drawCharacter = ' ' | A_REVERSE;

  // Consume centipede copy to obtain data for drawing.
  for (int i = 0; i < QUEUE_SIZE; ++i) {
    colorNumber = 1 + (i % 7);
    currentPosition = centipede.pushAndPop(dummyPosition);
    attron(COLOR_PAIR(colorNumber));
    mvaddch(currentPosition.row, currentPosition.col,
            drawCharacter);
    attroff(COLOR_PAIR(colorNumber));
  }
}

//----------------------------------------------------

// Update position based on arrow key input.
void updatePosition(V2& position, int inputChar)
{
    switch (inputChar) {
    case KEY_UP:
      --position.row;
      break;
    case KEY_DOWN:
      ++position.row;
      break;
    case KEY_LEFT:
      --position.col;
      break;
    case KEY_RIGHT:
      ++position.col;
      break;
    default:
      // (Ignore all other keys.)
      break;
    }
}

//----------------------------------------------------

main()
{
  Queue<V2> centipede;
  int currentDirection;
  int inputChar;
  V2 currentHead;

  // Configure ncurses.
  configureNcurses();

  // Fill queue (all centipede segments at start position).
  currentHead.row = START_ROW;
  currentHead.col = START_COL;
  for (int i = 0; i < QUEUE_SIZE; ++i)
    centipede.pushAndPop(currentHead);

  // Draw instructions and initial centipede.
  attron(COLOR_PAIR(2));
  mvaddstr(1, 3, "USE ARROW KEYS TO MOVE, CTRL-C TO QUIT");
  attroff(COLOR_PAIR(2));
  drawCentipede(centipede, false);
  refresh();

  // Process input until killed.
  currentDirection = KEY_RIGHT;
  while (true) {
    // Show current state, then check for input.
    usleep(SLEEP_MICROSECONDS);
    inputChar = getch();
    if (inputChar != ERR)
      currentDirection = inputChar;

    // When input received, erase old centipede.
    drawCentipede(centipede, true);

    // Then use new input to update centipede.
    updatePosition(currentHead, currentDirection);
    centipede.pushAndPop(currentHead);

    // Then draw new centipede, and refresh.
    drawCentipede(centipede, false);
    refresh();
  }

  // Clean up ncurses.  (Re-display cursor.)
  curs_set(1);
}

//----------------------------------------------------
4

1 回答 1

1
template <class TYPE> Queue<TYPE>::Queue()
{
  array=new TYPE[size];
}

是什么size

template <class TYPE> TYPE Queue<TYPE>::pushAndPop(TYPE x)
                                                   ^^^^^^
{
  ...
  for(int x = 0; ...) {
      ^^^^^

你在这里有相互矛盾的名字。这是个坏主意。以不同的方式命名参数或循环变量。

template <class TYPE> Queue<TYPE>:: Queue(Queue& other)
                                          ^^^^^^

这接受什么样的Queue?如果它只是Queue<TYPE>,则需要准确指定。如果它可以采用其他类型,您将需要另一个模板参数(但这在这里没有意义)。

template <class TYPE> Queue& Queue<TYPE>:: operator=(Queue other)
                      ^^^^^^                         ^^^^^

这里有同样的评论,加上参数看起来应该被(可能const)引用。

array[x] = other.pushAndPop(0);
                            ^

(两次。)除非TYPE是某种 ,否则int这是没有意义的。您没有pushAndPop需要int.

如果我正确理解您的代码,则复制构造函数和复制赋值运算符都会破坏它们的参数。这是非常令人惊讶的。这些论点应该被接受const&并且只能从中读取。

于 2013-05-22T06:13:46.810 回答