Sign in to follow this  
ricekrispyw

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


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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this