c++ Allocating memory for a class -


i have class :

class myclass{     public:         myclass(string label);         ~myclass();         string myget();         void myset(string label);          void myadd(class2 * edge);     private:         string label;         vector<class2 *> mylist; }; 

i want write function checks equality of objects of type myclass :

 bool operator== (myclass& l,myclass& r) {           } 

in order check mylist vectors of objects, wanted copy l , r, sort vectors of copies , check if sorted vectors of copies equal. think failed making copies of l , r. here wrote :

myclass x = new myclass("s1"); myclass y = new myclass("s2");   x = l; y = r; sort(x.mylist.begin(),x.mylist.end()); sort(y.mylist.begin(),y.mylist.end()) if( x == y)     return true;     else     return false; 

this code gives error : error: conversion 'myclass*' non-scalar type 'myclass' requested.

i tried making x , y pointers didn't work either. can tell me how can fix code?

thanks in advance.

myclass x = new myclass("s1"); - here problem 

operator new returns pointer. either store pointer or create copy on automatic storage (usually referenced stack)

myclass x = myclass("s1"); - should (stack) myclass *x = new myclass("s1"); - or (heap) 

Comments

Popular posts from this blog

Change php variable from jquery value using ajax (same page) -

Pull out data related to my apps from Android Play Store and iOS App Store -

How can I fetch data from a web server in an android application? -