2011-10-27 29 views
-1

请帮我一把。我收到上述错误消息,但尚未能够解决它。我在这里粘贴了头文件和commonStack函数。也许你会看到我错过的东西。我也收到“未终止的ifndef”错误消息。正如你从头文件中看到的那样,我用endif结束了这个类。所以我不确定我做错了什么。请帮忙。谢谢。变量或字段'commonStack'声明为空

//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); 
} 
+0

'commonStack'被注释掉,并且在您发布的代码中没有'#endif'。 – curiousguy

+0

@curiousguy:是的,只是不在文件的末尾。 –

+0

我看到'#endif'。但是'commonStack'的声明在哪里? – curiousguy

回答

1

您定义commonStack但在class从来没有宣布它(声明被注释掉)。而且,这不是用头警卫通常的方式,它通常做这样的:

#ifndef MY_HEADER_GUARD_H 
#define MY_HEADER_GUARD_H 

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

#endif 

更改宏定义的东西,不以下划线开头,因为这些名称均用于实施。

+0

作为K-假面舞会指出: (1)commonStack在类注释掉 (2)你有警卫关闭(虽然我不知道,如果在宏观领先的下划线是一个大问题,我知道这是对符号由于连接器的行为 成员函数的另外commonStack哈希语义...但是,这没有任何意义。 如果commonStack是一个成员,它要么被定义为一个独立的功能以及(堆栈也许有朋友)或也许是比较自我的另一个ASA成员函数。 – nhed

+0

我忘了取消注释commonStack。我评论它,当我开始收到错误消息。你说的堆栈的立场-A-孤独的功能或朋友的意思吗?你能给我是一个例子吗?这是我第一堂课,所以我不熟悉所有的术语。谢谢 – ck22