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
Printable View
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
You need an overloaded = operator and, I believe, a copy constructor to use your own class as the templated class
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.
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 ?
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.
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....
}
Don't you think that should be private?Code:public:
int ItemData;
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.
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