-
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 :(
-
vector lives in namespace std
you should declare
Code:
std::vector<std::string> alarms
Kurt
-
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.
-
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
-
>> 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.
-
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)
-
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
-
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