C++ C++;新接线员报错了

C++ C++;新接线员报错了,c++,C++,在下面的代码中,语句“s=new int[50];”给出了错误 错误: 错误1错误LNK2001:未解析的外部符号“private:static int*Instack::s”(?s@Instack@@0PAHA)Stack.obj Stack\u pr #include<iostream> #include<stdlib.h> #define maxma 50; using namespace std; class Instack{ private: stati

在下面的代码中,语句“s=new int[50];”给出了错误

错误: 错误1错误LNK2001:未解析的外部符号“private:static int*Instack::s”(?s@Instack@@0PAHA)Stack.obj Stack\u 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();

}
错误2错误LNK2001:未解析的外部符号“private:static int Instack::top1”(?top1@Instack@@0HA)Stack.obj Stack\u 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();

}
错误3致命错误LNK1120:2个未解析的外部C:\Users\vinoda.kamble.LGE\Desktop\Bill\New folder\stack\u pr\Debug\stack\u pr.exe stack\u 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();

}
#包括
#包括
#定义maxma50;
使用名称空间std;
Instack类{
私人:
静态int*s;
静态int-top1;
公众:
Instack()
{
s=新整数[50];
}
void Instack::推送(int t)
{
s[top1++]=t;
}
int Instack::pop()
{
int t;
t=s[--top1];
返回t;
}
};
空干管()
{
Instack S1、S2;
S1.推动(522);
S2.推送(255);

cout原因是静态类成员需要一个定义。在类定义之外添加类似的内容将解决链接问题

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

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

堆栈中的所有变量都是静态的(在实例之间共享)-一旦您解决了链接器问题,它将无法正常工作