954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

6 Line class -> Me pounding head into wall

I need to write a class using a vector and this is my first time using vectors. But I have looked through several books/web pages and can't see why this thing won't compile.

#include

template
class pQ
{
public:

private:
vector a(1);

};

Any ideas. There must be some small syntax issue here I cant see. The error is something along the lines of using a struct that is not defined.

jasweb2002
Junior Poster in Training
56 posts since Sep 2004
Reputation Points: 11
Solved Threads: 2
 

Your problems all stem from this line:

vector<el> a(1);

First and foremost you need to remember that any standard names are in the std namespace. If you don't have a using declaration (using std::vector;) or a using directive (using namespace std;) then you need to qualify the declaration explicitly:

std::vector<el> a(1);

The second problem is that you're trying to call a constructor in a class declaration. Because this isn't executable code, you want to remove the argument list:

std::vector<el> a;

Then you can call the constructor of a in the constructor of pQ:

template <class el>
class pQ
{
public:
  pQ()
    : a ( 1 )
  {}
public:
  std::vector<el> a;
};

Though because vectors grow dynamically, you typically don't need to worry about this and can just rely on push_back working it's magic. Only in extreme cases of performance or if the vector is not likely to change its size do you want to set a base size or preallocate a certain amount of memory.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
template <class el>
class pQ
{
public:
  pQ()
    : a ( 1 )
  {}
public:
  std::vector<el> a;
};

QUOTE]

Thanks, that helped some. I am having a few problems though. I couldn't use that exact code snippet due to certain ways the code needs to be formatted. Here is the relevant code. This class is being called as an int.

using namespace std;

template
class pQ
{
public:
pQ();

private:
vector a;

};

template
pQ:: pQ()
{
a(1);
}

Is it possible to do it this way? The error is:

no match for call to '(std::vector >) (int)

Thanks again.

jasweb2002
Junior Poster in Training
56 posts since Sep 2004
Reputation Points: 11
Solved Threads: 2
 

>Is it possible to do it this way?
No, what I showed you was an initializer list for the class constructor. This list has special properties that allow you to actually initialize variables rather than just assign to them as you would have to in the constructor body. If you do not use the initializer list then the vector will be default constructed and you need to use resize:

template <class el>
class pQ
{
public:
  pQ();

private:
  vector<el> a;

};

template <class el>
pQ<el>:: pQ()
{
  a.resize ( 1 );
}
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Thanks Narue.

jasweb2002
Junior Poster in Training
56 posts since Sep 2004
Reputation Points: 11
Solved Threads: 2
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You