变量或字段“commonStack”宣告无效

发布于 2024-12-12 01:39:11 字数 2433 浏览 0 评论 0原文

请帮帮我。我收到上述错误消息,但尚未解决。我已将头文件和 commonStack 函数粘贴到此处。也许你会看到一些我错过的东西。我还收到“未终止的 ifndef”错误消息。从头文件中可以看到,我以endif结束了该类。所以我不确定我做错了什么。请帮忙。谢谢。

//Header file. 
//Class definition for the stack ADT

#ifndef _mystack_H
#include <ostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define _mystack_H


const unsigned int maxSize = 10;

class Stack
{
  public:
            Stack(); //constructor

            ~Stack(); //Destructor

            bool isEmptyStack();

            bool isFullStack();

            void pushStack(int newItem);

            void popStack(int item);

            void initializeStack();

            void fillStack(int numSize);

            void exchangeTopAndBottom(Stack &stk);

            void printStack(Stack &stk);

            int sumStack(Stack &stk);

            void OddElem(Stack &stk);

            //void commonStack(Stack &stk1, Stack &stk2);

            void intersectStack(Stack &stk1, Stack &stk2);

   private:
            int maxSize;  //variable to store the maximum stack size
            int stackTop; //variable to poit to the top of the stack
            Stack arrList;//pointer to the array that holds the stack 
                          //elements

};      

#endif



//commonStack finds the union of two stacks
void Stack::commonStack(Stack &stk1, Stack &stk2)
{
     Stack temp, temp2, tempStk, tempStk1, tempStk2, cStack;
     int elem, elem1, elem2, elem3;

     while (!stk1.isEmptyStack())
     {
       stk1.popStack(elem);
       cStack.pushStack(elem);
       tempStk1.pushStack(elem);
     }

 while (!stk2.isEmptyStack())
 {
       stk2.popStack(elem1);

       while (!tempStk1.isEmptyStack())
       {
             tempStk1.popStack(elem2);

             if (elem1 == elem2)
             {
                       temp.pushStack(elem2);
                       tempStk2.pushStack(elem2);
             }
             else
             {
                 temp2.pushStack(elem2);
                 tempStk2.pushStack(elem2);
             }
       }

       while (!tempStk2.isEmptyStack())
       {
             tempStk2.popStack(elem2);
             tempStk1.pushStack(elem);
       }

       tempStk.pushStack(elem1);
}

 while (!cStack.isEmptyStack())
 {
       temp2.popStack(elem3);
       cStack.pushStack(elem3);
 }

 printStack(cStack);
}

Please help me out. I am getting the above error message but haven't been able to resolve it yet. I have pasted the header file and the commonStack function here. Maybe you'll see something I missed. I'm also getting an "unterminated ifndef" error message. As you can see from the header file, I ended the class with endif. So I'm not sure of what I'm doing wrong. Please help. Thanks.

//Header file. 
//Class definition for the stack ADT

#ifndef _mystack_H
#include <ostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define _mystack_H


const unsigned int maxSize = 10;

class Stack
{
  public:
            Stack(); //constructor

            ~Stack(); //Destructor

            bool isEmptyStack();

            bool isFullStack();

            void pushStack(int newItem);

            void popStack(int item);

            void initializeStack();

            void fillStack(int numSize);

            void exchangeTopAndBottom(Stack &stk);

            void printStack(Stack &stk);

            int sumStack(Stack &stk);

            void OddElem(Stack &stk);

            //void commonStack(Stack &stk1, Stack &stk2);

            void intersectStack(Stack &stk1, Stack &stk2);

   private:
            int maxSize;  //variable to store the maximum stack size
            int stackTop; //variable to poit to the top of the stack
            Stack arrList;//pointer to the array that holds the stack 
                          //elements

};      

#endif



//commonStack finds the union of two stacks
void Stack::commonStack(Stack &stk1, Stack &stk2)
{
     Stack temp, temp2, tempStk, tempStk1, tempStk2, cStack;
     int elem, elem1, elem2, elem3;

     while (!stk1.isEmptyStack())
     {
       stk1.popStack(elem);
       cStack.pushStack(elem);
       tempStk1.pushStack(elem);
     }

 while (!stk2.isEmptyStack())
 {
       stk2.popStack(elem1);

       while (!tempStk1.isEmptyStack())
       {
             tempStk1.popStack(elem2);

             if (elem1 == elem2)
             {
                       temp.pushStack(elem2);
                       tempStk2.pushStack(elem2);
             }
             else
             {
                 temp2.pushStack(elem2);
                 tempStk2.pushStack(elem2);
             }
       }

       while (!tempStk2.isEmptyStack())
       {
             tempStk2.popStack(elem2);
             tempStk1.pushStack(elem);
       }

       tempStk.pushStack(elem1);
}

 while (!cStack.isEmptyStack())
 {
       temp2.popStack(elem3);
       cStack.pushStack(elem3);
 }

 printStack(cStack);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

转角预定愛 2024-12-19 01:39:11

您定义了commonStack,但从未在类中声明它(该声明已被注释掉)。另外,这不是使用标头保护的常用方法,通常是这样完成的:

#ifndef MY_HEADER_GUARD_H
#define MY_HEADER_GUARD_H

... code for the header file here, ALL code ...

#endif

将宏定义更改为不以下划线开头的内容,因为这些标识符是为实现保留的。

You define commonStack but never declare it in the class (the declaration is commented out). Also, that's not the usual way to use header guards, its usually done like this:

#ifndef MY_HEADER_GUARD_H
#define MY_HEADER_GUARD_H

... code for the header file here, ALL code ...

#endif

Change the macro definition to something that does not start with an underscore, as those identifiers are reserved for the implementation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文