operator==

This is a discussion on operator== within the C++ Programming forums, part of the General Programming Boards category; I'm trying to overload the operator== from a struct that i have made. I hold the struct's in a std::list ...

  1. #1
    Unregistered
    Guest

    operator==

    I'm trying to overload the operator== from a struct that i have made. I hold the struct's in a std::list -->> list<Record>My_Rec_List;
    -----------------------------------------------

    Trying to do:
    if ( iter == My_Rec_List.front() )

    ------------------------------------------------
    what i have (defined in the struct):

    friend bool operator==(Record r, const Record &IN2){
    return r.First_number == IN2.First_number;
    }


    VC++ 6.0 error
    --------------------
    error C2678: binary '==' : no operator defined which takes a left-hand operand of type 'class std::list<struct Record,class std::allocator<struct Record> >::iterator' (or there is no acceptable conversion)
    Error executing cl.exe.

  2. #2
    Unregistered
    Guest
    better yet,

    writing the record....

    what iter x is pointing to is what i need to write...

    Writes garbage but the only thing i can get to compile

    Code:
    SfileS.write((char*)(&x), sizeof(Record));

  3. #3
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >Trying to do:
    if ( iter == My_Rec_List.front() ) <

    You'll have to use the operator* on iter.

    >what iter x is pointing to is what i need to write...<

    You could overload operator>> and operator<< to read/write a Records elements to file.

  4. #4
    Unregistered
    Guest
    You could overload operator>> and operator<< to read/write a Records elements to file.
    Can i do that, but still write in Binary mode ?

  5. #5
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    No if you really need to read/write in binary, then you need to do something like -

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    struct TwoInts
    {
    	int a;
    	int b;
    };
    
    int main()
    {
    	ofstream os("ti.bin",ios::binary);
    	TwoInts one={1,2};
    	os.write((char *)&one,sizeof(one));
    	os.close();
    
    	ifstream is("ti.bin",ios::binary);
    	TwoInts two;
    	is.read((char*)&two,sizeof(two));
    
    	cout << two.a << ' ' << two.b << '\n';
    
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. operator==
    By Tux0r in forum C++ Programming
    Replies: 17
    Last Post: 07-09-2009, 02:29 AM
  2. stl error
    By eklavya8 in forum C++ Programming
    Replies: 8
    Last Post: 06-30-2008, 12:01 PM
  3. "error: incomplete type is not allowed"
    By Fahrenheit in forum C++ Programming
    Replies: 9
    Last Post: 05-10-2005, 09:52 PM
  4. Problem with Template Function and overloaded equality operator
    By silk.odyssey in forum C++ Programming
    Replies: 7
    Last Post: 06-08-2004, 04:30 AM

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