dimanche 9 janvier 2022

Problem about creating an empty vector in a class delegating constructor

I can create an empty vector like:

std::vector<std::string> svec;

Now I'm trying to write a delegating constructor for the Book class:

class Book {
public:
    Book(): Book("", "", std::vector<std::string> svec, 0, "", 1){}
    
    Book(const std::string &i, const std::string &n,
         const std::vector<std::string> &au,
         unsigned y, const std::string &p = "", unsigned v = 1)
            : isbn(i), name(n), author(au),
              publish_year(y), publisher(p), version(v) {}
    }

In the above code the first constructor (which is the default constructor) is delegating to the second one. I want to provide the delegated constructor with an empty vector as an argument in the same way I create the empty vector before. However, the compiler shows an error here:

Expected '(' for function-style cast or type construction

And then I was suggested to use std::vector<std::string> () instead and it works! My questions:

  1. Why I can use std::vector<std::string> svec create an empty vector while cannot in the class constructor?
  2. How to explain the format std::vector<std::string> ()? what does it do or how is it executed?

Aucun commentaire:

Enregistrer un commentaire