i tried it ..........But i am getting redifnation of ostream
operator
That's because you're defining it multiple times.
File1.cc
#include <map>
#include <iostream>
#include "File3.cc"
What's this? The compiler doesn't care, but by convention,
files with names ending in .cc or .cpp (or .C or .cxx) are
source files, not headers, and so shouldn't be included.
File2.cc
#include <map>
#include <iostream>
#include <iterator>
#include "File3.cc"
Same thing here.
#include<iostream>
using namespace std;
class A
{
public :
int x;
int y;
A(int x1,int y1) // Constructor
{
x = x1;
y = y1;
}
int operator==(const A &rhs) const
Note that operator== should normally return bool.
{
if( this->x != rhs.x) return 0;
if( this->y != rhs.y) return 0;
return 1;
}
friend ostream & operator<<(ostream &, const A &);
};
The above (except for the using namespace) is a class
definition, which belongs in a header file if it is to be used
in more than one source file. Something like:
file A.hh:
#include <iosfwd> // since you don't need all of iostream.
struct A
{
int x ;
int y ;
A( int initialX, int initialY ) ;
bool operator==( A const& other ) const ;
friend std:
stream& operator<<( std:
stream&, A const&) ;
} ;
You can put inline definitions in the header, but I'd recommend
against it for normal classes. (For templates, the issues are a
bit different.) I'd also define a member function isEqual,
and make the operator== (and operator!=) non-members. (More a
style issue than otherwise, except that I use a template base
class to generate them automatically, so that I'm guaranteed
never to have one without the other, and that both have the same
definition of equality.)
Also, there's no real point in making operator<< a friend, since
all of the data are public.
ostream &operator<<(ostream &output, const A &aaa)
{
output << aaa.x << ' ' << aaa.y ;
return output;
}
This is implementation. It belongs in a source file, along with
the other function implementations, e.g.:
file A.cc
#include "A.hh" // Always first, to ensure that it
// is independent.
#include <ostream>
A::A(
int initialX,
int initialY )
: x( initialX )
, y( initialY )
{
}
bool
A:
perator==(
A const& other ) const
{
return x == other.x && y == other.y ;
}
std:
stream&
operator<<(
std:
stream& dest,
A const& object )
{
dest << object.x << ' ' << object.y ;
return dest ;
}
struct cmp
{
bool operator()(const A & lhs, const A & rhs)
{
if(lhs.x < rhs.x ) return 1;
return 0;
Why not just:
return lhs.x < rhs. x ;
?
Again, a mixture of definition and implementation. In this
case, the function is inline, so it's permitted to put this in a
header; the class is simple enough that many programmers would
even prefer it in a header, but I'd generally still use two
files here.
What I would also ask myself is whether this should be part of
the definition of A or not. If so, I would seriousy consider
making it an operator< member, and letting the default arguments
for map do their job. If not, then I would probably put it in a
separate header with a typedef of the map (in which case, I
probably would implement the function inline, rather than create
a separate source file just for it).