STL vector

Hello,
I do my first steps with the stl and so i star with the famous vector.

I created a class who contain a vector. The vector can be use by class's functions. I use virtual studio 2008.

example:

class Data
{ public: "constructors, destructor and operators..."
void test (char * data_tmp)
{ for ( int i = 0; i < data_tmp_max; i++ )
{
this->vdata.push_back( data_tmp[i] );
}
}

private:
int data_type, data_size_max, data_loaded, num;
std::vector<char> vdata;
}

i know i can use others functions for load a tab but it s test example and this doesn't function: Unhandled exception ... Microsoft C++ exception: std::bad_alloc at memory location 0x0053ed68..

Somebody for some idea? :)
Maybe i must define a std::vector<char *> vdata; ????
Do you want create a string class?

And it will be great if you attach a full code with main function. It gives opportunity to debug your problem.
I cann t give you all code, so heavy. But the program bug immediately in the first push.
The thing very strange, it s if i do a push in the constructor, this function Oo.

----main
include"data.h"

int main (void)
{
Data d;
d.add_a();

return 0;
}

---data.h
class Data
{ public:
Data()
{ //std::cout << "Constructor 1\n"<< std::endl;
this->vdata.push_back('b' );
}
~Data()
{ //std::cout << "destructor\n"<< std::endl;
}

Data(const Data & aData)
{ //std::cout << std::endl << "copie"<< std::endl;
this->vdata=aData.Getvdata();
}
Data & operator=(const Data &aData)
{ //std::cout << "operator=\n"<< std::endl;
if (this != &aData)
{
this->vdata=aData.Getvdata();
}
return *this;
}

void add_a (void)
{
this->vdata.push_back( 'a' );
}

private:
std::vector<char> vdata;
}

I really don t understand why the push in the constructor fonctions and not the second contained in add_a().


Other point very strange if i do:
int main (void)
{
Data *d= new Data();
d->add_a();

delete d;

return 0;
}

again std::bad_alloc at memory location but immediately during the constructor's launch Oo
Last edited on
At a quick glance at your code, I see that there is no semicolon after class definition. I hope you have put it in your original code.
Last edited on
Topic archived. No new replies allowed.