6 Line class -> Me pounding head into wall

Reply

Join Date: Sep 2004
Posts: 56
Reputation: jasweb2002 is an unknown quantity at this point 
Solved Threads: 2
jasweb2002 jasweb2002 is offline Offline
Junior Poster in Training

6 Line class -> Me pounding head into wall

 
0
  #1
Oct 16th, 2004
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,630
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 718
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
0
  #2
Oct 16th, 2004
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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 56
Reputation: jasweb2002 is an unknown quantity at this point 
Solved Threads: 2
jasweb2002 jasweb2002 is offline Offline
Junior Poster in Training

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

 
0
  #3
Oct 17th, 2004
  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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,630
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 718
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
0
  #4
Oct 17th, 2004
>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. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 56
Reputation: jasweb2002 is an unknown quantity at this point 
Solved Threads: 2
jasweb2002 jasweb2002 is offline Offline
Junior Poster in Training

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

 
0
  #5
Oct 17th, 2004
Thanks Narue.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC