C++堆栈实现(作业)

发布于 2024-12-09 15:58:00 字数 1864 浏览 1 评论 0原文

已解决: 我一生都无法弄清楚为什么在尝试在这里初始化堆栈时出现错误:

#include "stack.h"
#include "linkList.h"


Stack::Stack() : m_top(0), m_size(0)
{
    m_stack = new List(); // cannot assign m_stack this way. How do i initialize here?
}

根据 Intellisense 的语法错误如下:

Error: a value of type List* cannot be assigned to an entity of type List*

堆栈类在这里:

#ifndef stack_H
#define stack_H

#include "linkList.h"


class Stack
{
public:

    //
    // Constructor to initialize stack data
    //
    Stack();

    //
    // functionality to determine if stack is empty
    //
    bool isEmpty();

    //
    // methods for pushing data on to stack and for
    // popping data from the stack
    //
    void push(Node* current, int newValue);
    void pop();

private:

    //
    // member data which represent the stack, the top
    // of the stack and the size of the stack
    //
    Node* m_top;
    List* m_stack;
    unsigned m_size;
};

#endif

我知道 linkList 类可以工作,因为我测试了它前。如果我想创建一个新列表,我所要做的就是:

List* myList = new List();

已解决:现在我遇到了一些令人恼火的链接器错误,我无法弄清楚为什么:

1>------ Build started: Project: Stack, Configuration: Debug Win32 ------
1>Build started 10/10/2011 4:50:24 PM.
1>InitializeBuildStatus:
1>  Touching "Debug\Stack.unsuccessfulbuild".
1>ClCompile:
1>  myStack.cpp
1>  linkList.cpp
1>  Generating Code...
1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
1>C:\Users\Dylan\documents\visual studio 2010\Projects\Stack\Debug\Stack.exe : fatal error        LNK1120: 1 unresolved externals
1>
1>Build FAILED.

为了确保我的堆栈头文件不与 STL 或其他文件冲突,我将其重命名为 myStack.h(是的,开始笑):

#ifndef myStack_H
#define myStack_H

SOLVED:
I cannot for the life of me figure out why I get an error when trying to initialize the stack here:

#include "stack.h"
#include "linkList.h"


Stack::Stack() : m_top(0), m_size(0)
{
    m_stack = new List(); // cannot assign m_stack this way. How do i initialize here?
}

The syntax error according to Intellisense is as follows:

Error: a value of type List* cannot be assigned to an entity of type List*

The stack class is here:

#ifndef stack_H
#define stack_H

#include "linkList.h"


class Stack
{
public:

    //
    // Constructor to initialize stack data
    //
    Stack();

    //
    // functionality to determine if stack is empty
    //
    bool isEmpty();

    //
    // methods for pushing data on to stack and for
    // popping data from the stack
    //
    void push(Node* current, int newValue);
    void pop();

private:

    //
    // member data which represent the stack, the top
    // of the stack and the size of the stack
    //
    Node* m_top;
    List* m_stack;
    unsigned m_size;
};

#endif

I know that the linkList class works because I tested it out before. If I wanted to create a new list, all I have to do is:

List* myList = new List();

SOLVED: Now I am getting some infuriating linker errors, and I cant figure out why:

1>------ Build started: Project: Stack, Configuration: Debug Win32 ------
1>Build started 10/10/2011 4:50:24 PM.
1>InitializeBuildStatus:
1>  Touching "Debug\Stack.unsuccessfulbuild".
1>ClCompile:
1>  myStack.cpp
1>  linkList.cpp
1>  Generating Code...
1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
1>C:\Users\Dylan\documents\visual studio 2010\Projects\Stack\Debug\Stack.exe : fatal error        LNK1120: 1 unresolved externals
1>
1>Build FAILED.

To make sure my stack header file was not in conflict with STL's or whatever, I renamed it myStack.h (yes, begin laughing):

#ifndef myStack_H
#define myStack_H

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野生奥特曼 2024-12-16 15:58:00

此错误:

1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup

通常在您的项目设置错误时发生。我猜您正在编写一个控制台应用程序,但您选择了除控制台应用程序之外的其他项目作为项目类型。

This error :

1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup

Usually happens when your project is wrongly setup. I guess that you are writing a console app but you have selected as type of project something else than a console app.

扛刀软妹 2024-12-16 15:58:00

此链接器错误

1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
1>C:\Users\Dylan\documents\visual studio 2010\Projects\Stack\Debug\Stack.exe : fatal error        LNK1120: 1 unresolved externals

意味着链接器找不到 main() 函数。您正在尝试创建一个可执行文件,因此您必须有一个 main()。

另外,您似乎已将原来的问题编辑为其他问题。这非常令人困惑,因为问题和答案/评论不再匹配。如果您遇到其他问题,请开始一个新问题。

This linker error

1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
1>C:\Users\Dylan\documents\visual studio 2010\Projects\Stack\Debug\Stack.exe : fatal error        LNK1120: 1 unresolved externals

means that the linker can't find a main() function. You're trying to make an executable, so you have to have a main().

Also, it appears you've edited your original question to now be something else. That's extremely confusing because the question and answers/comments no longer match. Start a new question if you run into another issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文