最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

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);
}
发布评论

评论列表(0)

  1. 暂无评论