【发布时间】:2011-12-04 21:45:23
【问题描述】:
已解决: 我一生都无法弄清楚为什么在尝试在这里初始化堆栈时会出错:
#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
【问题讨论】:
-
我看不出有什么根本上的错误。添加您收到的实际错误消息。
-
m_stack = new List;呢? -
只使用
List m_stack;而不是动态分配 List 对象怎么样? -
无视 Intellisense 的说法,编译器会报错吗?在初始化列表中也应该是
m_list(new List())。 -
@Dylan:列表本身不需要动态分配。
标签: c++ data-structures stack