c++ - How do i make a predetermined parameter for a struct inside a constructor - Stack Overflow

Whenever i try to run my program, it gives me an error saying that i'm missing an argument for the

Whenever i try to run my program, it gives me an error saying that i'm missing an argument for the eight parameter in my constructor Here is the struct Here is the struct

struct maximos {
    size_t lengthTitulo;
    size_t lengthAutor;
    size_t lengthIdioma;
    size_t lengthGenero;
    void CreacionVariables();
    
};

Here is the constructor

Libro(const std::string = "", const std::string = "", const std::string = "", int = 0, int = 0, const std::string = "", int = 0, maximos&);

i've tried writing "maximos& obj", but it doesn't work. other than that i don't know what to do.

Whenever i try to run my program, it gives me an error saying that i'm missing an argument for the eight parameter in my constructor Here is the struct Here is the struct

struct maximos {
    size_t lengthTitulo;
    size_t lengthAutor;
    size_t lengthIdioma;
    size_t lengthGenero;
    void CreacionVariables();
    
};

Here is the constructor

Libro(const std::string = "", const std::string = "", const std::string = "", int = 0, int = 0, const std::string = "", int = 0, maximos&);

i've tried writing "maximos& obj", but it doesn't work. other than that i don't know what to do.

Share Improve this question edited Nov 17, 2024 at 3:50 John Kugelman 363k69 gold badges553 silver badges597 bronze badges asked Nov 17, 2024 at 3:45 Franco Zavala ManFranco Zavala Man 1 5
  • 3 Libro is not a constructor for maximos – Ted Lyngmo Commented Nov 17, 2024 at 3:50
  • I'm not using Libro as a constructor for maximos, i want to know how to put maximos inside the constructor as a predetermined parameter – Franco Zavala Man Commented Nov 17, 2024 at 3:57
  • 2 Always post verbatim error messages. I am pretty sure if you read the error to the end then it contains an answer. Non default parameters are not allowed after the parameters with the default arguments. If it was allowed, how would compiler know which arguments are to wich parameters. – 3CxEZiVlQ Commented Nov 17, 2024 at 3:57
  • I know, but i don't know how to make it a default argument – Franco Zavala Man Commented Nov 17, 2024 at 4:01
  • 3 You supposed to post a minimal reproducible example so we know what you want to implement. Now your question looks like the XY-problem - your are solving X but asking how to solve Y. – 3CxEZiVlQ Commented Nov 17, 2024 at 4:03
Add a comment  | 

1 Answer 1

Reset to default 4

If you want to pass a default argument to the last parameter then declare it const:

Libro(const std::string = "", const std::string = "", const std::string = "", int = 0, int = 0, const std::string = "", int = 0, const maximos& = {});

You cannot make it non const because a non const reference can't bind rvalue, which is a default argument because it does not have a name.

If you strictly should pass a non const default argument, then overloaded functions/constructors are your friends.

Libro(const std::string, const std::string, const std::string, int, int, const std::string, int, maximos&);

Libro(const std::string a = "", const std::string b = "", const std::string c = "", int d = 0, int e = 0, const std::string f = "", int g = 0) {
  maximos deafault_maximos;
  Libro(a, b, c, d, e, f, g, deafault_maximos);
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745639293a4637588.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信