简体   繁体   中英

How to allocate memory for new structure?

I'm trying to allocate the memory for a new linked list within the constructor of a class, but when I try to compile it, I get some weird message.

struct Node {
  Song s;
  Node *next = NULL;
};


tsuPod::tsuPod(int songs, int size)
{
  MAX_SONGS = songs;
  MAX_MEM   = size;
  memory = 0;
  Node *list = new Node; // call to implicitly deleted default constructor of Node
}

Seems like this should be a relatively quick response, as I'm guessing I'm missing something key to c++ and not necessarily making a syntax error. To be honest, I didn't even know structs could have constructors, but apparently they can. Anyways, if someone could quickly tell me what's wrong or why it's saying then that you would be awesome! Thank you!

As you didn't provide any constructor for Node , the default constructor for Node would be provided by the compiler. But when it attempted to do so, it ran into a problem: To (default) construct a Node , you first need to construct a Song . Creating a Song can be impossible for one of three reasons:

  1. All constructors for Song take one or more arguments, and you didn't provide a default value for them.
  2. You explicitly deleted the default constructor: Song() = delete .
  3. There is a default constructor, bt it's private.

That's why the error message also notes that the default constructor is implicitly deleted; you can now rule out options #2 and #3.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM