Error - cannot call a member function without an object in C ++

I get a compilation error

cannot call member function ‘bool GMLwriter::write(const char*, MyList<User*>&, std::vector<std::basic_string<char> >)’ without object

when i try to compile

 class GMLwriter{
    public:
    bool write(const char *fn, MyList<User*>& my_vec, vector<string>edges);
};

function is defined later called in mainwith

GMLwriter::write(argv[3], Users, edges);

Users are declared earlier using MyList<User*> Users;(MyList is an ADT list and I have a User class), and edges are declared usingvector<string>edges

What objectdoes this error refer to?

+5
source share
3 answers

GMLwriter::writeis not a static GMLwriter function, you need to call it through an object. For instance:

GMLwriter gml_writer;   
gml_writer.write(argv[3], Users, edges);

If it is GMLwriter::writenot dependent on any state of the GMLwriter (access to any member GMLwriter), you can make it a static member function. Then you can call it directly without an object:

class GMLwriter
{
public:
   static bool write(const char *fn, MyList<User*>& my_vec, vector<string>edges);
   ^^^^
};

then you can call:

GMLwriter::write(argv[3], Users, edges);
+18

GMLwriter , .

- , :

GMLwriter foo;   
foo.write(argv[3], Users, edges);

, , :

class GMLwriter{
    public:
    // static member functions don't use an object of the class,
    // they are just free functions inside the class scope
    static bool write(const char *fn, MyList<User*>& my_vec, vector<string>edges);
};

// ...
GMLwriter::write(argv[3], Users, edges);

bool write(const char *fn, MyList<User*>& my_vec, vector<string>edges);
// ...
write(argv[3], Users, edges);
+1

Perhaps you are trying to call / create a static method to be

In this case, you may be preceded by an ad using a “static” modifier.

http://www.functionx.com/cppcli/classes/Lesson12b.htm

0
source

All Articles