Hi,

I am still experimenting with STL trying to weigh up the pros and cons. I wish to define nested/embedded classes i.e.

typedef std::vector vector <class B> bVecArray;
typedef std::vector vector<std::string> sVecArray;
class A{

bVecArray bObjArray;
std::srting curLine // current line in dataFile

...

};

class B{

sVecArray header;

};

The class A is used to parse a file containing data which is sliced into different "chunks" using the bObjArray. The data file contains a main header followed by subheaders for each data chunk, and a data-chunk footer

when opening a data file I create an A object

A::A(char *dataFile....)

I can successfully create b objects using bObjArray.push_back(B constructor). However I am having difficulty coding up so that the relevant "chunk" header is written to the current (most recently created) b Object

i.e. I need something like

bObjArray.at(currentBObject).header.push_back(currentLineInFile)

- does not compile

The following does compile

a.temp_header.push_back(curLine);

Clearly something is wrong with above?

Would it be better to use an iterator for the currentBObject?

Or should I first write a temporary header object in A and copy it to the current bObject, and subsequently clear the temporary object? This seems horribly inefficient-

Thanks for taking the time to read this

Mark

Recommended Answers

All 2 Replies

std::vector has a back() method that returns a reference to the last element. You are using push_back() to add elements, so the last element will be the most recent.

bObjArray.back().header.push_back(currentLineInFile);

at() should work too, as long as the index is bObjArray.size()-1. Edward would need more code to say why it does not work for you. Can you post a complete program that fails?

>>typedef std::vector vector <class B> bVecArray

is that supposed to be :

typedef std::vector< vector <class B> >bVecArray
//or
typedef std::vector<class B> bVecArray
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.