Thread: some help needed here

  1. #1
    Registered User n3cr0_l0rd's Avatar
    Join Date
    Feb 2009
    Posts
    62

    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 ===|
    I had crush on her, before that crush could CRUSH me, i CRUSHED that crush !!! Fun time over, Lets find a way to not to blow off the whole leg !

    __________________________________________________ __________________________________________________ _______________

    In C++ it's harder to shoot yourself in the foot, but when you do, you blow off your whole leg.
    - Bjarne Stroustrup

  2. #2
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    There's also a std::distance. They conflict.

    That's why you should never use namespace std!

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Pick another name - it seems there is a distance somewhere in the STL.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User n3cr0_l0rd's Avatar
    Join Date
    Feb 2009
    Posts
    62
    @brafil, u mean i shouldn't do using namespace std; ???

    and thx to all
    I had crush on her, before that crush could CRUSH me, i CRUSHED that crush !!! Fun time over, Lets find a way to not to blow off the whole leg !

    __________________________________________________ __________________________________________________ _______________

    In C++ it's harder to shoot yourself in the foot, but when you do, you blow off your whole leg.
    - Bjarne Stroustrup

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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 dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    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;

  7. #7
    Registered User n3cr0_l0rd's Avatar
    Join Date
    Feb 2009
    Posts
    62
    umm... as i read in accelerated cpp, i should be doing
    using std::cout;
    using std::cin;
    .........
    Do you mean that ?
    I had crush on her, before that crush could CRUSH me, i CRUSHED that crush !!! Fun time over, Lets find a way to not to blow off the whole leg !

    __________________________________________________ __________________________________________________ _______________

    In C++ it's harder to shoot yourself in the foot, but when you do, you blow off your whole leg.
    - Bjarne Stroustrup

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Yeah

  9. #9
    Registered User n3cr0_l0rd's Avatar
    Join Date
    Feb 2009
    Posts
    62
    okay i get it then! thx guys
    I had crush on her, before that crush could CRUSH me, i CRUSHED that crush !!! Fun time over, Lets find a way to not to blow off the whole leg !

    __________________________________________________ __________________________________________________ _______________

    In C++ it's harder to shoot yourself in the foot, but when you do, you blow off your whole leg.
    - Bjarne Stroustrup

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. free needed or not?
    By quantt in forum Linux Programming
    Replies: 3
    Last Post: 06-25-2009, 09:32 AM
  2. C Programmers needed for Direct Hire positions
    By canefan in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 09-24-2008, 11:55 AM
  3. G++ Makefile Help Needed
    By OWD2k7 in forum Linux Programming
    Replies: 17
    Last Post: 04-30-2007, 06:43 PM
  4. C++ help needed
    By Enkindu in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 08-31-2004, 11:24 PM
  5. semi-colon - where is it needed
    By kes103 in forum C++ Programming
    Replies: 8
    Last Post: 09-12-2003, 05:24 PM