//This is class for Linked list
class SinglyLinkedListNode {
public:
int data;
SinglyLinkedListNode *next;
SinglyLinkedListNode(int node_data) {
this->data = node_data;
this->next = nullptr;
}
};
SinglyLinkedListNode* insertNodeAtHead(SinglyLinkedListNode* head, int key )
{
SinglyLinkedListNode* newNode = &SinglyLinkedListNode(key);
/*1st way of creating a node sing a class constructor
error(i got for this) :error: taking address of temporary [-fpermissive]
*/
SinglyLinkedListNode* newNode = new(SinglyLinkedListNode );//2nd way of creating a node
newNode->data=key;
newNode->next=NULL;//following error i got by second method
/*solution.cc:59:66: error: no matching function for call to ‘SinglyLinkedListNode::SinglyLinkedListNode()’
SinglyLinkedListNode* newNode = new(SinglyLinkedListNode );//2nd way of creating a node
^
solution.cc:10:9: note: candidate: ‘SinglyLinkedListNode::SinglyLinkedListNode(int)’
SinglyLinkedListNode(int node_data) {
^~~~~~~~~~~~~~~~~~~~
solution.cc:10:9: note: candidate expects 1 argument, 0 provided
solution.cc:5:7: note: candidate: ‘constexpr SinglyLinkedListNode::SinglyLinkedListNode(const SinglyLinkedListNode&)’
*/
}
答案 0 :(得分:0)
当你写的时候 new(SinglyLinkedListNode); 您正在堆上创建一个SinglyLinkedListNode类型的对象,编译器将尝试为该对象调用默认构造函数,但该类没有默认构造函数。如果你要写 新的SinglyLinkedListNode(6); 编译器将能够调用您仅有的带有int参数的构造函数。 我的猜测是,这是结构而不是类,因为该结构没有任何CTOR,因此编译器能够为其创建默认的CTOR。