2013-05-22 55 views
-1

我在寫一個C++隊列中實現模板時遇到了一些麻煩。如果你們能幫我解決一些編譯器問題,我很樂意!帶隊列程序和模板的編譯器問題

這裏是我的編譯器輸出:

In file included from a7_main.cpp:3:0: Queue.h: In member function TYPE Queue::pushAndPop(TYPE):

Queue.h:33:25: warning: name lookup of x changed [enabled by default]

Queue.h:27:28: warning: matches this x under ISO standard rules [enabled by default]

Queue.h:30:11: warning: matches this x under old rules [enabled by default]

Queue.h: At global scope:

Queue.h:45:23: error: invalid use of template-name Queue without an argument list

Queue.h: In copy constructor Queue::Queue(Queue&) [with TYPE = V2, Queue = Queue]:

a7_main.cpp:56:19: instantiated from here

Queue.h:41:5: error: no matching function for call to Queue::pushAndPop(int)

Queue.h:41:5: note: candidate is:

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

Queue.h:27:28: note: no known conversion for argument 1 from int to 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); 
} 

//---------------------------------------------------- 
+0

pushAndPop使用x作爲參數名稱和循環變量名 - 根本不是一個好主意。另一個錯誤是指使用該模板看起來不正確。你需要顯示「調用」代碼。 – Mat

+0

剛添加它! – CCguy

+0

那麼,對不起,調用代碼是無關緊要的。沒有正確讀取錯誤信息。 – Mat

回答

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,這是沒有意義的。您沒有需要intpushAndPop過載。

如果我正確理解您的代碼,則複製構造函數和複製賦值操作符都會銷燬它們的參數。這非常令人驚訝。參數應該由const&拍攝,並且只能讀取。