使用“新” c++ 中带有结构的关键字
#include "PQueue.h"
struct arcT;
struct coordT {
double x, y;
};
struct nodeT {
string name;
coordT* coordinates;
PQueue<arcT *> outgoing_arcs;
};
struct arcT {
nodeT* start, end;
int weight;
};
int main(){
nodeT* node = new nodeT; //gives error, there is no constructor
}
我的目的是在堆中创建一个新的nodeT
。错误是:
错误 C2512:“nodeT”:没有合适的默认构造函数可用
#include "PQueue.h"
struct arcT;
struct coordT {
double x, y;
};
struct nodeT {
string name;
coordT* coordinates;
PQueue<arcT *> outgoing_arcs;
};
struct arcT {
nodeT* start, end;
int weight;
};
int main(){
nodeT* node = new nodeT; //gives error, there is no constructor
}
My purpose is to create a new nodeT
in heap. Error is:
error C2512: 'nodeT' : no appropriate default constructor available
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
PQueue
没有合适的默认构造函数,因此编译器无法生成nodeT
的默认构造函数。为PQueue
创建适当的默认构造函数,或者为nodeT
添加用户定义的默认构造函数,以适当地构造outgoing_arcs
。PQueue<arcT *>
does not have an appropriate default constructor, so a default constructor fornodeT
cannot be generated by the compiler. Either make an appropriate default constructor forPQueue<arcT *>
or add a user-defined default constructor fornodeT
which constructsoutgoing_arcs
appropriately.如果问题中当前发布的代码是精确副本,则导致此错误的唯一可能原因是
PQueue<...>
未定义默认构造函数,而是定义了另一个构造函数。否则这段代码会编译。
更准确地说,由于您没有为结构定义构造函数,C++ 会尝试自动生成它们。但它只能做到这一点,只要它的所有成员变量都是适当的默认构造或可初始化的。
std::string
有一个默认的构造函数,而coordT*
作为一个指针,可以被初始化。因此只有PQueue<...>
仍然是罪魁祸首。If the currently posted code in the question is an exact copy, then the only possible cause for this error is that
PQueue<…>
doesn’t define a default constructor, and defines another constructor instead.Otherwise this code would compile.
More precisely, since you didn’t define a constructor for your structures, C++ tries to auto-generate them. It can only do this, though, as long as all its member variables are appropriately default constructible or initialisable.
std::string
has a default constructor, andcoordT*
, being a pointer, can be initialised. So onlyPQueue<…>
remains as the culprit.这可能不是你的问题,但你在 arcT 的这一行上只声明了一个指针:-
你已经将 start 声明为指针,将 end 声明为实际的 nodeT 对象。这是你想做的吗?
THis may not be your problem but you've only declared one pointer on this line in arcT :-
You've declared start as a pointer and end as an actual nodeT object. Is this what you wanted to do?