Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring vectors in a C++ header file

Tags:

I am having some trouble with vector declarations in the header file of a C++ class I am making. My entire header file looks like this:

#ifndef PERSON_H #define PERSON_H  #include "Message.h" #include <string> #include <vector>   class Person {  public:   Person() {};  Person(std::string emailAddress);  private:   vector<Message> inbox;  vector<std::string> contacts;  std::string emailAddress;  };  #endif PERSON_H 

My error occurs on the lines following the "private" declaration (where I declare my vectors). The error I am getting is C4430 - missing type specifier and and C2238 - unexpected tokens preceding ';'

Thank you for any help.

like image 808
James W. Avatar asked Nov 19 '10 23:11

James W.


People also ask

What is the header file for vector?

The header file for the STL vector library is vector. (Note that when using C++, header files drop the . h; for C header files - e.g. stdlib.

Can I define array in header file?

A global array does not have to be declared in a header file. However, doing so allows the array to be used, without having to copy/paste the declaration to everywhere it is needed.

What is the use of #include vector?

By writing #include <vector> , you are telling the compiler to not only use your own code, but to also compile a file called vector .


2 Answers

You're missing the namespace:

std::vector 
like image 74
Moo-Juice Avatar answered Oct 25 '22 06:10

Moo-Juice


You need to put 'std::' before 'vector' just like you did with string.

like image 31
Edward Strange Avatar answered Oct 25 '22 06:10

Edward Strange