# Template Question

## Recommended Posts

I'm having trouble using a template class in a class. I'll explain: Here's the code...
#include <vector>

class myClass
{
public:
std::vector<char> myArray(7);

bool resizeIt(int);
};

bool myClass::resizeIt(int re)
{
myArray.resize(re);

return true;
}


When I try to compile this, I get 2 errors: (Line 6) error C2059: syntax error : 'constant' (Line 13) error C2228: left of '.resize' must have class/struct/union type What's wrong!? Maybe it's something I don't understand about templates...? Any help is much appreciated.

##### Share on other sites
What are you trying to do with a vector or chars? A std::string might make things easier on you. You could also use the vector.push_back() method to automatically resize the vector whenever you add on values.

##### Share on other sites
Line 6: std::vector<char> myArray(7);

Pass class member constructor parameters in the class constructor initialization list.

class myClass{public:	std::vector<char> myArray;        myClass() : myArray(7) {}	bool resizeIt(int);};

Line 13: myArray.resize(re);

There was an error in the definition of myArray (see line 6 above), this is why the compiler does not recognize it later, and tells you the token before .resize(re) needs to be an object.

##### Share on other sites
I think it is a problem with how your declaring your Vector rather than a template.

##### Share on other sites
'preciate it, Fruny.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628344
• Total Posts
2982186

• 9
• 24
• 9
• 9
• 13