C ++ Visual Studio 2010,在实现动态堆栈时编译错误C3867 [英] C++ Visual Studio 2010, compile error C3867 when implementing dynamic stack

查看:508
本文介绍了C ++ Visual Studio 2010,在实现动态堆栈时编译错误C3867的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前正在使用具有动态内存分配的堆栈在大学中为我的数据结构类工作。目前我得到一个编译错误C3867说我缺少一个从参数列表的函数调用。我不是真正了解这个错误是从哪里来的/我有麻烦识别我的错误在我的代码是什么;所以我想知道,如果有人可能会很好,解释我是什么,也许一个友好的提示记住,所以我不能再发生这种情况。



此外,对于糟糕的格式化,我表示歉意,我从来没有张贴在这里,对不起,如果它很难阅读。





感谢您, b

头文件

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

struct Node
{
Node * nextPtr;
int价值;
};

类堆栈
{
public:
//构造函数和解构函数这里
Stack构造函数
〜Stack(); //默认解构函数

//逻辑方法||函数here
bool isEmpty(void); //原型检查栈是否为空

//栈操作||函数原型在这里
void push(int); //原型将int类型的值推送到栈上
int pop(); // prototype弹出值返回堆栈并返回值
int top(); //原型返回顶值
private:
Node * topPtr; //指向类Node对象的指针对于栈顶
};

#endif

类文件: / p>

  #includeCPTN278_A3_Stack_Arsenault.h

using namespace std;
Stack :: Stack()
{
topPtr = 0; //设置顶指针等于零。
}

Stack ::〜Stack()
{
while(!Stack :: isEmpty())
{
堆栈: :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; // **这是我的错误被抛出的地方**
}


方案

之前:

  bool Stack :: isEmpty()
{
if(top == 0)< - top是一个函数,你的意思是'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; 

我可能是边界式的讲道风格,但是尝试通过这种方式来理解表达式。涉及诸如==,!=,&&& ||,<>等逻辑运算符的表达式求值为true或false,因此不需要执行条件分支,只返回表达式最初评估的内容



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

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

解决方案

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.

这篇关于C ++ Visual Studio 2010,在实现动态堆栈时编译错误C3867的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆