编译器问题与队列程序和模板

时间:2013-05-22 05:36:26

标签: c++ compiler-warnings

我在编写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:错误:没有匹配的调用函数   队列:: pushAndPop(int)的

     

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

     

Queue.h:27:28:注意:TYPE Queue :: pushAndPop(TYPE)[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);
}

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

1 个答案:

答案 0 :(得分: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&进行,并且只能从。

中读取