I wanted to have a vector<string> and a vector<double> in my class so I used forward declarations below:

class string;
template <typename T> class vector;

But I recieve the error: field 'varname' has incomplete type for both vector<string> and vector<double> variables.
I searched internet for this error but all the things I found was problems with user written classes not STLs'.
Thanks

Edited 5 Years Ago by pywriter: n/a

Are you trying to use STL or not?

If not then all you have to do is include headers

#include <vector>
#include <string>
using namespace std;
int main(){
 vector<string> a;
 vector<double> b;
}

Edited 5 Years Ago by firstPerson: n/a

Well that has some problems too.When I include vector and string instead of making forward declarations,I recieve following errors:

explicit Term(const string&);
const Term derive(const string&);
/*error1:expected ',' or '...' before '&' token
error2:ISO C++ forbids declaration of 'string' with no type*/

vector<string> var;
vector<double> power;
/*error1:ISO C++ forbids declaration of 'vector' with no type
error2:expected ';' before '<' token*/

Edited 5 Years Ago by pywriter: n/a

Its probably not in the namespace, try this :

std::vector< std::string > var;
explicit Term(const std::string&);

Thanks.That solved the problem.
But forward declaration should have done it too!what was wrong?

If you're creating your own vector template class, then the forward declaration is:

template <class T> class vector;

And depending on your compiler, the entire source for the template class may need to be included into the same file (specifically, you may not be able to separate the prototype into a vector.h file and the implementation into a vector.cpp file).

Unless you're also writing your own string class, then you have to include it -- it's not part of the C++ language, it's a class written in C++:

#include <string>
using namespace std;

string myString;

or

#include <string>

std::string myString;
This article has been dead for over six months. Start a new discussion instead.