C++ Visual Studio 2010, compile error C3867 when implementing dynamic stack

落爺英雄遲暮 提交于 2019-12-13 05:35:40

问题


Currently working on an assignment for my datastructures class in university using stacks with dynamic memory allocation. Currently I'm getting a compile error C3867 saying I'm missing a function call from an argument list. I'm not really understanding where this error is coming from / I'm having trouble identifying what exactly is my error in my code; so I was wondering if someone might be kind enough to explain to me what it is, and maybe a friendly tip to remember so I can not have this happen again.

also, I apologize for the poor formatting, I've never posted here before sorry if its hard to read. :(

code posted below.

Thanks, and regards. :P

Header File:

#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

Class File:

#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**
}

回答1:


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

top is a function, to check its result you need top(). But I think you should be testing topPtr instead.




回答2:


Before you had:

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

Fix:

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

That's your only build error. I'm not sure why you or the compiler thought it was in the top method. That's perfectly fine. Also there's no need to write:

if (expression_that_is_true_or_false)
    return true;
else
    return false;

Just do:

return expression_that_is_true_or_false;

I might be borderline preaching style here, but try to get used to understanding expressions this way. Expressions involving logical operators like ==, !=, &&, ||, <, >, etc. evaluate to true or false, so there is no need to do conditional branching only to then return what the expression originally evaluated to in the first place.

Oh and I realize this is homework, but check out std::stack later in your free time if you haven't already.




回答3:


In function Stack::isEmpty(), something wrong with top.

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

I think it should be as below:

  if(topPtr==0)
  ...


来源:https://stackoverflow.com/questions/9525815/c-visual-studio-2010-compile-error-c3867-when-implementing-dynamic-stack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!