473,508 Members | 2,053 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Vector used inside class definition appears to become static

2 New Member
Here is my code (abstracted from the original problem):

Expand|Select|Wrap|Line Numbers
  1.  #include <c:\djgpp\include\stdio.h> 
  2. #include <c:\djgpp\include\stdlib.h>
  3. #include <c:\djgpp\include\string.h>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. class PhrasePool{
  8. public:
  9. vector<int> num;
  10. PhrasePool(){
  11. num[0]=1;
  12. }
  13. };
  14.  
  15. int main(){
  16. PhrasePool TempPool,TempPool2;
  17. for(int x=1;x<10;x++){
  18. TempPool.num[x]=x;
  19. }
  20. for(int x=1;x<10;x++){
  21. TempPool2.num[x]=10;
  22. }
  23. for(int x=0;x<10;x++){
  24. printf("%d",TempPool.num[x]);
  25. }
  26. return 0;
  27. }
  28.  
I find when I run this program that I get the following output:
1101010101010101010

When I expect to get:
1123456789

Essentially I expect to have an individual copy of a vector of integers called num in each of TempPool and TempPool2. However it appears that I have just one shared vector of integers much as if I had declared the vector static.

Any clues as to why this is happening and/or how to fix the code so that it behaves as expected?

Thanks in advance to anyone kind enough to help.
Feb 9 '06 #1
1 4754
Barnaby Dawson
2 New Member
A friend of mine has got back to me on this problem so I thought I'd post his answer here for the benefit of readers of this forum (and so others don't spend time telling me what I already know).

He says that num[x]=x is incorrect because the size of num is not known to be greater than x (indeed is not in this example). So instead I should use num.push_back(x) or resize the vectors ahead of time. I changed the code to use push_back(x) and it worked:

#include <c:\djgpp\include\stdio.h>
#include <c:\djgpp\include\stdlib.h>
#include <c:\djgpp\include\string.h>
#include <vector>
using namespace std;

int grabInt();
int phraseGrabInt(char input[],int pos);

FILE *filey;

class PhrasePool{
public:
vector<int> num;
PhrasePool(){
num.push_back(1);
}
};

int main(){
PhrasePool TempPool,TempPool2;
for(int x=1;x<10;x++){
TempPool.num.push_back(x);
}
for(int x=1;x<10;x++){
TempPool2.num.push_back(10);
}
for(int x=0;x<10;x++){
printf("%d",TempPool.num[x]);
}
return 0;
}

Presumably until the vectors had anything in them the [] operator gave the same locations in memory for both instances of the class (when looking beyond the actual end of the vector). There happened not to be anything using that location in memory so I got away with using it. When I wrote name.num[x] it accessed the same piece of memory whichever object it was within. Thats why it appeared to be static. The problems went away when I accessed the vectors in a valid way.
Feb 10 '06 #2

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

Similar topics

7
3696
by: Martin Magnusson | last post by:
I'm having trouble clearing and resizing a static std::vector of std::vectors (segmentation fault). Is it OK to call clear() and resize() on a static attribute? My code is similar to the one posted...
4
4872
by: Serge | last post by:
Hi, I have no problem creating a static member variable with integers, etc but when I try the same with a vector then I always get linker errors that the static member variable is unknown...
17
3326
by: Michael Hopkins | last post by:
Hi all I want to create a std::vector that goes from 1 to n instead of 0 to n-1. The only change this will have is in loops and when the vector returns positions of elements etc. I am calling...
8
5008
by: He Shiming | last post by:
Hi, I've developed a class that implements an interface definition. It looks like this: class IRecord { public: // define interface methods by pure virtual methods // no member variables }
16
4408
by: Martin Jørgensen | last post by:
Hi, I get this using g++: main.cpp:9: error: new types may not be defined in a return type main.cpp:9: note: (perhaps a semicolon is missing after the definition of 'vector') main.cpp:9:...
13
2286
by: Gernot Frisch | last post by:
Which method is the fastest/best: std::vector<intfoo1() { std::vector<intv; ... reutun v; } std::vector<int>& foo2()
2
1497
by: robertoviperbr | last post by:
Hello to everybody. I have a doubt see the code: #include <vector> #include <iostream> #include <string> #include <algorithm> #include <sstream> #include <fstream> #include <iomanip>
6
5605
by: Bobrick | last post by:
Hi. Thanks to everyone who replied to my last post, it turns out it wasn't the line where I was trying to treat the variable in question as an array which was the problem, but the line above. ...
15
7839
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
0
7226
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
7388
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...
1
7049
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5631
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5055
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3186
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1561
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
767
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
422
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.