CAsyncSocket and std::vector

This is a discussion on CAsyncSocket and std::vector within the C++ Programming forums, part of the General Programming Boards category; i have a class derived from casyncsocket "CWS" but when i use std::vector <CWS> socket_list; CWS ws; socket_list.push_back(ws); it will ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    103

    CAsyncSocket and std::vector

    i have a class derived from casyncsocket "CWS" but when i use std::vector <CWS> socket_list; CWS ws; socket_list.push_back(ws); it will give error that the CWS class doesnt have a "=" function

    is there any solution to this ?

    thanks

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    You need an overloaded = operator and, I believe, a copy constructor to use your own class as the templated class
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    CAsyncSocket was not designed to be copied, but whatever you store in a vector will get copied. You should store a std::vector<boost::shared_ptr<CWS> > instead, or if you don't want to use boost store a raw pointer (from new) and make sure you delete the allocated memory when you clear or erase from the vector.

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    103
    JaWiB: how do i add those ? i tried something and gave me error
    Daved: thanks i didnt know about that, but cant get it to work what should i include ?
    Last edited by eXistenZ; 08-13-2005 at 02:19 AM.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    To use boost, you have to go to boost.org and download it. It's a great library if you want to do that. If you don't want to go through the minor hassle of using boost, then like I said you can just hold pointers and remember to delete them. That doesn't require anything extra to include, just knowledge of new and delete.

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    103
    i found a way to add the right constructors but im facing some problems like the server fails when i connect something to it the first 2 times but when i try it the 3rd time it works, could these weird problems be because CAsyncSocket doesnt support that ? here is my class code for constructors
    Code:
    class CWSS : public CAsyncSocket
    {
    // Attributes
    public:
    
    // Operations
    public:
    	CWSS();
    	CWSS(const CWSS &src) {}
    	CWSS& operator=(const CWSS &src) {return *this;}
    	virtual ~CWSS();
    
    // Overrides
    public:
    	int ItemData;
    etc....
    }

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    public:
    	int ItemData;
    Don't you think that should be private?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    You didn't actually implement those, you just created empty methods. When the vector adds a copy of your object into its internal storage, the copy it gets will have no information because your copy constructor and assignment operator don't actually copy anything.

    I don't know the internals of CAsyncSocket, but IMO the solution is still to hold a vector of pointers to memory allocated with new and delete them when you remove them from the vector.

    Technically, a better design might be to use containment instead of inheritance and just hold the socket internally, but you'd still run into the exact same problem with the copying.

  9. #9
    Registered User
    Join Date
    Dec 2004
    Posts
    103
    thanks alot for the replys i got it to work now using pointers they are no problem to me but i thought i could solve it that way :P

Popular pages Recent additions subscribe to a feed

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