why can't my program compile?

This is a discussion on why can't my program compile? within the C++ Programming forums, part of the General Programming Boards category; I'm trying to compile code from a book but it is having errors. here is the code: Code: // englio.cpp ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    3

    why can't my program compile?

    I'm trying to compile code from a book but it is having errors. here is the code:

    Code:
    // englio.cpp
    // overloaded << and >> operators
    #include <iostream>
    using namespace std;
    ////////////////////////////////////////////////////////////////
    class Distance                           //English Distance class
       {
       private:
          int feet;
          float inches;
       public:
          Distance() : feet(0), inches(0.0)  //constructor (no args)
             {  }
                                             //constructor (two args)
          Distance(int ft, float in) : feet(ft), inches(in)         
             {  }
          friend istream& operator >> (istream& s, Distance& d);  
          friend ostream& operator << (ostream& s, Distance& d);
       };
    //--------------------------------------------------------------
    istream& operator >> (istream& s, Distance& d)  //get Distance
       {                                            //from user
       cout << "\nEnter feet: ";  s >> d.feet;      //using
       cout << "Enter inches: ";  s >> d.inches;    //overloaded
       return s;                                    //>> operator
       }
    //--------------------------------------------------------------
    ostream& operator << (ostream& s, Distance& d)  //display
       {                                            //Distance
       s << d.feet << "\'-" << d.inches << '\"';    //using
       return s;                                    //overloaded
       }                                            //<< operator
    ////////////////////////////////////////////////////////////////
    int main()
       {
       Distance dist1, dist2;          //define Distances
       Distance dist3(11, 6.25);       //define, initialize dist3
    
       cout << "\nEnter two Distance values:";
       cin >> dist1 >> dist2;          //get values from user
                                       //display distances
       cout << "\ndist1 = " << dist1 << "\ndist2 = " << dist2;
       cout << "\ndist3 = " << dist3 << endl;
       return 0;
       }
    the errors are

    (23) : error C2248: 'feet' : cannot access private member declared in class 'Distance'
    (9) : see declaration of 'feet'
    (24) : error C2248: 'inches' : cannot access private member declared in class 'Distance'
    (10) : see declaration of 'inches'
    (30) : error C2248: 'feet' : cannot access private member declared in class 'Distance'
    (9) : see declaration of 'feet'
    (30) : error C2248: 'inches' : cannot access private member declared in class 'Distance'
    (10) : see declaration of 'inches'
    (40) : error C2593: 'operator >>' is ambiguous
    (42) : error C2593: 'operator <<' is ambiguous
    (43) : error C2593: 'operator <<' is ambiguous

    i'm using the microsoft visual c++ compiler. why are these errors coming up?

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Your code compiles without errors or warnings for me on MSVC++ .NET 2003 and MSVC++ 6.0.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    3
    ok then I guess I have to enable or disable something in the compiler, but I have no idea what. can anyone help?

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Try the following in MSVC++ 6.0 (if you are using .NET, let us know):
    • Go to the File menu and select "New...".
    • Make sure the Projects tab is selected.
    • Set the Project name: Distances (or whatever you want)
    • Set the Location: Whatever you want (e.g. C:\Tests\Distances)
    • Select Win32 Console Application.
    • Click OK.
    • "An empty project" should be selected.
    • Click Finish.
    • Click OK.
    • Go to the File menu and select "New..." again.
    • Make sure the Files tab is selected.
    • Check the box next to "Add to project:"
    • Set the File name: englio.cpp
    • Set the Location - leave the default location.
    • Select C++ Source File.
    • Click OK.
    • Paste in code exactly as you posted it.
    • Build the project by going to the Build menu and selecting "Build Distances.exe", or by hitting F7, or by clicking the Build button on the toolbar.
    Does that work?

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    3
    shoot, it didn't work. same errors.

  6. #6
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Based on the errors, it doesn't seem to recognize the friend keyword, or the friend declaration and the definition aren't matching. Of course, in the code you posted, that is not the case, which is why it compiles for me. Are you sure the code you posted is exactly the same as the code you are compiling?

    If that's the case, the only thing I can think of is a bad install, but that doesn't make any sense.

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    98
    I had same question in VC++ , but ok in DEV-C++,
    after read some source code from book, I find this is answer:
    Code:
    class Distance;      //adding prototype declare to remove this error.
    istream& operator >> (istream& s, Distance& d);  
    ostream& operator << (ostream& s, Distance& d);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Compile Public Domain Pocket PC C Program
    By m1l in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 07-20-2007, 04:02 AM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. new to C--can't get program to compile
    By samerune in forum C Programming
    Replies: 12
    Last Post: 04-02-2007, 09:44 AM
  5. how do i compile a program that deals w/classes?
    By Shy_girl_311 in forum C++ Programming
    Replies: 5
    Last Post: 11-11-2001, 01:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21