3

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
dtg
  • 1,803
  • 4
  • 30
  • 44
  • 5
    I fail to see anything fundamentally wrong. Add the actual error message you get. – K-ballo Oct 10 '11 at 23:22
  • 2
    What about doing `m_stack = new List;` ? – karlphillip Oct 10 '11 at 23:25
  • 6
    What about just `List m_stack;` instead of allocating the List object dynamically at all? – Jerry Coffin Oct 10 '11 at 23:27
  • karlphillip: tried doing that, got the same error – dtg Oct 10 '11 at 23:28
  • 6
    Disregard what Intellisense says, does the compiler report an error? Also it should be `m_list(new List())` in the initialiser list. – Cat Plus Plus Oct 10 '11 at 23:29
  • Jerry: I think the idea is that I want to add an indeterminate number of nodes to the list, hence the dynamic memory allocation – dtg Oct 10 '11 at 23:31
  • 2
    @Dylan: You don't need the dynamic allocation for the list itself. – Cat Plus Plus Oct 10 '11 at 23:35
  • What compiler are you using and how are you invoking it? – Michael Aaron Safyan Oct 10 '11 at 23:41
  • Intellisense's syntax checking can be a bit hit-or-miss. Does the actual compiler complain? – Ayjay Oct 10 '11 at 23:43
  • 1
    Also btw: If you're going to manually manage your memory, you need to define a destructor, a copy constructor and an operator=. Check out the Rule of Three. – Ayjay Oct 10 '11 at 23:46
  • Thanks Ayjay. I understand the destructor part, but in this case all I would be display is ints. Why would i need an overloaded operator here? – dtg Oct 10 '11 at 23:53
  • 1
    @Dylan Whenever you have dynamic memory data member you need to overload the copy c'tor and the assignment operator so that you can create a deep copy of the object. – FailedDev Oct 10 '11 at 23:56
  • See if the comments in the accepted answer of http://stackoverflow.com/questions/4845410/error-lnk2019-unresolved-external-symbol-main-referenced-in-function-tmaincr help. Specifically, "I figured it out, in the project properties, under Linker, it was set to Windows, instead of Console." – David Alber Oct 11 '11 at 00:05

2 Answers2

3

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.

FailedDev
  • 26,680
  • 9
  • 53
  • 73
1

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.

Adam
  • 16,808
  • 7
  • 52
  • 98