Hey, i'm trying to create a c++ stomp client, my client constructor is :
Client(std::string &server, short port, std::string &login, std::string &pass, Listener &listener);
it gets a listener object which when L开发者_JAVA技巧istener is the following interface :
class Listener {
virtual void message(StmpMessage message) =0;
};
now i attempt to instantiate a client in a test class :
class test : public virtual Listener {
public:
void message(StmpMessage message) {
message.prettyPrint();
}
int main(int argc, char* argv[])
{
Client client("127.0.0.1", 61613, *this);
return 0;
}
};
i'm sending this to the client because this is a listener object, i get the following error :
/Users/mzruya/Documents/STOMPCPP/main.cpp:18: error: no matching function for call to 'Client::Client(const char [10], int, test&)'
/Users/mzruya/Documents/STOMPCPP/Client.h:43: note: candidates are: Client::Client(std::string&, short int, std::string&, std::string&, Listener&)
/Users/mzruya/Documents/STOMPCPP/Client.h:37: note: Client::Client(const Client&)
It does not work because you're passing the wrong parameters to the Client
c'tor …
Client(std::string &server, short port, std::string &login, std::string &pass, Listener &listener);
and you call it
Client client("127.0.0.1", 61613, *this);
… you missed the pass
and login
parameters. Your listener is fine. The problem is not related to inheritance.
You have something wrong on your code
First, afaik you can't define the main inside a class. The program compiles on my pc (using gcc) but it throws
_start':(.text+0x18): undefined reference to main'
Second, as Alexander said on his post you do not pass the correct number of arguments on your constructor getting those errors.
Third, the way you pass arguments on your Constructor is wrong. References require an lvalue
. If you want to send them an rvalue
you need to define your Constructor arguments as
Client(const std::string &server, short port, const std::string &login, const std::string &pass)
Stroustup's book states that this will be interpreted as
std::string temp = std::string("127.0.0.1");
server = temp;
simply-speaking - a temp object will be created to satisfy the Reference.
As you see I removed the Listener &listener
Arguments because I don't know (and tbh I don't think) if you can pass this
as an argument. Maybe someone could clarify-fix that.
if you want to pass your arguments in the Constructor as strings (eg. Client("127.0.0.1" , ...)
) then I guess you would have to do something like that :
std::string hostname = "127.0.0.1";
std::string log = "UserName";
std::string pw = "PassWord";
Client client(hostname, 61613, log , pw);
and of course define your Constructor as
Client(std::string &server, short port, std::string &login, std::string &pass)
{
}
Maybe someone could clarify whats happening with the this
thingy, and if and how you can do that.
edit:
This is the code I used in my pc to come up with all that. Hope it is ok.
#include <iostream>
//Ghost Class
class StmpMessage
{
public:
void prettyPrint()
{
}
};
class Listener
{
virtual void message(StmpMessage message) = 0;
};
class Client : public virtual Listener
{
public:
Client(std::string &server, short port,
std::string &login, std::string &pass)
{
}
void message(StmpMessage message)
{
message.prettyPrint();
}
};
int main(int argc, char* argv[])
{
std::string hostname = "127.0.0.1";
std::string log = "UserName";
std::string pw = "PassWord";
Client client(hostname, 61613, log , pw);
return 0;
}
精彩评论