# STL Vectors, and classes

This topic is 4828 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've just run into this problem with my Map Editor. It uses my own format called .ohm. I am trying to add support for loading models and placing them in the world, but i have a problem with using a certain class in my vector. NOTE: This source is not in one file, it is a collection of relevant code.
struct OHMHEADER
{
char MapName[100];
float YScale;
char Texture1[80];
char Texture2[80];
char Texture3[80];
char Texture4[80];
char WaterTexture[80];
float WaterLevel;
int Step;
std::vector<CObjModel> Models;
BYTE *data;

};

//.............

class CObjModel
{
public:
int numVerts, numUV, numNormals, numFaces;
bool Triangles;
std::vector <CVector3> Verts;
std::vector <CVector3> Normals;
std::vector <CVector3> UVs;
std::vector <CIndex3> Verticies;

char * FileName;

CObjModel();
void Draw();
};

//.............

{
char FileName[80];
CObjModel tempModel;
break;
ohm.Models.push_back(tempModel);
break;
}

//..............

int DrawGLScene(GLvoid)								// Here's Where We Do All The Drawing
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);			// Clear The Screen And The Depth Buffer

if(keys[VK_LEFT])
rY-=1;
if(keys[VK_RIGHT])
rY+=1;
if(keys[VK_UP])
rX-=1;
if(keys[VK_DOWN])
rX+=1;
zoom+=0.1;
if(keys[VK_SUBTRACT])
zoom-=0.1;
glTranslatef(0,0,zoom);
glRotatef(rX,1,0,0);
glRotatef(rY,0,1,0);
glScalef(0.015,0.015,0.015);
{
DrawHeightMap(&ohm,gSolid);
DrawSea(&ohm);

level4.Bind();
//Pipe.Draw();
for(int I=0;I<ohm.Models.size();I++)
{
ohm.Models.Draw();
}

}



...That says?

##### Share on other sites
I think I have a very similar problem, but it seems to be for other STL objects, like a map too.

##### Share on other sites
What both of you probably need is to create proper copy constructor and op= for your classes I can see a few bald pointers hanging around and in this case that makes me suspect that you simply free your resources in the destructor but don't clone them properly when copying leading to both double deletions and access failiures when you try to read memory that you no longer own.

For strings I advice you to use std::string instead of char* since it will solve that and a slew of ofter problems without you needing to think about it again. For byte data why not consider std::vector<BYTE> as it does automatic memory handling also for you.

Hope that was of some assistance.

##### Share on other sites
One solution is pointers.

Kuphryn

##### Share on other sites
Quote:
 Original post by DigitalDelusionWhat both of you probably need is to create proper copy constructor and op= for your classes I can see a few bald pointers hanging around and in this case that makes me suspect that you simply free your resources in the destructor but don't clone them properly when copying leading to both double deletions and access failiures when you try to read memory that you no longer own.For strings I advice you to use std::string instead of char* since it will solve that and a slew of ofter problems without you needing to think about it again. For byte data why not consider std::vector as it does automatic memory handling also for you.Hope that was of some assistance.

I have barely used any pointers in that. However, do i need to delete a char* pointer at the end of the program?

I use no constructors or destructors as there was no use for them IMO.

As for this copying stuff, i dont seem to have caught your drift.

##### Share on other sites
You sure you're not accessing out of bounds on one any array or such?

##### Share on other sites
Which class is the problem?

Does that class have pointers in them?

If so then you should have destructors to delete the data from the heap.

Are they base classes. If so then you need virtual destructors.

If you are allocating heap memory then you should really have copy constructors..you should also have assignment constructors as well. The rule of 3 (or something like that) it's called.

Are you "slicing" one of the objects? Assigning a derived class to a base class.

To be honest it's a little difficult without seeing the error memssage and the other classes?

##### Share on other sites
out of interest, is it possible to call functions from a class in a vector?

##### Share on other sites
Quote:
 Original post by Kris2456out of interest, is it possible to call functions from a class in a vector?

You mean like:

#include <iostream> //for cout#include <vector> //for vector#include <algorithm> //for for_each#include <functional> //for mem_fun_refclass foo {public:    int i;    foo( int i ) : i( i ) {}    foo( const foo & other ) : i( other.i ) {}    void saymynumber( void ) {        std::cout << "My number is " << i << std::endl;    }};std::vector< foo > foos;foos.push_back( foo( 5 ) );foos.push_back( foo( 3 ) );foos.push_back( foo( 4 ) );//calls foo.saymynumber() where foo is the 3rd foo in the vector  :foos[2].saymynumber();//calls foo.saymynumber() where foo is the foo pointed to by the iterator i (in this case the 2nd foo in the vector)std::vector< foo >::iterator i = foos.begin(); ++i;i->saymynumber();//calls foo.saymynumber for foo = *[ foos.begin() .. foos.end() )  - that is, once on every foo in the vector  :std::for_each( foos.begin() , foos.end() , std::mem_fun_ref( &foo::saymynumber ) );

In a word? Yes :-).

EDIT: I need to learn to type and generally use my brain.

1. 1
2. 2
Rutin
21
3. 3
4. 4
frob
14
5. 5

• 12
• 9
• 17
• 19
• 9
• ### Forum Statistics

• Total Topics
632598
• Total Posts
3007333

×