what is the reason for getting error while the same code for class name starting with caps compiling and running well.

what is the reason for getting error while the same code for class name starting with caps compiling and running well.
#include <iostream>
using namespace std;
template<typename t>
class max{t n1;
t n2;
public:
      max(t a,t b){n1=a;n2=b;
      }
      t maximum(){return (n1>n2)?n1:n2;
      }
};
int main(){max<int> obj(5,8);
obj.maximum();
return 0;
}
error: In function 'int main()':
      [Note] candidates are: template<class t> class max

#include <iostream>
using namespace std;
template<typename t>
class Max{t n1;
t n2;
public:
      Max(t a,t b){n1=a;n2=b;
      }
      t maximum(){return (n1>n2)?n1:n2;
      }
};
int main(){Max<int> obj(5,8);
obj.maximum();
return 0;
}
output:8
Ganesh SadanalaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fabrice LambertFabrice LambertCommented:
Hi,

First, be aware that C++ is case sensitive. max() isn't the same as Max().
Second, your error is simple:
using namespace std;

There is a max function in the standard Library, by using this directive, your class is conflicting with it.
Ehence why the "using namespace" directive is considered a bad practice. It's sole and only purpose is to ensure compatibility with old code base, back when namespaces were introduced in C++ (1990), thus it have no business in modern codes.

Either fully qualify your function calls / variables declaration, or select explicitly what you intend to use:
int main()
{
    std::cout << "Hello world." << std::end;
}

Open in new window

int main()
{
    using std::cout;
    using std::endl;

    cout << "Hello world." << end;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sarabandeCommented:
to add to above solution:

C++ is case sensitive. max() isn't the same as Max().
Ganesh was aware of this and didn't make a mistake regarding case sensitivy.

It's sole and only purpose is to ensure compatibility with old code base, back when namespaces were introduced in C++ (1990)

that is not true. if they would have not provided the using clause, all old code automatically would have accessed the right (old) functions. au contraire, the purpose of the using clause is to allow lazy coding if there is no problem with an old code base that might have used same names as the standard library.

the "using namespace" directive is considered a bad practice
although i am not making use of the usage clause myself, we should make precise distinctions: in header files which might be included together with various other headers, the usage of using clauses would make the header file a potential problem for the source file that includes the header since the using clause would apply to all subsequent source code after th einclude statement as well. therefore, using clauses in header files are not only bad practice but a big mistake. using clauses in cpp files which are made below any include statement, would only apply to the source code of this one source. if there is only new code and no old interfaces to consider, using clauses are subject of the internal coding rules, if any,  which may or may not allow using clauses. i would not use it for the STL since the std:: prefix is really short and makes clear that STL templates are used which have a specific and consequently used naming. as i have seen some third-party libraries which were using nested namespaces with huge names, i would explicitly recommend to making usage of using clauses in order to keep your code readable and avoid long and error-prone typing.

Sara
0
sarabandeCommented:
Fabrice rightly pointed to the 'using namespace std;' clause which was the reason for the compile error when calling a private max function. My comment adds some valuable knowledge where using clauses could be used and where not.

Sara
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.