Thread: vectors in header files

  1. #1
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168

    vectors in header files

    hey guys, found a problem when writing a class, not really a problem, more the fact that im not sure what to do about it. Anyway, here's the class:
    Code:
    class room
    {
    public:
    	room();
    	virtual ~room();
    	void addAlarm();
    	void const getAlarms();
    
    protected:
    	std::string name;
    	vector<std::string> alarms;
    
    private:
    
    };
    error C2143: syntax error : missing ';' before '<'

    Now im pretty sure it's going to be because i've used a vector without including the vector header. Is there a way around it, like i have done for the string datatype?

    Edit: i actually just included the vector header file, and the errors still crop up
    Last edited by Welshy; 08-28-2005 at 01:50 PM.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    vector lives in namespace std
    you should declare
    Code:
    std::vector<std::string> alarms
    Kurt

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    You used std::string without including the <string> header? Well that doesn't make sense. You would have had to include it at some point, it doesn't matter that you were referencing it through the std::, when you #include <string> the string class gets added to the namespace. Same with a vector. And if you weren't using the std namespace (or specific classes of it), you would #include <vector>, just like <string>, and then say like:

    Code:
    std::vector<std::string> alarms
    Edit: minute too slow.
    Last edited by Tonto; 08-28-2005 at 01:56 PM.

  4. #4
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    I've read in more than one place to include as few files as possible in the interface header file, and then include them in the implementation file, but ill include them as you suggest

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I've read in more than one place to include as few files as possible in the interface header file, and then include them in the implementation file, but ill include them as you suggest

    This is true, include only the headers that you need. Since you are using a vector, you need <vector>. Since you are using string, you need <string>. A forward declaration is all that is necessary if you only use a pointer or reference to the class, although for standard library classes it is better to just include the header.

  6. #6
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    alright, thanks guys.

    Just one more thing, about inclusion guards and header files. Do i put the inclusion guards around the header files too, or do i only include my code itself (not including "using namespace std" either)

  7. #7
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You put the include-guards around all the code in the headers ( including included headers ).
    You do not have to because the headers usually have include-guards themselves but it speeds up compilation a little.
    Kurt

  8. #8
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Well, generally, it is best to avoid 'using namespace ...' for any namespace within a header. This is because you will 'pollute' the global namespace for anything that includes your header. For source files, since they are not included by anything*, the choice is mainly preference.

    As for the inclusion guards. All the headers you are including should have their own inclusion guards, so it should not make any functional difference. That said, my prefence is to put everything inside the inclusion guards.

    * Now, if you have a templated class or function(s), and you include the definitions at the bottom to keep your header at least looking clean, then in that definition file, avoid 'using namespace ...' as well. Generally, I give these files a different extension (.tmpl) to differentiate them from normal source files.

    *edit*
    And ZuK makes a good point. This will only really be noticeable in very large programs, but going out for lunch, and coming back to find the program still compiling can be a royal pain.

    Cheers
    Last edited by Zach L.; 08-28-2005 at 02:24 PM.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Confusion on header and source files
    By dnguyen1022 in forum C++ Programming
    Replies: 4
    Last Post: 01-17-2009, 03:42 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. more header files
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-28-2001, 01:56 PM