-
some help needed here
Code:
// To convert meter to feet and inch
#include <iostream>
using namespace std;
const float M = 3.25;
class distance
{
private:
int feet;
float inch, meter;
public:
distance() {}
~distance() {}
distance(float a)
{
meter = a;
float k = meter*M;
feet = int(k);
inch = (k-feet) * 12.0;
}
void show()
{
cout << feet << " feets " << inch << " inches" << endl;
}
};
int main()
{
distance::distance d1(1.5);
d1.show();
system("pause");
return 0;
}
if i dont use distance::distance d1(1.5) and rather use just distance d1(1.5), i get a series of error messages:
I:\C++\Lab 7\1a.cpp||In function `int main()':|
I:\C++\Lab 7\1a.cpp|32|error: use of `distance' is ambiguous|
I:\C++\Lab 7\1a.cpp|10|error: first declared as `class distance' here|
i:\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\. .\..\..\..\include\c++\3.4.5\bits\stl_iterator_bas e_funcs.h|115|error: also declared as `typename std::iterator_traits<_Iterator>::difference_type std::distance(_InputIterator, _InputIterator)' here|
I:\C++\Lab 7\1a.cpp|32|error: `distance' was not declared in this scope|
I:\C++\Lab 7\1a.cpp|32|error: expected `;' before "d1"|
I:\C++\Lab 7\1a.cpp|33|error: `d1' was not declared in this scope|
||=== Build finished: 6 errors, 0 warnings ===|
-
There's also a std::distance. They conflict.
That's why you should never use namespace std!
-
Pick another name - it seems there is a distance somewhere in the STL.
-
@brafil, u mean i shouldn't do using namespace std; ???
and thx to all
-
Or make your own namespace perhaps.
using namespace std;
is a quick hack for the lazy typist.
Sooner or later, sloppy use of namespace will bite you.
-
If you just want to save yourself some typing when using cout, you can just do something like this which is safer:
Code:
using std::cout;
using std::endl;
using std::cin;
-
umm... as i read in accelerated cpp, i should be doing
using std::cout;
using std::cin;
.........
Do you mean that ?
-
-
okay i get it then! thx guys