header file

This is a discussion on header file within the C++ Programming forums, part of the General Programming Boards category; hi all, I guess i'm missing somthing, because this header file just won't complile. I get loads of errors.... the ...

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

    header file

    hi all,
    I guess i'm missing somthing, because this header file just won't complile.
    I get loads of errors....
    the first error is marked, (".... error C2143: syntax error : missing ';' before 'const')
    i didn't bother marking the subsequent errors... (ther're all very similar)
    thanks

    Code:
    //Graph.h
    #ifndef _GRAPH_
    #define _GRAPH_
    
    #include <iostream>
    //#include "LinkedList.h"
    using namespace std;
    
    
    /**************FORWARD DECLARATIONS******************************************/
    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(const int &source,const int &dest); // <--- ERROR!!!
    	
    	//find vertex (by id value)
    	Node* findVer const(const int & id);
    
    	//find vertex (by vertex)
    	Node* findVer const(const Vertex &);
    
    	//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 (const Graph & g2);
    	
    	//operator substarct
    	Graph operator- const (const Graph & g2);
    
    };
    #endif

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Looks to me like you have the const on the wrong side of the function arguments.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    14
    Assuming that you want to declare findEdge() and the others as const member functions, the proper syntax is
    Code:
    EdgeNode* findEdge(const int &source,const int &dest) const;
    The compiler is complaining because "const" is in the wrong place.

    Also, it's not a good idea to put a using declaration in a header file.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    43
    obviously!!!

    please explain why it's not a good idea to put a using declaration in a header file

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,275
    Quote Originally Posted by msshapira
    please explain why it's not a good idea to put a using declaration in a header file
    A using declaration at file scope in a header file would then be present in any file that includes that header, even if the programmer does not expect it to be present. In this case you have a using directive, not using declaration, at file scope in a header file, but the reasoning is the same.
    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

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Quote Originally Posted by msshapira View Post
    please explain why it's not a good idea to put a using declaration in a header file
    It opens up that namespace to any source file that happens to include that header whether the programmer intends it or not. This can cause confusion to the compiler if you happen to have an object in your source with the same name as an object in the namespace. Tricky problems can also be caused simply due to the order in which a source file may include a set of headers - one way might work, another might not - because one may open up that namespace and cause problems for all subsequent headers.

    You should explicitly qualify any objects that require a namespace within the header. Once your back in the realm of the source file, if you want to do it there you can (after all headers have been included).
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    14
    Sorry, that was a using directive in your header file, not a using declaration.

    The reason it's not a good idea to put a using directive in a header file is that it opens the namespace in every source file (.cpp or whatever your C++ extension is) in which the header file is included. This defeats the purpose of the namespace feature, which is to prevent name collisions. Using directives are best used only in source files.

    When you need to use an identifier from a namespace in a header file, use the namespace name with the scope resolution operator and identifier:
    Code:
    std::cout

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    	void addEd(const int & sourceId, const int & destSource);
    Is there any particular reason these are const references, rather than simple integers? If you are trying to make it faster, then that's NOT going to be the case, since using references adds another dereference in the called function (and often makes the code slightly less optimized).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    43
    dunno,
    it's what i've been taught. whenever i want to protect a value and assure it won't be changed- pass as const ref...

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sure, but passing small basic types (e.g. int) as a non-reference (and with const if you like) is fine, it won't change the original value of the passed in value.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 08:51 PM
  2. Replies: 30
    Last Post: 06-19-2006, 01:35 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  4. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 09:19 PM

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