• Advertisement
Sign in to follow this  

Template Question

This topic is 4710 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement