如何修复仅在使用命名空间时出现的错误C2664

时间:2013-05-15 17:50:10

标签: c++ namespaces

我收到此错误:

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

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

以下是代码:

main.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;
}

codec.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

codec.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;
}

protocol_decoder.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__

protocol_decoder_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__

protocol_decoder_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;
}

1 个答案:

答案 0 :(得分:5)

你不小心宣布了两个protocol_decoder课程。一个在全局命名空间中,一个在proto命名空间中。

更改此声明:

class protocol_decoder;

对此:

namespace proto
{
    class protocol_decoder;
}