By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,169 Members | 1,044 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,169 IT Pros & Developers. It's quick & easy.

Struggling with vectors

P: 27
Hi
I have a problem with vectors (its my first time using them) I am trying to add to a vector but it only adds the first time I call push_back! I am using VC++, the relevent code is below,
Thank you in advance
------------------------------

Expand|Select|Wrap|Line Numbers
  1.  #include <vector> 
  2.  
  3. void PipeElementClass::AddPipe(PipeElementClass pipeElem)
  4. {
  5.     ...
  6.     for(int i=0; i<10; i++){
  7.         pipeModel.push_back(pipeElem);
  8.     }
  9.     ...
  10. }
  11.  
  12. I have also overloaded the = and == operators as follows;
  13.  
  14. bool PipeElementClass:: operator==(const PipeElementClass& x){
  15.     return ((*this).fromNode == x.fromNode);
  16. }
  17.  
  18. PipeElementClass PipeElementClass:: operator=(const PipeElementClass& x){
  19.     if(this != &x){
  20.         (*this).fromNode = x.fromNode;
  21.         (*this).toNode = x.toNode;
  22.         (*this).xLength = x.xLength;
  23.         (*this).yLength = x.yLength;
  24. ...
  25.     }
  26.     return *this;
  27. }
  28.  
Jun 16 '06 #1
Share this Question
Share on Google+
6 Replies


Banfa
Expert Mod 5K+
P: 8,916
Unfortunately you have left out the most important piece of information which is the declarations of the class PipeElementClass and the variable pipeModel.

I will guess that pipeModel is

vector<PipeElementClass> pipeModel;

However the declaration of PipeElementClass is really important because if pipeModel is a member of PipeElementClass then each PipeElementClass instance will have it's own instance of pipeModel and each instance will only contain 1 entry. Unless of course you have declared it static which is why we need the defintion.

If possible (i.e. if your code is not 10000s lines in multiple files) post a compilable example of the problem.
Jun 16 '06 #2

P: 27
using namespace std;

class PipeElementClass : public CWnd
{

// Construction
public:
PipeElementClass();
PipeElementClass(const PipeElementClass& pipeElem);
void AddPipe(PipeElementClass pipeElem);
bool operator==(const PipeElementClass& x);
PipeElementClass operator=(const PipeElementClass& x);

// Attributes
public:
vector<PipeElementClass> pipeModel;
};



pipeModel is a member of PipeElementClass, I see the problem know you mention it Banfa. Though i'm not sure how to get around it, I put the vector in this class as it is to contain PipeElementClass objects.
Jun 19 '06 #3

Banfa
Expert Mod 5K+
P: 8,916
pipeModel is a member of PipeElementClass, I see the problem know you mention it Banfa. Though i'm not sure how to get around it, I put the vector in this class as it is to contain PipeElementClass objects.
Sorry about the delay. The problem is easily solved, instead of 1 vector per instantiated object you want 1 vector for the entire class. Giving it the static storage specifier will achieve this.

You may then want to make the method AddPipe static as well, a static method is one that only accesses static data members, or rather that doesn't access any non-static data members.

When you make a data member static you then also need to declare it in the class cpp file like so

vector<PipeElementClass> PipeElementClass::pipeModel;


Hope this gets you going because I have to get to bed now but post again if you need more help.
Jun 19 '06 #4

P: 27
That works great, thanks Banfa!

I have a problem though, the way I have done it (with the vector being a member of PipeElementClass and storing PipeElementClass objects) it means that every member of the vector pipeModel will contain another vector (again pipeModel) containing PipeElementClass objects which will also contain another vector and so on...
To get around this I was thinking about putting the vector in another class, but I would then have problems with overloading the operator functions (=, ==) as the implicit parameter (this) is no longer a PipeElementClass - and cant be passed more than one parameter if i'm not mistaken? Any thoughts on how I would overcome this?

Again any help would be much appreciated.
Jun 20 '06 #5

Banfa
Expert Mod 5K+
P: 8,916
I have a problem though, the way I have done it (with the vector being a member of PipeElementClass and storing PipeElementClass objects) it means that every member of the vector pipeModel will contain another vector (again pipeModel) containing PipeElementClass objects which will also contain another vector and so on...
To get around this I was thinking about putting the vector in another class, but I would then have problems with overloading the operator functions (=, ==) as the implicit parameter (this) is no longer a PipeElementClass - and cant be passed more than one parameter if i'm not mistaken? Any thoughts on how I would overcome this?
lol, no you had that problem before you made the vector static :)

As you surmised every time you added an element to the vector that element itself contained another vector, although this vector had the potential to contain more objects and vectors in practice I imagine it would have 0 length and not contain any more objects and vectors. I did think of mentioning this in my last post.

However you have made the vector pipeModel static, this means that there is only 1 copy of the vector in memory and that all instances of the class use this 1 instance. So when you are adding elements to this vector you are not creating more vectors.

Because the vector is static you do not even need an instance of the class to access it you can always access it as

Expand|Select|Wrap|Line Numbers
  1. PipeElementClass::pipeModel.clear();
  2.  
for instance.

You don't have to put the vector in another class, you could just declare it outside the class and a normal static variable, I'm not sure of the form (i.e. good or bad form) of doing this as opposed to having the vector as a static class member as I am mainly a C rather than C++ programmer.

You shouldn't get any problems with your = and == operator overrides as they are nothing to do with the vector. Just leave them as part of the PipeElementClass if you where to do this.
Jun 20 '06 #6

P: 27
Cheers Banfa,

Got it now, i'm just new to this programming carry on!

Thanks for all your help.
Jun 20 '06 #7

Post your reply

Sign in to post your reply or Sign up for a free account.