2012-03-01 140 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。