473,385 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Struggling with vectors

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
6 8126
Banfa
9,065 Expert Mod 8TB
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
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
9,065 Expert Mod 8TB
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
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
9,065 Expert Mod 8TB
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
Cheers Banfa,

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

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

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

Similar topics

10
by: Michael Aramini | last post by:
I need to represent 1D and 2D arrays of numeric or bool types in a C++ program. The sizes of the arrays in my intended application are dynamic in the sense that they are not known at compile time,...
5
by: Pratyush | last post by:
Hi, Suppose there is a vector of objects of class A, i.e., std::vector<A> vec_A(N); The class A satisifies all the STL vector requirements. Now I wish to add some attributes for each of the...
5
by: Computer Whizz | last post by:
I was reading through Accelerated C++ at work when I read through the first mention of Vectors, giving us certain functions etc. Is there any benefit of Arrays over Vectors? Since all Vectors...
3
by: Amit | last post by:
Hello. I am having some problem organizing a set of vectors. The vectors itself, could contain a pointer( say integer pointer) or could contain another object MyClass. 1>So, first of all, is...
4
by: Dr. J.K. Becker | last post by:
Hi all, I have vectors that holds pointers to other vectors, like so: vector<whatever> x; vector<whatever*> z; z=&x; Now I add something to x
5
by: madhu | last post by:
http://msdn2.microsoft.com/en-us/library/fs5a18ce(VS.80).aspx vector <intv1; v1.push_back( 10 ); //adds 10 to the tail v1.push_back( 20 ); //adds 20 to the tail cout << "The size of v1 is " <<...
2
by: wuzertheloser | last post by:
Use the program skeleton below (starting with #include <stdio.h>) as the starting point for quiz4. Add the necessary code to the functions prob1() and prob2(), and add the other 2 functions, as...
1
by: Rob | last post by:
How would I do this? I want to be able to handle vectors of many different types of data and vectors that can contain any number of other vectors of data. Currently, I have a templated...
2
by: joeme | last post by:
How would one using STL do the following tasks: 1) merge 2 sorted vectors with dupes, result shall be sorted 2) merge 2 sorted vectors without dupes, result shall be sorted 3) merge 2...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.