943,987 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2396
  • C RSS
Oct 16th, 2004
0

6 Line class -> Me pounding head into wall

Expand Post »
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 <vector>

template <class el>
class pQ
{
public:

private:
vector<el> 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.
Similar Threads
Reputation Points: 11
Solved Threads: 2
Junior Poster in Training
jasweb2002 is offline Offline
56 posts
since Sep 2004
Oct 16th, 2004
0

Re: 6 Line class -> Me pounding head into wall

Your problems all stem from this line:
  1. 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:
  1. 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:
  1. std::vector<el> a;
Then you can call the constructor of a in the constructor of pQ:
  1. template <class el>
  2. class pQ
  3. {
  4. public:
  5. pQ()
  6. : a ( 1 )
  7. {}
  8. public:
  9. std::vector<el> a;
  10. };
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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 17th, 2004
0

Re: 6 Line class -> Me pounding head into wall

  1. template <class el>
  2. class pQ
  3. {
  4. public:
  5. pQ()
  6. : a ( 1 )
  7. {}
  8. public:
  9. std::vector<el> a;
  10. };
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 el>
class pQ
{
public:
pQ();

private:
vector<el> a;

};

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

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

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

Thanks again.
Reputation Points: 11
Solved Threads: 2
Junior Poster in Training
jasweb2002 is offline Offline
56 posts
since Sep 2004
Oct 17th, 2004
0

Re: 6 Line class -> Me pounding head into wall

>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:
  1. template <class el>
  2. class pQ
  3. {
  4. public:
  5. pQ();
  6.  
  7. private:
  8. vector<el> a;
  9.  
  10. };
  11.  
  12. template <class el>
  13. pQ<el>:: pQ()
  14. {
  15. a.resize ( 1 );
  16. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 17th, 2004
0

Re: 6 Line class -> Me pounding head into wall

Thanks Narue.
Reputation Points: 11
Solved Threads: 2
Junior Poster in Training
jasweb2002 is offline Offline
56 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: How to change a button's shape ?
Next Thread in C Forum Timeline: Quicksort/Insertion sort Hyrbid?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC