473,569 Members | 2,879 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:
110101010101010 1010

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 4758
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\inclu de\stdio.h>
#include <c:\djgpp\inclu de\stdlib.h>
#include <c:\djgpp\inclu de\string.h>
#include <vector>
using namespace std;

int grabInt();
int phraseGrabInt(c har input[],int pos);

FILE *filey;

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

int main(){
PhrasePool TempPool,TempPo ol2;
for(int x=1;x<10;x++){
TempPool.num.pu sh_back(x);
}
for(int x=1;x<10;x++){
TempPool2.num.p ush_back(10);
}
for(int x=0;x<10;x++){
printf("%d",Tem pPool.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
3703
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 below. The code below works, though. My original code crashes on the second call to X::f(). My question is: is this code valid, or is it just a...
4
4885
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 (unresolved external symbol) Below is what an example of the code I use. Can somebody tell me what I am doing wrong ? Thanks very much in advance, Serge ...
17
3338
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 this uovec at the moment (for Unit-Offset VECtor). I want the class to respond correctly to all usage of STL containers and algorithms so that it is...
8
5016
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
4425
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: error: two or more data types in declaration of 'set' I don't really see the problem... Here's the code:
13
2301
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
1508
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
5614
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. char temp; std::vector<unsigned charmmessage; while (!done){
15
7845
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
7703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7619
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7930
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5514
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5228
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3662
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2118
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 we have to send another system
1
1229
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.