Jump to content
  • Advertisement
Sign in to follow this  
silverphyre673

I'm asking a pointer question! Run!

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm creating a custom iterator class for one of my own containers for a terrain engine. Think of it as a vector of vectors, something I've been posting a lot about lately :) It looks something like this:
class TerrainMesh
{
    public:
    friend class const_iterator;
    struct const_iterator
    {
        //Functions!
    };
    std::vector<std::vector<TerrainCell> > mesh;
};
I'm trying to make TerrainMesh::const_iterator iterator through each "horizontal" value of each "row" of "mesh". I basically figured that I would have to give it a pointer to the original class, probably through a TerrainMesh.begin() function. So I did this:
struct const_iterator
{
    std::vector<std::vector<TerrainCell> > * mesh_ptr;
};

TerrainMesh::const_iterator::operator = (const TerrainMesh & parent)
{
    *mesh_ptr = &parent.mesh.begin();
    //etc.
}

Obviously I don't want to copy a huge vector of vectors into each iterator, so I tried ust making mesh_ptr a pointer to the parent's mesh... it keeps giving me this warning: [Warning] taking address of temporary //I thought it wasn't temporary since //I'm passing by reference! no match for 'operator=' in '*((TerrainMesh::const_iterator*)this)->Mesh::const_iterator::mesh_ptr = &((const std::vector<std::vector<TerrainCell, std::allocator<TerrainCell> >, std::allocator<std::vector<TerrainCell, std::allocator<TerrainCell> > > >*)parent)->std::vector<_Tp, _Alloc>::begin [with _Tp = std::vector<TerrainCell, std::allocator<TerrainCell> >, _Alloc = std::allocator<std::vector<TerrainCell, std::allocator<TerrainCell> > >]()' note C:\Dev-Cpp\include\c++\3.4.2\bits\vector.tcc:131 candidates are: std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(const std::vector<_Tp, _Alloc>&) [with _Tp = std::vector<TerrainCell, std::allocator<TerrainCell> >, _Alloc = std::allocator<std::vector<TerrainCell, std::allocator<TerrainCell> > >] //Aaack! Big! I don't know what the problem is! Anyways, any help you can give on how I should be making this iterator would be great! Thanks, goodnight!

Share this post


Link to post
Share on other sites
Advertisement
Tell me: why are you making things so hard on yourself? Why not just use a one-dimensional vector?

vec[y*width + x] is your friend.

Share this post


Link to post
Share on other sites
Your problem, BTW, is that you are defining a member function that was never declared in the class body. But your REAL problem is that you are using nested vectors for a situation that would be better handled with a regular vector.

Share this post


Link to post
Share on other sites
Okay, I don't know the answer to the question, mainly because I'm not up on my iterator knowledge, and personally, I hate compile errors with templated classes. It takes five minutes to read 2-3 lines and realise that its talking about a single map object, or something.

Anyway, I was wondering, what was the point of the "friend class const_iterator;" if you're defining a const_iterator struct inside the class. You'll be able to access it all anyway. Right?

Share this post


Link to post
Share on other sites
Fine, yes. I will make a one-dimensional vector, master. I'll tell you if it works better. But the question will remain: if I want to make an iterator for it, how will I get a pointer to the vector for my iterator, so I can do iterator++? Thanks.

Share this post


Link to post
Share on other sites
Well, of course, a one-dimensional vector has an iterator built in which works fine:


class TerrainMesh
{
public:
typedef std::vector<TerrainCell>::const_iterator const_iterator;

const_iterator begin() const { return mesh.begin(); }
const_iterator end() const { return mesh.end(); }

private:
std::vector<TerrainCell> mesh;
};


Share this post


Link to post
Share on other sites
Quote:
Original post by silverphyre673

TerrainMesh::const_iterator::operator = (const TerrainMesh & parent)
{
*mesh_ptr = &parent.mesh.begin();
//etc.
}



You problem is that your types don't match at all, begin returns an iterator which is typically a user-defined type and you taking the address of it trying to assign it to a dereferenced pointer to vec of vec of terrain cells now can you the see the problem? your types don't match up:

typedef std::vector<std::vector<TerrainCell> > terrain2d;

terrain2d != terrain2d::const_iterator* (not even remotely)


Quote:
Original post by silverphyre673
[Warning] taking address of temporary //I thought it wasn't temporary since
//I'm passing by reference!


parent isn't but you've created a temporary constant iterator via a call to begin.

Quote:
Original post by Endar
I hate compile errors with templated classes. It takes five minutes to read 2-3 lines and realise that its talking about a single map object, or something.


When you become more experienced with templates and the standard library and you do abit of template meta-programming you start decypher errors almost instaneously.

In anycase it took me like nano-second to see what problem with-out looking at errors because what he did was odd.

Share this post


Link to post
Share on other sites
Quote:
Original post by Endar
Okay, I don't know the answer to the question, mainly because I'm not up on my iterator knowledge, and personally, I hate compile errors with templated classes. It takes five minutes to read 2-3 lines and realise that its talking about a single map object, or something.

Just to note, as always, get STLFilt - used by professionals... It decrypts STL error messages from all the major compilers.

Share this post


Link to post
Share on other sites
snk_kid, how about when you use stl heavily in a class and change something, yielding several pages worth of template errors... they certainly aren't as pretty as C errors (but luckily they are syntax errors).

Anyway I remember reading that boost wants to reimplement certain parts of stl to improve on the error messages.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!