我有两个结构
template<typename T>
struct Node{
T obj;
Node* next;
Node* prev;
Node();
Node(T a, Node<T>* b=NULL, Node<T>* c=NULL);
};
template<typename T>
struct Monomial : public Node<T>{
int n;
Monomial(T coeff = 0, int p = 0) : Node<T>(coeff){ n=p; }
};
编译器告诉我,我做错了什么但我无法理解?
我尝试在main
函数中执行此操作:
Monomial<int> *m1;
m1->n=5;
m1->obj=6;
我得到的错误信息是“运行时检查失败#3 - 正在使用变量'm1'而未初始化。”
答案 0 :(得分:3)
像这样初始化m1。
Monomial<int> *m1 = new Momonial<int>();
答案 1 :(得分:3)
声明指针不会创建对象。
// Allocate memory for the object and create it.
Monomial<int> *m1 = new Monomial<int>;
m1->n=5;
m1->obj=6;
// When you are done with the object, destroy it and deallocate memory.
delete m1;
在堆栈上交替创建对象
Monomial<int> m1;
m1.n=5;
m1.obj=6;
无需致电new
或delete