为什么我不能使用单词min和max作为变量名? [英] why I can't use the words min and max as variable names?

查看:155
本文介绍了为什么我不能使用单词min和max作为变量名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此C ++程序中,函数 print_nums 不接受
vector< double> 数字作为在 switch_function 中调用的实际参数,我希望 add_num 函数接受vector作为参数,但我认为

In this C++ program the function print_nums doesn't accept the vector<double> numbers as an actual prameter when called in the switch_function and I wanted the add_num function to accept vector as an argument but I think it will cause the same error!

请注意,所提到的错误未出现在此代码中,但是当我编译此代码以及更改最低价最低价最高价两次 double maxx 我对上面提到的错误感到满意,顺便说一下,我还是C ++的初学者,所以请解释为什么我不能使用单词min和max,也是主要的函数 print_nums 的错误以及任何其他错误或警告。

note that the error mentioned doesn't appear in this code but many errors and warnings appear when I compile this code and when I change double min to double minn or double max to double maxx I'm lift with the mentioned error, by the way I'm still a beginner in C++ so please explain why I can't use the words min and max ,also the main error of the function print_nums and any other error or warning.

#include <iostream>
#include <vector>
#include <climits>
#include <cfloat>
using namespace std;
double mean{};
    double min=FLT_MAX;
    double max=FLT_MIN;
vector<double>numbers{};
char select;
char list();
void print_nums(vector<double>&numbers);
void program_func();
double nums_mean(double);
char switch_function(char);
void display_nums_mean(double);
int main(){


   program_func();


        return 0;
}
void program_func(){
    while(select!='q'&&select!='Q'){
       list();
  switch_function(select);

        }
    }
char list(){
     cout<<"Please select an Order\n";
        cout<<"***********************\n";
        cout<<"P-Print numbers\n";
        cout<<"A-Add a number\n";
        cout<<"M-Display the numbers mean\n";
        cout<<"S-Display smallest number\n";
        cout<<"L-Display largest number\n";
        cout<<"Q-Quit\n";
        cout<<"Order " ;
        cin>>select;
        cout<<endl;
    return select;

    }
void print_nums(const vector <double> &numbers){
    if(numbers.size()!=0)
     for(auto value : numbers)
              cout<<value<<endl;
            else
                cout<<"Array is empty"<<endl;

    }
void add_num(){
        cout<<"How many numbers will be added to the list : ";
        unsigned int num_of_nums;
        double num;
        cin>>num_of_nums;
        cout<<endl;
        cout<<"add numbers to the list : ";
        for(unsigned int i=0;i<num_of_nums;i++){
            cin>>num;
            numbers.push_back(num);
            }
        return;
        }
double nums_mean(double mean){
    double sum{};
    for(unsigned int i=0;i<numbers.size();++i){
        sum+=numbers.at(i);

        }
    mean = sum/(numbers.size());


    return mean;


    }
double small_num(double min){
    for(unsigned int i=0;i<numbers.size();++i){

                if(min>=numbers.at(i)){

                    min = numbers.at(i);
                    }

            }
            return min;
    }
double large_num(double max){
    for(unsigned int i=0;i<numbers.size();++i){
                if(max<=numbers.at(i)){

                    max = numbers.at(i);
                }

            }
            return max;

    }
void display_nums_mean(double mean){
    cout<<"The mean of the numbers is : "<<mean<<endl;


    }
void display_nums_min(double min){
    cout<<"The minimum number is : "<<min<<endl;

    }
void display_nums_max(double max){
    cout<<"The maximum number is : "<<max<<endl;
    return;
    }
char switch_function(char select){
    switch(select){
        case 'p':
        case 'P':
                 print_nums(numbers);
                 break;
        case 'a':
        case 'A':
                 add_num();
                 break;
        case 'm':
        case 'M':
                 mean=nums_mean(mean);
                 display_nums_mean(mean);
                 break;
        case 's':
        case 'S':
                 min=small_num(min);
                display_nums_min(min);
                 break;
        case 'l':
        case 'L':
                 max=large_num(max);
                 display_nums_max(max);
                 break;
        case 'q':
        case 'Q':
         return select;
        default:
         cout<<"Please Enter a valid character "<<endl;


    }

    return select;   
    }


推荐答案


我仍然是C ++的初学者,所以请解释为什么我不能使用单词min和max ...

I'm still a beginner in C++ so please explain why I can't use the words min and max...

您可以!

第一节(以下)以最少的代码演示了正在发生的事情。

Section one (below) demonstrates (in minimal code) what is happening.

第二部分显示了一个解决方案(如下)。

Section two shows a 'solution' (below).

奖金-第三部分使用C ++类解决。

Bonus - third section solves using C++ class.

1)两种编码选择中的第一种是[MCVE](您应该认识到它的起源),它重新创建了此选择导致的歧义(并因此导致错误)。

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)供您考虑,另一个难题(尚未提及)(以MCVE形式)解决是将您的代码尽可能简单地移动到可区分命名空间中。此操作可以很容易地完成:


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.

符号不再冲突,并且AZ :: min和AZ :: max在尊重方面是唯一且可区分的

The symbols no longer 'clash', and AZ::min and AZ::max are unique and distinguishable with respect to the std::min and std::max.

注意1:这应该足以使他们选择不使用以下行: using namespace std;

Note1: This should be sufficient motivation to make the choice to not use the line: "using namespace std;".

注2:如果您保留 using namespace std;行,请将其放置在您独特的命名空间包装器(AV)中。

Note2: If you keep the line "using namespace std;", do not place it inside of your distinguishing namespace wrapper (AV).

是的-仍然可以访问std ::,在您独特的命名空间中,它们分别称为std :: min和std :: max。

Yes - it is still possible to access std::, within your distinguishing namespace, they are referred to as std::min and std::max.


函数print_nums的主要错误

main error of the function print_nums

也许有第二个值得提出的问题。在您的MCVE上工作,并使用SO工具进行练习。您可以通过提问获得积分(不赞成投票的人是要求您进行更多练习的人!)。

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!).

我想向您强调mcve的 M。

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)另一个(尚未提及的)难题的解决方案是移动代码,就像可能,加入区分类

3) Yet another (as yet unmentioned) 'solution' to your dilemma is to move your code, as simply as possible, into a 'distinguishing' class.

您已将此代码标记为C ++。恕我直言,C ++的主要功能是类。因此,我建议初学者将精力集中在创建和使用类上,并学习所提供的库(等。我进一步建议避免编写C样式代码(带有一些C ++类),这是消除任何误导性习惯的时候。

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.

使用一个区分类:

#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.

注意3:此时,类AZ_t是其余代码的位置。关于在班级中编码的一件好事是,您不需要功能转发。 (您的帖子大约有6条转发-C风格代码的证据)只需按任意顺序展示该函数(编译器即可解决),但我建议您尝试使用最易读的序列。

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.

这篇关于为什么我不能使用单词min和max作为变量名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆