Like Tree2Likes
  • 1 Post By jimblumberg
  • 1 Post By laserlight

Overloading '<<' operator using Struct

This is a discussion on Overloading '<<' operator using Struct within the C++ Programming forums, part of the General Programming Boards category; I am currently trying to overload the '<<' operator using a struct that holds a flight's number, origin, destination, and ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    13

    Overloading '<<' operator using Struct

    I am currently trying to overload the '<<' operator using a struct that holds a flight's number, origin, destination, and price. However, I am continuously getting this error. I have searched boards and tried multiple of the suggestions but I have had no luck. Any help is well appreciated!

    My error is:
    "error: expected unqualified-id before 'friend'"

    Here's my header file:
    Code:
    #include <iostream>#include <string>
    
    
    using namespace std;
    
    
    #ifndef TYPE_H
    #define TYPE_H
    
    
    struct flightStruct
    {
        int flightNum;                                                        //Flight Number
        string source;                                                        //Origin Flight
        string destination;                                                    //Destination Flight
        int price;                                                            //Price of Flight
        
        bool operator < (const flightStruct& rhs) const;                    //Overloaded operator for "less than"    
        bool operator == (const flightStruct& rhs) const;                    //Overloaded operator for "equal to"
        bool operator != (const flightStruct& rhs) const;                    //OVerloaded operator for "not equal to"
        friend ostream& operator << (ostream& os, const flightStruct& rhs);    //Overloaded operator for output
        
    };
    flightStruct flightRec;                                                    //flightRec is variable to represent flightStruct
    
    
    #endif
    And my overloaded '<<' definition:

    Code:
    friend flightStruct::ostream& operator << (ostream& os, const flightStruct& rhs)    //Overloaded operator for '<<'{                                                                                    //for struct output
        os << rhs.flightStruct.flightNum
           << rhs.flightStruct.source
           << rhs.flightStruct.destination
           << rhs.flightStruct.price;    
        return os;
        
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,697
    Since you're using a structure and all the variables are public you don't actually need the friendship. Just use a normal overload.
    Code:
    ostream& operator << (ostream& os, const flightStruct& rhs)    //Overloaded operator for '<<'{                                                                                    //for struct output
    {
        os << rhs.flightNum
           << rhs.source
           << rhs.destination
           << rhs.price;
        return os;
    
    }
    Jim

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    13
    Thanks, Jim.
    I found my errors and have corrected them but I am getting another error. Isn't this error similar to the original just worded differently? Why is this happening?

    type.h:26: error: 'std:stream& flightStruct:perator<<(std:stream&, const flightStruct&)' must take exactly one argument
    type.cpp:31: error: expected constructor, destructor, or type conversion before '&' token

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,697
    Post the code that is causing these errors. How did you correct your errors?

    Jim

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    13
    I removed friend from both my header and implementation.

    I kept 'flightStruct' scope resolution operator, but I am not sure if this is necessary. If I remove it from this definition and my other overloaded operator definitions, it just provides more errors.

    I realized "rhs.flightStruct.flightNum" was not correctly accessing those members of flightStruct so I removed flightStruct. Those were the only changes I made based on the suggestions you made and it gave me the new error I posted previously.

    Code:
    flightStruct::ostream& operator << (ostream& os, const flightStruct& rhs)	//Overloaded operator for '<<'{																					//for struct output
    	os << rhs.flightNum
    	   << rhs.source
    	   << rhs.destination
    	   << rhs.price;	
    	return os;
    	
    }

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    2,697
    You seem to be missing the opening brace. (Actually it seems to be embedded within the comment).

    Did you place the declaration outside the class?


    Jim
    laserlight likes this.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,445
    Might be just some forum formatting problem, but this:
    Code:
    #include <iostream>#include <string>
    should be:
    Code:
    #include <iostream>
    #include <string>
    Furthermore, these header inclusions should come within the header inclusion guard to make it more likely that the compiler will avoid parsing the whole header file in the event it is included multiple times.

    This is wrong:
    Code:
    using namespace std;
    Using directives should not be at file scope in a header file. Remove it. Of course, once you do so, you would qualify the names from the std namespace, e.g., use std::string instead of string and std::ostream instead of ostream. Actually, this brings me to my next point, which is that this is unnecessary and even inaccurate here:
    Code:
    #include <iostream>
    Rather, it should be:
    Code:
    #include <iosfwd>
    then you would include in the source file:
    Code:
    #include <ostream>
    <iostream> would be more for cases where you use say, std::cout.

    Now, this is probably wrong:
    Code:
    flightStruct flightRec;
    For starters, avoid global variables. If flightRec must be global, then you probably should have a naming convention to distinguish it, and it should not be defined in the header file like this. Rather, you would declare it extern:
    Code:
    extern flightStruct flightRec;
    then define it in one source file.
    Elysia likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Feb 2013
    Posts
    13
    Thank you all for your help, I was able to resolve the problem through the help provided.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. operator overloading
    By jbook in forum C++ Programming
    Replies: 4
    Last Post: 10-17-2007, 02:37 AM
  2. Overloading * Operator
    By Bnchs400 in forum C++ Programming
    Replies: 3
    Last Post: 10-01-2007, 12:21 PM
  3. = operator overloading
    By linuxdude in forum C++ Programming
    Replies: 21
    Last Post: 09-21-2007, 08:48 AM
  4. Operator Overloading - Can This Be Done?
    By mike_g in forum C++ Programming
    Replies: 21
    Last Post: 09-21-2007, 03:03 AM
  5. operator overloading ()
    By terracota in forum C++ Programming
    Replies: 4
    Last Post: 04-01-2004, 06:52 AM

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