我有这些编译错误,我真的看不出我的代码有什么问题,请帮帮我
g ++主要抱怨一些不使用' - >'的行操作员,但我正在使用它所以这个错误对我来说似乎很奇怪
g++ tests/client.cpp src/jsonrpc/transport/PipeClient.cpp -o client --std=c++11 -I ./include -lstdc++ -lboost_thread-mgw52-mt-1_59 -lboost_system-mgw52-mt-1_59 -D_WIN32_WINNT=0x0600
include/jsonrpc/Client.h: In instantiation of 'bool jsonrpc::Client<ServerSocketType, ClientSocketType>::connect(std::__cxx11::string) [with ServerSocketType = jsonrpc::PipeServer; ClientSocketType = jsonrpc::PipeClient; std::__cxx11::string = std::__cxx11::basic_string<char>]':
tests/client.cpp:13:42: required from here
include/jsonrpc/Client.h:29:48: error: request for member 'connect' in '((jsonrpc::Client<jsonrpc::PipeServer, jsonrpc::PipeClient>*)this)->jsonrpc::Client<jsonrpc::PipeServer, jsonrpc::PipeClient>::m_socket', which is of pointer type 'jsonrpc::PipeClient*' (maybe you meant to use '->' ?)
include/jsonrpc/Client.h: In instantiation of 'bool jsonrpc::Client<ServerSocketType, ClientSocketType>::sendRequest(jsonrpc::Request*) [with ServerSocketType = jsonrpc::PipeServer; ClientSocketType = jsonrpc::PipeClient]':
tests/client.cpp:19:32: required from here
include/jsonrpc/Client.h:52:34: error: invalid initialization of non-const reference of type 'std::__cxx11::string& {aka std::__cxx11::basic_string<char>&}' from an rvalue of type 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}'
include/jsonrpc/Client.h:85:18: note: initializing argument 1 of 'bool jsonrpc::Client<ServerSocketType, ClientSocketType>::sendPacket(std::__cxx11::string&) [with ServerSocketType = jsonrpc::PipeServer; ClientSocketType = jsonrpc::PipeClient; std::__cxx11::string = std::__cxx11::basic_string<char>]'
packet.append(buffer, bytesRead);
^
include/jsonrpc/Client.h: In instantiation of 'void jsonrpc::Client<ServerSocketType, ClientSocketType>::disconnect() [with ServerSocketType = jsonrpc::PipeServer; ClientSocketType = jsonrpc::PipeClient]':
include/jsonrpc/Client.h:24:27: required from 'jsonrpc::Client<ServerSocketType, ClientSocketType>::~Client() [with ServerSocketType = jsonrpc::PipeServer; ClientSocketType = jsonrpc::PipeClient]'
tests/client.cpp:11:36: required from here
include/jsonrpc/Client.h:34:17: error: request for member 'disconnect' in '((jsonrpc::Client<jsonrpc::PipeServer, jsonrpc::PipeClient>*)this)->jsonrpc::Client<jsonrpc::PipeServer, jsonrpc::PipeClient>::m_socket', which is of pointer type 'jsonrpc::PipeClient*' (maybe you meant to use '->' ?)
In file included from ./include/jsonrpc/Request.h:4:0,
from include/jsonrpc/Client.h:6:
./include/jsonrpc/Message.h: In instantiation of 'static std::__cxx11::string jsonrpc::Message::toJson(const std::vector<MessageType*>&) [with MessageType = jsonrpc::Request; std::__cxx11::string = std::__cxx11::basic_string<char>]':
include/jsonrpc/Client.h:52:59: required from 'bool jsonrpc::Client<ServerSocketType, ClientSocketType>::sendRequest(jsonrpc::Request*) [with ServerSocketType = jsonrpc::PipeServer; ClientSocketType = jsonrpc::PipeClient]'
tests/client.cpp:19:32: required from here
./include/jsonrpc/Message.h:57:21: error: no matching function for call to 'rapidjson::GenericDocument<rapidjson::UTF8<> >::Swap(jsonrpc::Request* const&)'
In file included from ./include/jsonrpc/Message.h:4:0,
from ./include/jsonrpc/Request.h:4,
from include/jsonrpc/Client.h:6:
D:/usr/msys2/mingw64/include/rapidjson/document.h:655:19: note: candidate: rapidjson::GenericValue<Encoding, Allocator>& rapidjson::GenericValue<Encoding, Allocator>::Swap(rapidjson::GenericValue<Encoding, Allocator>&) [with Encoding = rapidjson::UTF8<>; Allocator = rapidjson::MemoryPoolAllocator<>]
GenericValue& Swap(GenericValue& other) RAPIDJSON_NOEXCEPT {
^
D:/usr/msys2/mingw64/include/rapidjson/document.h:655:19: note: no known conversion for argument 1 from 'const value_type {aka jsonrpc::Request* const}' to 'rapidjson::GenericValue<rapidjson::UTF8<> >&'
测试/ client.cpp
#include "jsonrpc/Client.h"
#include "jsonrpc/transport/PipeServer.h"
#include "jsonrpc/transport/PipeClient.h"
#include <iostream>
using namespace jsonrpc;
int main(int argc, char * argv[])
{
Client<PipeServer, PipeClient> client;
client.connect("\\\\.\\pipe\\jsonrpc");
typedef Request::ValueType Value;
Request request(Value("_1").Move(), Value("test").Move(), Value(rapidjson::kArrayType).Move());
client.sendRequest(&request);
std::vector<Response *> responses;
client.recvResponses(responses);
std::cout << responses[0]->toJson() << std::endl;
}
jsonrpc / Client.h
#ifndef JSONRPC_CLIENT_H
#define JSONRPC_CLIENT_H
#include "jsonrpc/base.h"
#include "jsonrpc/Server.h"
#include "jsonrpc/Request.h"
#include "jsonrpc/Response.h"
#include <string>
#include <vector>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
namespace jsonrpc
{
template <typename ServerSocketType, typename ClientSocketType>
class Server;
template <typename ServerSocketType, typename ClientSocketType>
class Client
{
typedef Server<ServerSocketType, ClientSocketType> ServerType;
typedef Client<ServerSocketType, ClientSocketType> ClientType;
friend ServerType;
public:
Client() : m_socket(new ClientSocketType()), m_server(NULL) {}
~Client()
{
disconnect();
}
bool connect(const std::string & address)
{
return m_socket->connect(address);
}
void disconnect()
{
m_socket->disconnect();
}
bool recvResponses(std::vector<Response *> & responses)
{
std::string packet;
if (!this->recvPacket(packet)) {
return false;
}
responses = Response::parseJson<Response>(packet);
return true;
}
bool sendRequest(Request * request)
{
return sendRequests(std::vector<Request *>(1, request));
}
bool sendRequests(std::vector<Request *> & requests)
{
std::string packet = Request::toJson<Request>(requests);
return sendPacket(packet);
}
private:
ClientSocketType * m_socket;
ServerType * m_server;
Client(ClientSocketType * socket, ServerType * server) : m_socket(socket), m_server(server) {}
bool recvPacket(std::string & packet)
{
char * buffer = new char[JSONRPC_READ_BUFFER_LENGTH];
packet.empty();
do {
ssize_t bytesRead = m_socket->read(buffer, JSONRPC_READ_BUFFER_LENGTH);
if (bytesRead > 0) {
packet.append(buffer, bytesRead);
} else {
break;
}
} while (packet[packet.length() - 1] != '\n');
return packet.size() > 0;
}
bool sendPacket(std::string & packet)
{
packet += "\n";
size_t length = packet.length();
size_t offset = 0;
while (offset < length) {
ssize_t bytesWritten = m_socket->write(packet.c_str() + offset, std::min<size_t>(JSONRPC_WRITE_BUFFER_LENGTH, length - offset));
if (bytesWritten > 0) {
offset += bytesWritten;
} else {
break;
}
}
m_socket->flush();
}
void start()
{
boost::thread thread(boost::bind(ClientType::run, this));
}
void run()
{
std::string packet;
while (recvPacket(packet)) {
handlePacket(packet);
}
}
void handlePacket(std::string packet)
{
typedef Request::ValueType Value;
std::vector<Request *> requests = Request::parseJson<Request>(packet);
std::vector<Response *> responses;
for (Request * request : requests) {
Response * response;
if (request->IsNull()) {
response = new Response(Value().Move(), Value(-32700).Move(), Value("Parse error").Move());
} else if (request->isValid()) {
response = m_server->handleRequest(request);
} else if (request->hasValidId()) {
response = new Response(Value(request->getId(), request->GetAllocator).Move(), Value(-32600).Move(), Value("Invalid request").Move());
} else {
response = new Response(Value().Move(), Value(-32600).Move(), Value("Invalid request").Move());
}
delete request;
responses.push_back(response);
}
sendPacket(Response::toJson<Response>(responses).c_str());
}
};
}
#endif // JSONRPC_CLIENT_H
jsonrpc / Message.h
#ifndef JSONRPC_MESSAGE_H
#define JSONRPC_MESSAGE_H
#include <vector>
#include <rapidjson/document.h>
#include <rapidjson/writer.h>
namespace jsonrpc
{
class Message : public rapidjson::Document
{
typedef rapidjson::StringBuffer StringBuffer;
typedef rapidjson::Writer<StringBuffer> StringWriter;
public:
const ValueType & getId() const
{
return operator[]("id");
}
const Ch * toJson()
{
StringBuffer buffer;
StringWriter writer(buffer);
Accept(writer);
return buffer.GetString();
}
template <typename MessageType>
static std::vector<MessageType *> parseJson(const std::string & json)
{
rapidjson::Document document;
document.Parse(json.c_str());
std::vector<MessageType *> messages;
if (document.IsArray()) {
for (rapidjson::SizeType i = 0; i < document.Size(); ++i) {
messages.push_back(new MessageType(document[i]));
}
}
else {
messages.push_back(new MessageType(document));
}
return messages;
}
template <typename MessageType>
static std::string toJson(std::vector<MessageType *> & messages)
{
rapidjson::Document document;
size_t nbMessages = messages.size();
if (nbMessages == 1) {
document.Swap(messages[0]->Move());
}
else if (nbMessages > 1) {
document.SetArray();
for (MessageType * message : messages) {
document.PushBack(*message, document.GetAllocator());
}
}
else {
return "";
}
StringBuffer buffer;
StringWriter writer(buffer);
document.Accept(writer);
return buffer.GetString();
}
};
}
#endif // JSONRPC_MESSAGE_H