最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin6PV0评论

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);
}
Post a comment

comment list (0)

  1. No comments so far