【问题标题】:cannot convert char* to int*无法将 char* 转换为 int*
【发布时间】:2014-10-09 02:25:34
【问题描述】:

我想要做的是创建一个堆栈类。堆栈是 char 类型,但是在创建 char 数组时出现编译时错误。语法对我来说看起来不错,但是当我编译它时,我得到下面的编译时错误:

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;
    }

【问题讨论】:

  • 检查你的宏定义(最好还是使用 typedefs)。编译器认为TYPEint,而不是char。弄清楚你在哪里告诉它的。还有,为什么你有时用TYPE,有时却显式写char
  • 它编译对我来说没有错误。使用宏#define TYPE char 风格很差;是某些代码或命令行选项您没有向我们展示将TYPE 重新定义为int
  • template&lt;typename TYPE&gt;
  • 您的标题中有using namespace std,但甚至不使用标题中的任何内容。在标头中使用命名空间并不是一个好习惯,而是在适当的时候指定命名空间

标签: c++ compile-time


【解决方案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 是丑陋的

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-31
    • 2012-10-19
    • 2011-07-03
    • 2015-01-21
    • 2012-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多