invalid use of non-static data member

This is part of the header-file for a class:
1
2
3
4
5
6
7
8
9
10
11
12
class BasexClient {
  public:
    BasexClient (const std::string&, const std::string&, const std::string&, const std::string&);
    virtual ~BasexClient();
    ...
    void Create(const std::string & dbName, const std::string  & content = "");
    QueryObject * Query(const std::string & query, BasexSocket * socket);

    BasexSocket *          getSocket();

  private:
    BasexSocket *Socket;

And this is part of a test-file:
1
2
3
4
5
6
7
8
9
10
int main() {
	std::string DBHOST     = "localhost";
	std::string DBPORT     = "1984";
	std::string DBUSERNAME = "Test";
	std::string DBPASSWORD = "<testPassword>";

	BasexClient Session (DBHOST, DBPORT, DBUSERNAME, DBPASSWORD);

	std::string query { "This is a test" };
	QueryObject * Q_Object = Session.Query(query, Session.getSocket());

After a BasexClient-object has been created (Session), this variable can be used to create a QueryObject (Q_Object). This QueryObjectobject uses the same socket as the session. Each session can use multiple QueryObjects.
It is possible to create multple BasexClient-objects, each with a unique socket.

I want to remove the Session.getSocket() argument from line 10 in the test-file and add a default value.
As in line 6 from BasexClient.h, I tried to add a default-argument to line 7:
 
QueryObject * Query(const std::string & query, BasexSocket * socket = Socket);

But when I compile this class, I get this error:
invalid use of non-static data member ‘BasexClient::Socket’
.
The 'Socket' member can't be made static since it is not shared by all the BasecClient-objects.

How can I add a default value for the socket?

Ben
You could create another overload of the Query function that takes only one argument and calls the two-argument version with the Socket member as second argument.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class BasexClient {
  ...
    QueryObject * Query(const std::string & query);
    QueryObject * Query(const std::string & query, BasexSocket * socket);
  ...
    BasexSocket *Socket;
}

QueryObject * BasexClient::Query(const std::string & query) {
    return Query(query, Socket);
}


QueryObject * BasexClient::Query(const std::string & query, BasexSocket * socket) {
    ...
}
Last edited on
since its a pointer, you may be able to do this:

QueryObject * Query(const std::string & query, BasexSocket * socket = nullptr)
{
//if its nullptr, make it Socket, else use what they passed in?
}
But when I compile this class, I get this error:


In a class/struct member function you can't use a member variable (unless static) as part of the parameters as at that point 'this' isn't available. So to use a default value for an arg you have to use something not defined in the class/struct (or static) that can be accessed without reference to 'this'.

The easiest way when wanting a default for a pointer is as jonnin describes above.

Topic archived. No new replies allowed.