-3

I have the given template class with the following attributes/classes

template<class T1, class T2, int max>
class Collection{
    T1 * _elementi1[max];
    T2 * _elementi2[max];
    int currently;
public:
    Collection() {
        for (size_t i = 0; i < max; i++) {
            _elementi1[i] = nullptr;
            _elementi2[i] = nullptr;
        }
        currently = 0;
    }
    ~Collection() {
        for (size_t i = 0; i < max; i++) {
            delete _element1[i]; _element1[i] = nullptr;
            delete _element2[i]; _element2[i] = nullptr;
        }
    }
    T1 ** GetT1() { return _element1; }
    T2 ** GetT2() { return _element2; }
    int GetCurrent() { return currently; }
    void Add(T1 t1, T2 t2) {
        if (currently == max)
        {
            throw exception("MAX SIZE REACHED");
        }

        _element1[currently] = new T1(t1);
        _element2[currently] = new T2(t2);
        ++currently;
    }

    friend ostream& operator<< (ostream &COUT, Collection&obj) {
        for (size_t i = 0; i < obj.currently; i++)
            COUT << *obj._element1[i] << " " << *obj._element2[i] << endl;
        return COUT;
    }


};

Max is used to limit the capacity of the Collection(stupid I know..)The issues is that I use #include <algorithm> that has a function called max as well. Every time I want to use the variable Intellisense and the compiler use the function instead of the variable.How do I tell the compiler to use the variable max and not the function?

Also before people submit code improvement and other suggestions.Its a exam example in which you are not allowed to rename/modify variables,you are only allowed to add stuff as you see fit.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
Adin Sijamija
  • 695
  • 2
  • 7
  • 19
  • ",you are only allowed to add stuff as you see fit. " - I think deleting would be a better idea. –  Aug 14 '17 at 18:16
  • Ask your teacher what to do here. The simplest solution is to rename it to `Max`, but if this is explicitly prohibited by your teacher then you should make it their problem. – GManNickG Aug 14 '17 at 18:20
  • 10
    Don't use `using namespace std;`? That way you'll have `max` and `std::max`. – simon Aug 14 '17 at 18:22
  • 10
    this is a good example of why 'using namespace std' is a Bad Thing (tm) – pm100 Aug 14 '17 at 18:23
  • Stacking multiple lines on one line for no reason is a first step towards creating utterly unmaintainable code. Don't do it unless you have no other choice. – tadman Aug 14 '17 at 18:24
  • Is COUT different than `std::cout`? The C++ is a case-sensitive language. – Thomas Matthews Aug 14 '17 at 18:25
  • COUT looks like an attempt to solve the same issue (poorly) – pm100 Aug 14 '17 at 18:27
  • @GManNickG Yeah I just renamed it. After searching for a while its seems the most simple way – Adin Sijamija Aug 14 '17 at 18:27
  • 1
    Er, as others are saying: do you have `using namespace std`? – GManNickG Aug 14 '17 at 18:28
  • Keep an eye out for preceding underscores. They often mean something in C++. [What are the rules about using an underscore in a C++ identifier?](https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) – user4581301 Aug 14 '17 at 18:33
  • Does the unmodified 'exam example' compile? – 2785528 Aug 14 '17 at 19:27
  • @DOUGLASO.MOEN yes its does assuming i don't include algorithm – Adin Sijamija Aug 15 '17 at 10:30
  • So why are you including if it is not needed? Or perhaps what is the feature of that you _are_ using? Instead of "using namespace std", your code could pull in ONLY the features you need, and thus NOT pull in "std::max". Did you yet answer if you have 'using namespace std' -- your addition of this might be the root of your problem. – 2785528 Aug 15 '17 at 17:43
  • @DOUGLASO.MOEN Yes I am using namespace std. I wanted to post the entire code but it isnt in English so I would just get downvoted into oblivion. I consulted my proffesor and just renamed it Max instead of max. – Adin Sijamija Aug 16 '17 at 10:05
  • @King23 - I consider leaving out non-English stuff reasonable (unless it was my grandparents language, perhaps). Please review [MCVE] for your next question, the key goal being to reproduce your _problem_ using a minimum of code. – 2785528 Aug 16 '17 at 18:10

1 Answers1

1

Does the unmodified 'exam example' compile?

yes its does assuming i don't include algorithm

I'd say this is evidence that you have added a "using namespace std;" somewhere, perhaps to get some feature out of < algorithm >

How do I tell the compiler to use the variable max and not the function?

One way is to cancel your compiler request to pull-in all of or any of namespace std functions into the local namespace ... by this I mean remove the "using namespace std;"

Now, perhaps you need a feature of < algorithm > ... how do you get it without also pulling in "std::max"

Example : from < algorithm >, I often use shuffle()

#include <algorithm>

// ... then I usually do 
std::shuffle (m_iVec.begin(), m_iVec.end(), gen);

// I have no problem using the std:: prefix.

At this point the function std::max() is also known to the compiler, but will not conflict with your variable name. The only way to access the function is through the "std::max()" symbol.


There is another form of 'using', that looks like:

#include <algorithm>

// now 'bring in' the feature you want.
using  std::shuffle; // pull in shuffle, BUT NOT std::max

// ... so I now can do 
shuffle (m_iVec.begin(), m_iVec.end(), gen);

// and have no worries about max being interpreted as a function
max = 0;

Forever after I must search for the hint / reminder of which 'shuffle()' method I am invoking here.

2785528
  • 5,438
  • 2
  • 18
  • 20
  • The writer of the code, with the constraints identified, has laid a trap for you and all who try it ... you fell for it. "using namespace std" pulled in std::max so that you could refer to it as simply 'max(). The trap fires because the symbol max() is already used by the variable. The variable does not have a namespace (does it?), so your only option is to recognize this trap, and pull in only the one std symbol you need. – 2785528 Aug 15 '17 at 18:28
  • The following use of a declarative list also illustrates your problem. "void showMax(int max) { using std::max; std::cout << max << std::endl; }" ... to which the compiler complains: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream}’ and ‘’)" where max is the function instead of the int. – 2785528 Aug 15 '17 at 21:04