vector declare in header file

I am trying to declare a vector in my header file.

Here is what is in my header file (yes, i used #include <vector>)

1
2
public:
    std:vector<int> testV;


Wonderful. Here's what is in my cpp:

 
testV.push_back(99);


Yay! It compiles, but then my program crashes when I include that last line, and doesn't crash when I don't include it.

How great!

I even try this above it:

1
2
size_t vSize = 10;
std::vector<int> testV(vSize);


or

 
std::vector<int> testV;


or

 
testV.resize(10);


All super fun, and some compile, but then it crashes. <:-D

Ideas!
I'll need to see some more code. Specifically, one of the versions that compile but crash.
Last edited on
hello,
I am a novice in vector (in c++), but I want to try to help you.
I looked exemples on this site and I saw that the vector's initializers don't contain "std::", but there is "using namespace std". And in your declaration (it may be a error of type), you wrote one ":" and not two "::".

Try to test a simple file (.cpp) with a simple declaration (vector<int> testV;) and a simple function (testV.push_back(99);) to see if the error is in an other place.

That is syntax and won't help.

We need to see the code. The problem is not in the vector::push_back(), it must be elsewhere.
Also, just a small note, I think push_back will append elements beyond the current size rather than filling it in. Consider either omitting the resize reserve call or using assign to populate them rather than push_back.

EDIT: Thanks helios.
Last edited on
Also, just a small note, I think push_back will append elements beyond the reserve rather than filling it in.
If I understood what you said, replace "reserve" with "resize", and that sentence is correct.
Thanks all for responses.

(I was using ::, not :, I just typed it in wrong)

As for including all of the code, I'm actually building on the BWSAL code base, so I don't think I could post it all here. Basically, it's found here: http://code.google.com/p/bwsal/

I'm editing BasicAIModule.cpp and BasicAIModule.h

I have only added the above lines regarding vectors, as I need to get that working before I proceed. I would like to point out that I am able to use "testV[5] = 99" -- so I am able to alter the vector somehow.

Can you give me an example of how I'd use resize to do this? That's unfortunate, because I thought push_back would automatically resize if there's not enough room.

If you think I should post all the code here, I can do that

Thanks again for all your responses
I thought push_back would automatically resize if there's not enough room
It does.

Are you by any chance using threads?
I honestly don't know, although it is possible. Is there an easy way I could check to find out?

Sorry, not used to oo programming at all, as you can probably tell.
BTW, here is a link to the two files:

Header->
http://code.google.com/p/bwsal/source/browse/trunk/BWSAL/BasicAIModule/BasicAIModule/BasicAIModule.h?r=50

Cpp->
http://code.google.com/p/bwsal/source/browse/trunk/BWSAL/BasicAIModule/BasicAIModule/BasicAIModule.cpp?r=50

I'm trying to use "testV.push_back(99)" in "void BasicAIModule::onFrame()" (this is run on every tick of starcraft as it moves along)

"void BasicAIModule::onStart()" is where I typically initialize variables that I've defined in the header... although of course this isn't working for vectors for some reason
http://code.google.com/p/bwsal/source/browse/trunk/BWSAL/BasicAIModule/Source/BasicAIModule.cpp
At first glance, it'd seem this source is not expected to run on different threads.

Sorry, but I can't give a definite answer without looking at the code that crashes.
Last edited on
vector in stl is a dinamic vector y resize automatilyc. You dont need resizce

so what i'll do :

example.cpp
#include <iostream>

using namespace std;
class Example{
vector <int> numbers;
public:
example():numbers();
~example();

main.cpp
#include <iostream>
#include "example.h"
using namespace std;
int main(){

example testV;
testV.push_back(99);


}

And realy its works
did you mean the first to be "example.h" instead of "example.cpp" ?

if so, where would I put this in the following example: http://code.google.com/p/bwsal/source/browse/trunk/BWSAL/BasicAIModule/BasicAIModule/BasicAIModule.h?r=50

or did you mean it to be a separate .cpp file?

sorry, a bit confused. if you could tell me where these would go in terms of the following header/cpp that would be awesome:

Header->
http://code.google.com/p/bwsal/source/browse/trunk/BWSAL/BasicAIModule/BasicAIModule/BasicAIModule.h?r=50

Cpp->
http://code.google.com/p/bwsal/source/browse/trunk/BWSAL/BasicAIModule/BasicAIModule/BasicAIModule.cpp?r=50
You are missing the point. It really makes no difference whether you place the class declaration within the header. Since this forum doesn't allow attachments most people write a complete, compilable example in one code block that can be copied into a post. That is what you need to do.

In these forums it is very difficult to debug a large scale application. We simply can't do that for you. What you need to do for yourself is to create a toy program that is very simple and attempt to duplicate the problem. In doing this you will learn that the push_back is really not the problem at all. In fact if you do that first you will probably realize this on your own and then posting a question isn't necessary unless the toy program crashes. If you can duplicate the problem within a smaller example then post that and ask a question and we can take a look at it and explain to you where you went wrong. jecaestevez was giving you an example of what you need to attempt to do on your own in order to duplicate the problem. It would be nice if jecaestevez would edit the post and add in some tags so that it is more readable but I appreciate what he or she was attempting to show you.
in the header file, there is already a class, so i just put what you wrote inside of that:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#pragma once
#include <BWAPI.h>
#include <BWTA.h>
#include <Arbitrator.h>
#include <WorkerManager.h>
#include <SupplyManager.h>
#include <BuildManager.h>
#include <BuildOrderManager.h>
#include <TechManager.h>
#include <UpgradeManager.h>
#include <BaseManager.h>
#include <ScoutManager.h>
#include <DefenseManager.h>
#include <InformationManager.h>
#include <BorderManager.h>
#include <UnitGroupManager.h>
#include <EnhancedUI.h>
#include <math.h>
#include <vector>
#include <iostream>

using namespace std;

class BasicAIModule : public BWAPI::AIModule
{
	vector<int> numbers;
public:
	example():numbers();
	~example();
	virtual void onStart();
	virtual void onEnd(bool isWinner);
	virtual void onFrame();
	virtual void onUnitShow(BWAPI::Unit* unit);
	virtual void onUnitHide(BWAPI::Unit* unit);
	virtual void onUnitMorph(BWAPI::Unit* unit);
	virtual void onUnitRenegade(BWAPI::Unit* unit);
	virtual void onUnitDestroy(BWAPI::Unit* unit);
	virtual bool onSendText(std::string text);
	~BasicAIModule(); //not part of BWAPI::AIModule
	void showStats(); //not part of BWAPI::AIModule
	void showPlayers();
	void showForces();
	bool analyzed;
	std::map<BWAPI::Unit*,BWAPI::UnitType> buildings;
	Arbitrator::Arbitrator<BWAPI::Unit*,double> arbitrator;
	WorkerManager* workerManager;
	SupplyManager* supplyManager;
	BuildManager* buildManager;
	TechManager* techManager;
	UpgradeManager* upgradeManager;
	BaseManager* baseManager;
	ScoutManager* scoutManager;
	BuildOrderManager* buildOrderManager;
	DefenseManager* defenseManager;
	InformationManager* informationManager;
	BorderManager* borderManager;
	UnitGroupManager* unitGroupManager;
	EnhancedUI* enhancedUI;
	bool showManagerAssignments;

	int T; //current tick

};


but it gives a compile error at: "example():numbers();" --

"error C2590: 'example' : only a constructor can have a base/member initializer list"
You are missing the point. It really makes no difference whether you place the class declaration within the header. Since this forum doesn't allow attachments most people write a complete, compilable example in one code block that can be copied into a post. That is what you need to do.


Thanks for the response, I am certain that I'm missing the point because I really don't know much about oo programming. I understand all your points, but I do not have the know-how to create a toy program. C++ confuses me, and I wish I didn't have to use it, but here I am. I just want to get vectors working within this framework, and the rest should fall into place for me. I'm not trying to make something overly complex, I just need to add some functionality to this program.

I just want to make sure that I get the syntax right so that I know whether or not something is going on in bwapi that is keeping me from using vectors properly.

In my main cpp, I am using:

1
2
3
4
5
6
7
8
	class Example{
		std::vector<int> numbers;
	public:
		example():numbers();
		~example();
	}

	example testV;


However, I get: " error C2590: 'example' : only a constructor can have a base/member initializer list" at "example():numbers();"

I appreciate jecaestevez's post, but if someone could clean it up for me then that would be amazing
example():numbers();
If you really want, I can tell you what's wrong with this line, but but the fact is (if we ignore that the class isn't called 'example'), having the ":numbers()" part doesn't help you at all and doesn't add anything that the language isn't already implicitly doing for you.
Furthermore, I'd suggest you ignore everything jecaestevez said.

The real problem has probably nothing to do with syntax, otherwise, the program just wouldn't compile. There's something more complex at play, here. Standard vectors don't get corrupted just like that. At the very least, there has to be some memory corruption or race conditions going on, or reliance on undefined behavior. The latter seems unlikely if this is code inside a plugin library.

EDIT: If you didn't modify too much of the code, I'd suggest asking the maintainers of http://code.google.com/p/bwsal/ for hints about what could be causing the corruption.
Last edited on
jecaestevez was just pointing out what I did which is that you can make a small example and test the vector's interface in order to prove to yourself that your method of manipulating the vector is not the fundamental problem with your program. Therefore I believe that he made a valid point. For all I know there could be some place within the program where you are doing something strange to the vector such as using an out of bound index on the operatotr[]. Do you ever use operator[] to access elements of the vector object? Helios' point is equally valid that your program is so complex that there could be many other problems causing undefined behavior and the code happens to stop working at the push_back call. That does not mean that there is anything wrong with that part of the code but that some thing else was corrupted along the way and that is where the program happened to stop working.

You could start posting the body of some of the functions where the vector object is manipulated. I really don't know what else to tell you. Someone might be able to eyeball it and notice something suspicious.
Topic archived. No new replies allowed.