简体   繁体   中英

C++ stack implementation (homework)

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM