变量或字段`commonStack'声明为void

时间:2011-10-27 04:13:31

标签: c++ class

请帮帮我。我收到上述错误消息但尚未解决。我在这里粘贴了头文件和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);
}

1 个答案:

答案 0 :(得分:1)

您定义commonStack但从未在class中声明它(声明已注释掉)。此外,这不是使用标题保护的常用方法,通常这样做:

#ifndef MY_HEADER_GUARD_H
#define MY_HEADER_GUARD_H

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

#endif

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