0

我收到此错误:

main.cpp(10) : error C2664: 'lr::codec::codec(protocol_decoder *)' 
: cannot convert parameter 1 from 'proto::protocol_decoder *' to 'protocol_decoder *'

如果我删除了 proto 命名空间的使用,那么这个错误就会消失。我该如何解决这个问题并仍然保留使用 proto 命名空间。

这是代码:

主.cpp:

#include "protocol_decoder_a.hpp"
#include "codec.hpp"

int main() {

   //factory function create protocol decoder
   proto::protocol_decoder* pro = new proto::protocol_decoder_a;

   lr::codec cdc(pro);

   return 0;
}

编解码器.hpp:

#ifndef __CODEC_HPP__
#define __CODEC_HPP__

#include <map>
#include <string>

class protocol_decoder;

//log replay namespace
namespace lr {


typedef bool (*c_f)(const char* id, unsigned char* rq, size_t rq_length, unsigned char*& response, size_t& resp_len);

// generic codec interface will use specific 
class codec {
   public:
      codec(protocol_decoder* decoder);
      ~codec() {}

      bool get_response(const char* id, unsigned char* rq, size_t rq_length, unsigned char*& response, size_t& resp_len);
      const char* get_monitored_dn(const char* id, unsigned char* rq, size_t rq_length);

      void load_msgs_from_disk();

     protocol_decoder* decoder_;
};

}  //namespace lr

编解码器.cpp:

#include "codec.hpp"

using namespace lr;

codec::codec(protocol_decoder* decoder) : decoder_(decoder) {
   load_msgs_from_disk();
}

void codec::load_msgs_from_disk() {
   //use specific protocol decoder here
}

bool codec::get_response(const char* id, unsigned char* rq, size_t rq_length, unsigned char*& response, size_t& resp_len) {
   return true;
}
const char* codec::get_monitored_dn(const char* id, unsigned char* rq, size_t rq_length) {
   return 0;
}

协议解码器.hpp:

#ifndef __PROTOCOL_DECODER_HPP__
#define __PROTOCOL_DECODER_HPP__

namespace proto {

enum id_type { UNKNOWN_ID, INT_ID, STRING_ID };

struct msg_id {
   msg_id() : type(UNKNOWN_ID) {}
   id_type type;
   union {
      const char* s_id;
      size_t i_id;
   };
};


class protocol_decoder {
public:
    virtual const char* get_monitored_dn(unsigned char* msg, size_t msg_len) = 0;
    virtual bool get_response(unsigned char* rq, size_t rq_len, unsigned char* response, size_t resp_len) = 0;
    virtual bool get_msg_id(unsigned char* rq, size_t rq_len, msg_id id) = 0;
};

} //namespace proto

#endif //__PROTOCOL_DECODER_HPP__

协议解码器a.hpp:

#ifndef __PROTOCOL_DECODER_A_HPP__
#define __PROTOCOL_DECODER_A_HPP__

#include "protocol_decoder.hpp"

namespace proto {

class protocol_decoder_a : public proto::protocol_decoder {
public:
   virtual const char* get_monitored_dn(unsigned char* msg, size_t msg_len);
   virtual bool get_response(unsigned char* rq, size_t rq_len, unsigned char* response, size_t resp_len);
   virtual bool get_msg_id(unsigned char* rq, size_t rq_len, proto::msg_id id);
};

} //namespace proto

#endif //__PROTOCOL_DECODER_A_HPP__

协议解码器a.cpp:

#include "protocol_decoder_a.hpp"

using namespace proto;


const char* protocol_decoder_a::get_monitored_dn(unsigned char* msg, size_t msg_len) {
  //specific stuff here
   return 0;
}

bool protocol_decoder_a::get_response(unsigned char* rq, size_t rq_len, unsigned char* response, size_t resp_len) {
   return true;
}

bool protocol_decoder_a::get_msg_id(unsigned char* rq, size_t rq_len, proto::msg_id id) {
    return true;
}
4

1 回答 1

5

你不小心声明了两个protocol_decoder类。一个在全局命名空间中,一个在proto命名空间中。

更改此声明:

class protocol_decoder;

对此:

namespace proto
{
    class protocol_decoder;
}
于 2013-05-15T17:58:12.983 回答