Thread: overloading <<

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    43

    overloading <<

    Hi,
    i'm new with c++, and this is my first overloading
    in the last line of this .h file (marked), the compiler desagrees.
    it says:
    Code:
    error C2143: syntax error : missing ';' before '&'
    error C2433: 'ostream' : 'friend' not permitted on data declarations
    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    now, when i add "std:stream&" instead of "ostream&, i get:
    Code:
     error C2270: '<<' : modifiers not allowed on nonmember functions
    the file: (error line is marked)
    Code:
    //Graph.h
    #ifndef _GRAPH_
    #define _GRAPH_
    
    #include <iostream>
    
    /**************FORWARD DECLERATIONS******************************************/
    class LinkedList;
    class EdgeNode;
    class Edge;
    class Vertex;
    
    class Node;
    class EdgeList;
    
    /*********************END OF FORWARD DECLARATIONS***************************/
    class Graph 
    {
    private:
    	LinkedList *m_list;
    	int m_countVer;
    
    public:
    
    	//locate edge. return pointer to location or NULL (if not found)
    	EdgeNode* findEdge (const int &source,const int &dest)const;
    	
    	//find vertex (by id value)
    	Node* findVer (const int & id)const;
    
    	//find vertex (by vertex)
    	Node* findVer (const Vertex &)const;
    
    	//Ccot
    	Graph(const Graph & g2);
    	//ctor
    	Graph();
    	//dtor
    	~Graph();
    
    	//add vertex to graph (check if not exist first)
    	void addVer(const Vertex & v1);
    
    	//add edge to graph, add vertexes if not exist
    	void addEd(const int & sourceId, const int & destSource);
    	
    	//delete vertex (argument- vertex)
    	void delVer(const Vertex & v1);
    
    	//delete edge. 
    	void delEd(const Edge & ed1);
    
    	//delete edges (by id)
    	void delEd(const int & source,const int & dest);
    
    	//operator union
    	Graph operator+ (const Graph & g2) const ;
    	
    	//operator Intersection
    	Graph operator-  (const Graph & g2)const;
    
    	//operator - unary - complement
    	Graph operator- () const;
     
    	//operator equal and opposite
    	bool operator == (const Graph & g2) const;
    	bool operator != (const Graph & g2) const {return !((*this)==g2);};
    	
    	//operator print
    	friend ostream& operator <<(ostream & os,const Graph & g2)const; // <--ERROR
    };
    #endif

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It is a free function (or nonmember function). Free functions can't be declared const - as the error message rather clearly says.

    The const after member functions applies to the implicitly passed this pointer (the object on the left of the . or -> operator). There is no this pointer in nonmember functions.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    43
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading << and >>
    By MMu in forum C++ Programming
    Replies: 1
    Last Post: 04-21-2008, 06:49 AM
  2. Overloading fstream's << and >> operators
    By VirtualAce in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2007, 03:17 AM
  3. Overloading << and >>
    By Enahs in forum C++ Programming
    Replies: 2
    Last Post: 09-15-2005, 04:33 PM
  4. Overloading the << operator.
    By antex in forum C++ Programming
    Replies: 5
    Last Post: 05-31-2005, 01:37 AM
  5. << and >> overloading
    By Armatura in forum C++ Programming
    Replies: 2
    Last Post: 12-07-2003, 06:19 PM