Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with Vector-Type Member Variable

Status
Not open for further replies.

japhy

Programmer
Sep 27, 2004
12
US
Hi all,

I've been using vectors of CStrings in various functions in my code. I'd like to make one of them a class member variable

In the function, I declare it as such:

vector<CString> vec(100,"");

where it has a size and is initialized.

when I attempt to put this same line under the public heading of my class, i get "constant" errors...what am I doing wrong?

any help would be greatly appreciated.
 
You cannot call the constructor when you declare a variable inside a class. You call member variables' constructors in the initializer list of the class. For example:
Code:
class Foo
{
public:
  Foo() : vec(100) { }
private:
  std::vector<CString> vec;
};
Note that you don't need the "" in the vector constructor since it will just call the default CString constructor which creates an empty string.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top