In this C++ program the function print_nums
doesn\'t accept
the vector
numbers as an actual prameter when called in the switch
I'm still a beginner in C++ so please explain why I can't use the words min and max...
you can!
Section one (below) demonstrates (in minimal code) what is happening.
Section two shows a 'solution' (below).
Bonus - third section solves using C++ class.
1) The first of two coding choices is a [MCVE] (which you should recognize its origins) that recreates the ambiguity (and thus the error) that this choice causes.
// this first section matches your code,
#include <iostream>
#include <vector>
#include <climits>
#include <cfloat>
using namespace std;
// this 'using' pulls a 'min' and 'max' (probably functions)
// from some std:: declaration
double min = FLT_MAX;
double max = FLT_MIN;
// ----^^^---- and now these 2 items create two ambiguity's because
// at this point, the compiler can no longer distinguish
// between the two min's (or max's).
void program_func(int argc, char* argv[]) {
std::cout << "\n " << argc
<< " " << argv[0]
<< "\n " << min // error: reference to ‘min’ is ambiguous
<< " " << max // error: reference to ‘max’ is ambiguous
<< std::endl;
}
int main(int argc, char* argv[]) {
program_func();
return 0;
}
2) For your consideration, another (as yet unmentioned) 'solution' to your dilemma (in MCVE form) is to move your code, as simply as possible, into a 'distinguishing' namespace. This action can be accomplished quite easily:
#include <iostream>
#include <vector>
#include <climits>
#include <cfloat>
using namespace std;
// this 'using' pulls a 'min' and 'max' (probably functions)
// from some std:: declaration (as previous)
// now place all of **your** code (except main()) into a distinguishing namespace.
namespace AZ // (your initials, if you are not already using them ;)
{
double min = FLT_MAX; // these are now AZ::min
double max = FLT_MIN; // and AZ::max - unambiguous!
void program_func(int argc, char* argv[])
{
std::cout << "\n " << argc
<< " " << argv[0]
<< "\n\n " << min // reference to AV::min
<< " " << max // and AV::max
<< std::endl;
}
} // namespace AZ end
int main(int argc, char* argv[])
{
AZ::program_func(argc, argv); // NOTE AZ:: namespace qualifier!
return 0;
}
This section compiles and runs with no errors.
The symbols no longer 'clash', and AZ::min and AZ::max are unique and distinguishable with respect to the std::min and std::max.
Note1: This should be sufficient motivation to make the choice to not use the line: "using namespace std;".
Note2: If you keep the line "using namespace std;", do not place it inside of your distinguishing namespace wrapper (AV).
Yes - it is still possible to access std::, within your distinguishing namespace, they are referred to as std::min and std::max.
main error of the function print_nums
Perhaps there is a 2nd worthwhile question to submit. Work on your MCVE, and practice with the SO tools. You can earn points with questions (and the down-votes are contributors asking you for more practice!).
I want to emphasize to you the "M" of mcve. Is the 'print_nums' error even related to the first question?
and any other error or warning.
Generally, 'help debug my code' is not appropriate for this site.
3) Yet another (as yet unmentioned) 'solution' to your dilemma is to move your code, as simply as possible, into a 'distinguishing' class.
You have marked this code as C++. IMHO, the key feature of C++ is class. Thus I advice beginners to focus their effort on creating and using classes, and learning the libraries provided (< vector >, < string >, < iostream >, < iomanip >, < sstream > etc. I further advise to avoid writing C style code (with a few C++ classes), this early training is the time to break any misleading habits.
Using a distinguishing class:
#include <iostream>
#include <vector>
#include <climits>
#include <cfloat>
using namespace std;
class AZ_t // suffix '_t' because a class defines a type
{
double min = FLT_MAX; // these are now AZ_t::min
double max = FLT_MIN; // and AZ_t::max - unambiguous!
int program_func(int argc, char* argv[])
{
std::cout << "\n " << argc
<< " " << argv[0]
<< "\n\n " << min // reference to AZ_t::min
<< " " << max // and AZ_t::max
<< std::endl;
}
// NOTE3 - see below
public: // above this statement is private
// a functor is easy to use, but it needs one more function:
int operator()(int argc, char* argv[]) {
return program_func(argc, argv);
}
}; // class AZ end
int main(int argc, char* argv[]) // main is now 1/2 the size
{
return AZ_t()(argc, argv); // because this functor is a 1 liner!
// ^^^^^^^^^^^^ -- parameter to operator()
// ^^^^^^ -- instance ctor (constructor)
// ^^^ -- operator() returns int
// instance dtor (destructor) runs when 1 liner is complete
}
This section compiles and runs with no errors.
NOTE3: At this point in the class AZ_t is the location for the rest of your code. One nice thing about coding 'in a class' is that you do not need function forwarding. (Your post has about 6 forwards -- evidence of c-style code) Simply present the function in any order (the compiler figures it out), but I recommend you try for the most readable sequence.
The line using namespace std
says that you include std namespace. By including this you are including a lot of functions and variables in this namespace.
When you use any variables, if a variable with the same name exists in namespace std
it will be tied up to it. Now by declaring another variable in your code the compiler is confused which one the coder is referring to.
Try to use more meaningful names for your variables max_num
and min_num
.
If you still want to use max
and min
, declare them in a user defined namespace and refer to them in your code.
You use using namespace std;
(get out of that habit) which means that std::min
and std::max
are pulled in from the std
namespace into the global namespace. That is why you can't then use those names in your own code, since they have already been defined.