STL List container with classes

This is a discussion on STL List container with classes within the C++ Programming forums, part of the General Programming Boards category; I have made a class that looks like this: Code: class DLLIMPORT Object { private: float x, y, z; public: ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    731

    STL List container with classes

    I have made a class that looks like this:

    Code:
    class DLLIMPORT Object
    {
        private:
            float x, y, z;
        
        public:
            LPDIRECT3DVERTEXBUFFER9 vertexbuffer;    //Main point here
    
            Object();
            void AddVertexBuffer(VERTEXBUFFER buf);
    };
    Than I made the STL list and iterator:

    Code:
            list<Object> objects;
            list<Object>::iterator iter;
    Than I tried to use the iter to get the vertexbuffer from the class and use it:

    Code:
        for (iter = objects.begin(); iter != objects.end(); iter++)
        {
            device->SetStreamSource(0, iter->vertexbuffer, 0, sizeof(CUSTOMVERTEX));    //Main point here
        }
        device->SetFVF(D3DFVF_CUSTOMVERTEX);
        device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, objects.size());
    It compiles fine but when I run it, it gives a windows error saying the program had to close. Am I not alowed to use a varaible from the class or is there a better way? I can not use STL vectors or stacks/deques for certain reasons I already looked over.
    Last edited by Rune Hunter; 10-21-2005 at 09:40 PM.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Hi,

    I don't think the operator<() is defined for iterators. I used != instead, and it worked for me:
    Code:
    #include <iostream>
    #include <list>
    
    using namespace std;
    
    class Object
    {
    private:
    	int num;
    
    public:
    	Object(int n)
    	{
    		num = n;
    	}
    	int getNum()
    	{
    		return num;
    	}
    };
    
    int main()
    {
    	Object obj0(10);
    	Object obj1(20);
    	Object obj2(30);
    
    	list<Object> objects;
    	objects.push_back(obj0);
    	objects.push_back(obj1);
    	objects.push_back(obj2);
    
    	list<Object>::iterator iter;
    	for(iter = objects.begin(); iter != objects.end(); iter++)
    	{
    		cout<<iter->getNum()<<endl;
    	}
    
    
        return 0;
    }
    class DLLIMPORT Object
    That doesn't cause a compiler error? Two word variable name?
    Last edited by 7stud; 10-21-2005 at 10:03 PM.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    Warning, I could be wrong about this.

    (*iter)->VertexBuffer

    Dereferencing the iterator gives you the address of the object instance.

  4. #4
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    Code:
    class DLLIMPORT Object
    DLLIMPORT is a predefined varaible (#define) that tells about the dll exporting.

    I don't think the operator<() is defined for iterators. I used != instead, and it worked for me:
    I did use != ?

    Quote Originally Posted by Eber Kain
    Warning, I could be wrong about this.

    (*iter)->VertexBuffer

    Dereferencing the iterator gives you the address of the object instance.
    I'm afraid I don't get what you mean. It apears to work just like a vector container would, except vector containers worked in this situation.
    Last edited by Rune Hunter; 10-21-2005 at 10:35 PM.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    Your Object class doesn't have a copy constructor, but vertexbuffer looks like a pointer. Be sure you aren't destroying that pointer or making it invalid in your destructor or elsewhere.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I did use != ?
    Sorry, I must be seeing things.

  7. #7
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by Rune Hunter
    I have made a class that looks like this:

    Code:
    class DLLIMPORT Object
    {
        private:
            float x, y, z;
        
        public:
            LPDIRECT3DVERTEXBUFFER9 vertexbuffer;    //Main point here
    
            Object();
            void AddVertexBuffer(VERTEXBUFFER buf);
    };
    snip...

    Code:
        for (iter = objects.begin(); iter != objects.end(); iter++)
        {
            device->SetStreamSource(0, iter->vertexbuffer, 0, sizeof(CUSTOMVERTEX));    //Main point here
        }
        device->SetFVF(D3DFVF_CUSTOMVERTEX);
        device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, objects.size());
    have you initialised the vertexbuffer pointer?
    BTW use ++iter, not iter++ (avoids creating a temporary iterator object)
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  8. #8
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    I have declared and filled the vertexbuffer, and ++iter I will use now.


    Well I now have reason to belive the class is causing all these problems. Because I did a test to see if could change a varaible inside the class and this is what the code looks like:

    Code:
    class object
    {
    
    public:
    
    int x;
    object();
    };
    
    object::object()
    {
    x = 5;
    }
    
    
    object obj();
    obj.x = 10;
    After I do that x will still equal 5, even though I changed it. I also typed that up inside the browser some there might be spelling errors, etc. Otherwise that is a short version of the code I have.

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    After I do that x will still equal 5, even though I changed it.
    Huh? Prove it.

    Note: you are not calling the default constructor properly.

  10. #10
    Registered User
    Join Date
    Oct 2005
    Posts
    11
    Just remove the () from object obj(); and as long as obj.x is inside of a function like main you are set. Outside won't work.
    Last edited by Kirmie; 10-24-2005 at 11:40 PM.

  11. #11
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    in otherwords I am using the winapi main entry point so if I have obj.x = 5 inside that it should work? Well I'll go look over every peice of code and start debuging it than.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with STL container list for INformation Manager
    By jackfraust in forum C++ Programming
    Replies: 5
    Last Post: 04-22-2009, 11:12 PM
  2. storing derived classes in a stl container
    By *DEAD* in forum C++ Programming
    Replies: 2
    Last Post: 10-03-2008, 07:50 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Replies: 6
    Last Post: 03-02-2005, 01:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21