变量或字段“commonStack”宣告无效
请帮帮我。我收到上述错误消息,但尚未解决。我已将头文件和 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您定义了commonStack,但从未在类中声明它(该声明已被注释掉)。另外,这不是使用标头保护的常用方法,通常是这样完成的:
将宏定义更改为不以下划线开头的内容,因为这些标识符是为实现保留的。
You define
commonStack
but never declare it in theclass
(the declaration is commented out). Also, that's not the usual way to use header guards, its usually done like this:Change the macro definition to something that does not start with an underscore, as those identifiers are reserved for the implementation.