2014-10-09 109 views
0

我想要做的是創建堆棧類。堆棧的類型是char,但是當我創建char數組時,我得到了編譯時錯誤。語法看起來好像沒什麼問題,但是當我編譯它,我得到下面的編譯時錯誤:無法將char *轉換爲int *

stack.cpp: In constructor ‘stack::stack(int)’: 
stack.cpp:17:14: error: cannot convert ‘char*’ to ‘int*’ in assignment 
     _arr = new char[_capacity]; 
      ^
stack.cpp: In copy constructor ‘stack::stack(const stack&)’: 
stack.cpp:24:14: error: cannot convert ‘char*’ to ‘int*’ in assignment 
     _arr = new char[_capacity]; 

我下面貼的標題和CPP文件:

#include <iostream> 
    #include <cstdlib> 
    #include <cassert> 
    using namespace std; 

    #define INITCAP 8 
    #define TYPE char 

    class FullStackException{}; 
    class EmptyStackException{}; 

    class stack{ 
    public: 
     // constructor with default capacity value 
     stack(int=INITCAP); 

     // copy constructor 
     stack(const stack&); 

     // assignment operator 
     stack& operator=(const stack&); 

     // destructor 
     ~stack(); 

     // push an element; 
     // if fixed sized stack, throw FullStackException when stack is full 
     void push(const TYPE x); 

     // remove and element; throw EmptyStackException when stack is empty 
     void pop(); 

     // return a reference to the top element without popping; throw EmptyStackException when stack is empty 
     TYPE& top(); 

     // return true if the stack is empty, false otherwise 
     bool empty(); 

     // return the number of elements currently on the stack 
     int size(); 

     // return the current capacity of the stack 
     int capacity(); 

    private: 
     TYPE * _arr;  // pointer to dynamic integer array 
     int _tos;  // index to top of stack 
     int _capacity; // current capacity 
    }; 





    #include "stack.h" 

    // constructor with default capacity value 
    stack::stack(int n) { 
      _capacity = INITCAP; 
      _arr = new char[_capacity]; 
      _tos = -1; 
    } 

    // copy constructor 
    stack::stack(const stack& s) { 
      _capacity = s._capacity; 
      _arr = new char[_capacity]; 

      for (int i = 0; i < s._tos + 1; i++) { 
        _arr[i] = s._arr[i]; 
      } 
      _tos = s._tos; 
    } 

    // assignment operator 
    stack& stack::operator=(const stack& s) { 
      _capacity = s._capacity; 

      for (int i = 0; i < s._tos + 1; i++) { 
        _arr[i] = s._arr[i]; 
      } 
      _tos = s._tos; 

      return *this; 
    } 

    // destructor 
    stack::~stack() { 
      delete[] _arr; 
    } 

    // push an element; 
    // if fixed sized stack, throw FullStackException when stack is full 
    void stack::push(const TYPE x) { 
      if (_tos + 1 == _capacity) { 
        _capacity *= 2; 
        int *temp = new int[_capacity]; 
        for (int i = 0; i < _capacity; i++) { 
          temp[i] = _arr[i]; 
        } 

        delete[] temp; 
      } 
      _tos++; 
      _arr[_tos] = x; 
    } 

    // remove and element; throw EmptyStackException when stack is empty 
    void stack::pop() { 
      if (_tos == -1) { 
        throw EmptyStackException(); 
      } 
      _tos--; 
    } 

    // return a reference to the top element without popping; throw EmptyStackException when stack is empty 
    TYPE& stack::top() { 
      if (_tos == -1) { 
        throw EmptyStackException(); 
      } 
      return _arr[_tos]; 
    } 

    // return true if the stack is empty, false otherwise 
    bool stack::empty() { 
      return (_tos == -1); 
    } 

    // return the number of elements currently on the stack 
    int stack::size() { 
      return _tos + 1; 
    } 

    // return the current capacity of the stack 
    int stack::capacity() { 
      return _capacity; 
    } 
+7

檢查您的宏定義(更好的是,使用typedef)。編譯器認爲'TYPE'是'int',而不是'char'。找出你告訴它的地方。另外,爲什麼有時候會使用'TYPE',而在其他時候顯式地寫'char'? – 2014-10-09 02:27:50

+1

它爲我編譯沒有錯誤。使用宏#define TYPE char是不好的風格;是一些代碼或命令行選項,你沒有顯示我們重新定義'TYPE'爲'int'? – 2014-10-09 02:30:00

+1

'template ' – 2014-10-09 03:07:15

回答

1

這看起來很奇怪

stack(int=INITCAP); 

您似乎完全忽略了您構造函數中的論點

stack::stack(int n) { 
     _capacity = INITCAP; 
     _arr = new char[_capacity]; 
     _tos = -1; 
} 

變化

stack(int n=INITCAP); 

stack::stack(int n) { 
    _capacity = n; 
    _arr = new TYPE[_capacity]; 
    _tos = -1; 
} 

甚至更​​好

stack::stack(int n) 
: _tos(-1), _capacity(n) 
{ 
    _arr = new TYPE[_capacity]; 
} 

注意,應該在他們的聲明相同的順序定義的成員變量。

,可能有點幫助,但你應該看看模板代替,使用的#define TYPE是醜陋