C++新运算符出现错误
C++ new operator is giving error
在下面的代码中,语句"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)堆栈.obj堆栈_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();
}
原因是静态类成员需要一个定义。在类定义之外添加这样的内容将解决链接问题。
int* Instack::s = nullptr;
int Instack::top;
不幸的是,这会泄露内存。您可能想要做的是将s
和top
都作为非静态成员变量。
相关文章:
- 在运算符重载定义中使用成员函数(const错误)
- 逻辑运算符上出现错误代码 a')'
- 错误 C2679:二进制"<<":未找到采用类型 'std::string_view' 的右侧操作数的运算符(或者没有可接受的转换)
- C++:需要帮助了解运算符重载错误
- 我在 .h 中有一个枚举类,并且在.cpp错误中有一个运算符重载:与"运算符<<不匹配
- C++分数混合比较运算符错误
- 为什么我收到错误:"运算符<<不匹配?
- 迭代器类的重载前缀增量运算符会引发分段错误
- 在 boost::qi 中使用过多的替代运算符会导致分段错误
- 调用运算符时出现链接器错误<<,如何解决?
- 错误:表达式必须具有算术、无作用域枚举或带有运算符重载的指针类型
- 解决错误:运算符 [] 不匹配
- 提升错误:"运算符=="的重载不明确
- 字符串输入中没有匹配的错误运算符错误
- 错误:运算符 = IN C++ 不匹配
- QT 创建器错误(运算符 + 不匹配)
- 错误C++运算符重载
- 错误:运算符==不匹配
- C++ 语法和编译器错误 - 运算符不匹配<<
- C++错误--运算符=不匹配