【问题标题】:Invalid use of non-static data member when declaring pointer声明指针时无效使用非静态数据成员
【发布时间】:2020-03-02 18:45:29
【问题描述】:

所以我知道还有很多其他类似的问题,但我一直在寻找大约 2 小时,但还没有找到对我有用的问题。在这段代码中,我尝试使用类模板创建一个简单的堆栈;但是,我不断收到错误消息:尝试声明指向我的数组的指针 (T*Arr) 时,非静态数据成员的使用无效。初始化我的 int ArrTop 时出现同样的错误,但不是我的 int ArrSize?最后,当我尝试从 .cpp 文件中调用 Top() 和 Empty() 时,我还收到错误无法调用没有对象的成员函数。任何帮助将不胜感激!

代码如下:

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
const int DEFAULTSIZE=100;


template <class T>
class Stack {
public:
    Stack();                    // Default Constructor, stack is size 100.
    Stack(const int size);      // Constructor, creates stack of size "size"
    Stack(const Stack<T> & item);// Copy constructor
    bool Full();                // Return true if the stack is full
    bool Empty();               // Return true if the stack is empty
    int Size();                 // Return the size of the stack
    T Top();                    // Returns the top element, does not pop it.
    bool Push (const T item);   // Put an item on the stack.
    bool Pop();                 // Pop an item off and display to std out
   friend ostream &operator <<(ostream & os, Stack<T> &s)
   {
        if(Empty())
                {
                cout<<"Stack underflow!";
                }
        else
                {
                cout<<"Element: "<<Top()<<"has been removed"<<endl;
                Arr[ArrTop--];
                }
   }

private:
     T *Arr;                        // The "stack"
     int ArrSize;                  // The number of elements the stack can hold
     int ArrTop;                  // Points to the first empty node
};

template <class T>
Stack<T>::Stack()
{
Arr = new T[DEFAULTSIZE];
ArrSize = DEFAULTSIZE;
ArrTop = -1;
}

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

template <class T>
Stack<T>::Stack(const int size)
{
Arr = new T[size];
ArrSize=size;
ArrTop = -1;
}

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

template <class T>
bool Stack<T>::Empty()
{
if(ArrTop==-1)
        return true;
}

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

template <class T>
int Stack<T>::Size()
{
return ArrTop+1;
}

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

template <class T>
bool Stack<T>::Full()
{
if(Size()==ArrSize)
        return true;

}
template <class T>
bool Stack<T>::Pop()
{
if(Empty())
        {
        cout<<"Stack underflow!";
        return false;
        }
cout<<"Element: "<<Top()<<"has been removed"<<endl;
Arr[ArrTop--];
return true;
}

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

template <class T>
T Stack<T>::Top()
{
if(!Empty())
        {
        return Arr[ArrTop];
        }
else
        {
        exit(EXIT_FAILURE);
        }
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

template <class T>
bool Stack<T>::Push(const T item)
{
if(Full())
        {
        cout<<"Stack overflow!";
        return false;
        }
else
        {
        cout<<"Element: "<<item<<"has been inserted!"<<endl;
        Arr[ArrTop++]=item;
        }
}

然后是我得到的错误:

In file included from main.cpp:2:0:
stack.cpp: In function ‘std::ostream& operator<<(std::ostream&, Stack<T>&)’:
stack.cpp:45:9: error: invalid use of non-static data member ‘Stack<T>::Arr’
      T *Arr;              // The "stack"
         ^
stack.cpp:40:3: error: from this location
   Arr[ArrTop--];
   ^
stack.cpp:47:10: error: invalid use of non-static data member ‘Stack<T>::ArrTop’
      int ArrTop;                  // Points to the first empty node
          ^
stack.cpp:40:7: error: from this location
   Arr[ArrTop--];
       ^
stack.cpp: In instantiation of ‘std::ostream& operator<<(std::ostream&, Stack<char>&)’:
main.cpp:29:16:   required from here
stack.cpp:33:11: error: cannot call member function ‘bool Stack<T>::Empty() [with T = char]’ without object
  if(Empty())
           ^
stack.cpp:39:26: error: cannot call member function ‘T Stack<T>::Top() [with T = char]’ without object
   cout<<"Element: "<<Top()<<"has been removed"<<endl;
                          ^
make: *** [main.o] Error 1

再次感谢你们,你们是最棒的!

【问题讨论】:

  • 提示:std::stack 已经存在。无需自己动手。
  • 我知道这是作业!
  • 不是问题,但输出运算符应该采用const 参考
  • 您的 operator&lt;&lt; 函数在语义上是错误的。它是流输出运算符,它应该将堆栈的内容打印到流os。而且它不是“pop”运算符。
  • 您是否真的想实现一个Stack::operator&lt;&lt; 来从堆栈中弹出一个元素?

标签: c++ c++11


【解决方案1】:

operator&lt;&lt; 函数不是成员函数,它没有声明函数EmptyTop,或变量ArrArrTop。您需要使用s 对象中的函数和变量。

例如

if (s.Empty()) { ... }

【讨论】:

  • 是的,就是这样!非常感谢!
  • 请注意,OP 没有使用运算符中的流。这可能会使错误静音,但它仍然不是正确的输出运算符
  • 如何做一个正确的输出操作符?还有究竟什么是输出操作符?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-12
  • 2023-03-12
  • 2013-10-14
相关资源
最近更新 更多