2012-03-01 129 views
0

目前正在使用具有动态内存分配的堆栈为大学中的数据结构类分配工作。目前,我得到一个编译错误C3867,说我缺少参数列表中的函数调用。我并不真正了解这个错误来自哪里/我无法确定我的代码中究竟发生了什么错误;所以我想知道是否有人可能会友善地向我解释它是什么,也许是一个友好的提示要记住,所以我不能再发生这种情况。C++ Visual Studio 2010,实现动态堆栈时编译错误C3867

另外,我对可怜的格式化表示歉意,如果它很难阅读,我从未在此发布过。 :(

代码下面贴

谢谢,和问候:第

头文件:

#ifndef STACK_H 
#define STACK_H 
#include <iostream> 
#include <iomanip> 

struct Node 
{ 
    Node *nextPtr; 
    int value; 
}; 

class Stack 
{ 
public: 
    //Constructors and Deconstructers Here 
    Stack(); //Default Constructor 
    ~Stack(); //Default Deconstructor 

    //logical methods || functions here 
    bool isEmpty(void); //prototype checks if stack is empty 

    //stack operations || function prototypes here 
    void push(int); //prototype to push values of type int onto the stack 
    int pop(); //prototype to pop values off of the stack and return a value 
    int top(); //prototype to return the top value 
private: 
    Node *topPtr; //pointer to class Node Object, specifically for the top of the stack 
}; 

#endif 

类文件:

#include "CPTN278_A3_Stack_Arsenault.h" 

using namespace std; 
Stack::Stack() 
{ 
    topPtr = 0; //set the top pointer equal to zero. 
} 

Stack::~Stack() 
{ 
    while (!Stack::isEmpty()) 
    { 
     Stack::pop(); 
    } 
} 

bool Stack::isEmpty() 
{ 
    if(top == 0) 
    { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 

void Stack::push(int valueTMP) 
{ 
    Node *itemPtr = new Node; 
    itemPtr->nextPtr = topPtr; 
    itemPtr->value = valueTMP; 
    topPtr = itemPtr; 
    return; 
} 

int Stack::pop() 
{ 
    int returnValue; //unintialized int 
    Node *itemPtr; //unintialized pointer to node 
    returnValue = topPtr->value; 
    itemPtr = topPtr; 
    topPtr = itemPtr->nextPtr; 
    delete itemPtr; 
    return returnValue; 
} 

int Stack::top(void) 
{ 
    return topPtr->value; //**this is where my error is being thrown** 
} 
+0

您是否检查http://msdn.microsoft.com/en-us/library/b0x1aatf(v=vs.100).aspx? – Pete 2012-03-01 23:35:03

+1

如果显示完整的错误/警告消息,这会很有帮助。 – kamae 2012-03-01 23:35:15

回答

2
bool Stack::isEmpty() 
{ 
    if(top == 0) // <-- here is the problem 
    { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 

top是一个函数,检查你需要的结果top()。但我认为你应该测试topPtr

+0

谢谢!哈哈,我完全错过了这一点。 – capnfoo 2012-03-01 23:38:21

0

在功能Stack::isEmpty()top有问题。

bool Stack::isEmpty() 
{ 
    if(top == 0) // here 
    { 
    return true; 
    } 
    else 
    { 
    return false; 
    } 
} 

我觉得应该是如下:

if(topPtr==0) 
    ... 
2

你以前:

bool Stack::isEmpty() 
{ 
    if(top == 0) <-- top is a function, did you mean 'topPtr'? 
    { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 

修复:

bool Stack::isEmpty() 
{ 
    return topPtr == 0; 
} 

这是你唯一的建立自己的错误。我不确定为什么你或者编译器认为它是最好的方法。这很好。也没有必要写:

if (expression_that_is_true_or_false) 
    return true; 
else 
    return false; 

只要做到:

return expression_that_is_true_or_false; 

我可能在这里是交界的说教式的,而是要设法习惯理解表达这种方式。涉及诸如==,!=,&,&,||,<,等等的逻辑运算符的表达式计算为真或假,因此不需要进行条件分支,只需要返回表达式最初评估到的第一名。

哦,我意识到这是家庭作业,但如果你还没有空闲时间,请在空闲时间检查std :: stack。