problem using vector

Started by
12 comments, last by nobodynews 17 years ago
I recently tried to alter some code from using arrays to using vectors. Everything I've read seemed like it would be easy enough, but I've run into a few problems.

#include <vector>

vector<int> v1;
vector v2;

Given the "source" above, I have two different problems. (And yes, I know that the vector v2 is incorrect, but it shows some info that seems peculiar). When I use the v1 version, I cannot access any functions of the vector. Using VC++6, I don't even get the list of functions available when typing "v1." that usually pops up automatically. If I leave out the type, the list pops up as normal, but with the obvious problem of not having any type set. Can anyone see what I might be doing wrong? If needed, I will supply full source code...
Advertisement
Are you doing:

using namespace std;

or
using std::vector;

?

If yes then paste the full code. Also, the intellisense for VS6.0 was pretty wobbley at times, a better test of correct syntax is whether it compiles.

Dave
No, I'm not. although the above snippet isn't copied from my source code, it's exactly what my source code has in it. (minus the v2 line).

I did try to create a new project, a simple "Hello World" console app, and included vector there in the same way I am in my current project, and it works fine there.

sorry, slight edit. Just noticed something else...
//When I type it like this....vector<int> v1;v1.push_back(4);//it works fine while designing, but won't compile.  when I do this...std::vector<int> v1;v1.push_back(4);//I get the above problems, but the program compiles fine.
Quote:Original post by Dave
If yes then paste the full code. Also, the intellisense for VS6.0 was pretty wobbley at times, a better test of correct syntax is whether it compiles.


Yeah, I was just going to say this.

Does your code compile? If it compiles it's just a problem with VS6.0 (which is ungodly horrible). There's not really anything you can do about that, it's an old piece of crap IDE. you should upgrade to Visual Studio 2005 (you can download the Express edition for free from MS).

-me

I should have mentioned that doing either of the 2 lines i coded in my previous post means that you don't need to put std:: on the front of vector. However not doing either of the lines i mentioned means that the second piece of code in your last post is the valid way of doing it, the first is wrong.

Dave
you know you don't have to use intellisense. I never use it. I like typing out the whole function name. As long as your code compiles and works does it really matter what intellisense does?
Quote:Original post by trick
Using VC++6, ...

That's your problem.

Seriously, that compiler is obsolete. It has problems with templates and has issues with the C++ standard. There is no reason for anyone to be using it. I recommend that you switch to any current compiler. If you like Visual Studio, then try Visual Studio 2005 Express Edition.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Looking into upgrading to Express, just trying to go through the downloads for .net framework, or whatever it needs....

full source
#include "stdafx.h"#include <vector>int main(int argc, char* argv[]){	std::vector<std::string> v1;	v1.push_back("Hello World");	printf(v1[0].c_str());	return 0;};


I'm not so much worried about the intellisence, and the above snippet works (though the intellisence doesn't work). It seems I'm just using vector wrong.

I was trying to use it to store pointers to structures. A seperate function would do...
//vec1 created outside of function, with vector<myStruct*> vec1myStruct* t1;t1 = new myStruct;vec1.push_back(t1);


My understanding was, since the object was created with new, it would have no problem being added and not going out of scope at the end of the function. (Until I manually use delete to remove the item). Is my understanding wrong?

And also, thanks for the help, and quick response!
The vector stores pointers to the data here so all the vector clears up here is the pointers, leaving the memory floating. You need to iterate through the vector and delete what each pointer points to and then call erase() on the vector to clean that up.

In your example the pointer t1 will go out of scope and be destroyed, but you will still have the data allocated by new and that won't be cleaned up. You also won't lose the data because a copy of the pointer is made when you added it to the vector. All it fine here.

Dave
Snippets from my actual code (not the test program i posted earlier).
void ZTemplateSet3::load(LPDIRECT3DDEVICE9 pd3dDevice, std::string name, std::string ext){	cleanup();	std::string file;	file = name;	name.append(ext);	load_img(pd3dDevice, file, ext);	//continue loading here...	file = name;	file.append(".zt3");	fstream xFile;	xFile.open(file.c_str(), ios::in);	int i, j, k;	float p;	int w, x, y, z;	ZTemplate3* t1;	ZTemplateGroup3* t2;	ZTemplateIcon3* t3;	xFile >> i;	templates.reserve(i);	for(int a = 0; a < i; a++){		t1 = new ZTemplate3;		xFile >> j;		templates.groups.reserve(j);		for(int b = 0; b < j; b++){			t2 = new ZTemplateGroup3;			xFile >> k;			templates->groups[j]->icons.reserve(k);			for(int c = 0; c < k; c++){				t3 = new ZTemplateIcon3;				xFile >> w >> x >> y >> z;				xFile >> p;				t3->anim_rate = p;				t3->icon.left = w;				t3->icon.right = w + y;				t3->icon.top = x;				t3->icon.bottom = x + z;				t2->icons.push_back(t3);			}			t1->groups.push_back(t2);		}		templates.push_back(t1);	}	xFile.close();	active = true;}int ZTemplateSet3::add_template(ZTemplate3* newtemplate){	templates.push_back(newtemplate);	active = true;	return templates.size();}//similar functions for adding groups, and iconsvoid ZTemplateSet3::cleanup(){	if(!active)		return;	ZTemplate3* t1;	ZTemplateGroup3* t2;	ZTemplateIcon3* t3;	for(int i = templates.size()-1; i >= 0; i++){		for(int j = templates->groups.size()-1; j >= 0; j++){			for(int k = templates->groups[j]->icons.size()-1; k >= 0; k++){				t3 = templates->groups[j]->icons[k];				templates->groups[j]->icons.pop_back();				delete t3;			}			t2 = templates->groups[j];			templates->groups.pop_back();			delete t3;		}		t1 = templates;		templates.pop_back();		delete t1;	}	active = false;}


EDIT: After this, if I say int num = templates.size(), I get something like -872541 (just some big negative number...)

This topic is closed to new replies.

Advertisement