C ++ new运算符给出了错误

时间:2015-02-13 04:40:20

标签: c++

在下面的代码中声明" s = new int [50];"给出错误。

错误: 错误1错误LNK2001:未解析的外部符号" private:static int * Instack :: s" (?s @ Instack @@ 0PAHA)Stack.obj stack_pr

错误2错误LNK2001:未解析的外部符号" private:static int Instack :: top1" (?top1 @ Instack @@ 0HA)Stack.obj stack_pr

错误3致命错误LNK1120:2个未解析的外部C:\ Users \ vinoda.kamble.LGE \ Desktop \ Bill \ New folder \ stack_pr \ Debug \ stack_pr.exe stack_pr

#include<iostream>
#include<stdlib.h>

#define maxma 50;
using namespace std;

class Instack{

private: 
static int *s;
static int top1;

public:

Instack ()
{
    s= new int[50];
}

void Instack:: push(int t)
{
    s[top1++]= t;

}


int Instack::pop()
{
    int t;
t= s[--top1];
return t;
}
};



void main ()
{
    Instack S1,S2;

S1.push(522);

S2.push(255);


cout<<"S1 pop",S1.pop();

cout<<"S2 pop",S2.pop();

}

1 个答案:

答案 0 :(得分:1)

原因是静态类成员需要定义。在课程定义之外添加这样的内容将解决您的链接问题。

int* Instack::s = nullptr;
int Instack::top;

不幸的是,这会泄漏内存。您可能要做的是将stop都作为非静态成员变量。