c++ - How do I use a variable that has the same name as a function? -


i have given template class following attributes/classes

template<class t1, class t2, int max> class collection{     t1 * _elementi1[max];     t2 * _elementi2[max];     int currently; public:     collection() {         (size_t = 0; < max; i++) {             _elementi1[i] = nullptr;             _elementi2[i] = nullptr;         }         = 0;     }     ~collection() {         (size_t = 0; < 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) {         (size_t = 0; < obj.currently; i++)             cout << *obj._element1[i] << " " << *obj._element2[i] << endl;         return cout;     }   }; 

max used limit capacity of collection(stupid know..)the issues use #include <algorithm> has function called max well. every time want use variable intellisense , compiler use function instead of variable.how tell compiler use variable max , not function?

also before people submit code improvement , other suggestions.its exam example in not allowed rename/modify variables,you allowed add stuff see fit.

does unmodified 'exam example' compile?

yes assuming don't include algorithm

i'd evidence have added "using namespace std;" somewhere, perhaps feature out of < algorithm >

how tell compiler use variable max , not function?

one way cancel compiler request pull-in of or of namespace std functions local namespace ... mean remove "using namespace std;"

now, perhaps need feature of < algorithm > ... how without pulling in "std::max"

example : < algorithm >, use shuffle()

#include <algorithm>  // ...  std::shuffle (m_ivec.begin(), m_ivec.end(), gen);  // have no problem using std:: prefix. 

at point function std::max() known compiler, not conflict variable name. way access function through "std::max()" symbol.


there form of 'using', looks like:

#include <algorithm>  // 'bring in' feature want. using  std::shuffle; // pull in shuffle, not std::max  // ... can  shuffle (m_ivec.begin(), m_ivec.end(), gen);  // , have no worries max being interpreted function max = 0; 

forever after must search hint / reminder of 'shuffle()' method invoking here.


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -