无法将namespace_something :: some_class转换为some_class

时间:2010-11-10 17:22:16

标签: c++

两个类都相同,例如:

///////////////server.h//////////////
#ifndef SERVER_H
#define SERVER_H

#ifdef WIN32
        #ifndef _WIN32_WINNT
                #define _WIN32_WINNT 0x0501
        #endif
#endif
#include <boost/shared_ptr.hpp>
#include <boost/asio.hpp>
#include <map>

#include "auxiliar.h"

class Client;

namespace Irc
{
        typedef boost::shared_ptr<Client> ClientPtr;
        typedef std::map<SocketPtr, ClientPtr> ClientsMap;

        class Server
        {
                public:
                        Server();
                        ~Server();

                        void start();

                        void startAccept();

                        ClientsMap::const_iterator begin() { return m_clients.begin(); }
                        ClientsMap::const_iterator end() { return m_clients.end(); }

                private:
                        ClientsMap m_clients;
                        boost::asio::io_service service;
                        boost::asio::ip::tcp::acceptor* m_acceptor;
        };
} //namespace Irc
#endif
/////////////server.cpp////////////////
#include "server.h"
#include "defines.h"
#include "client.h"

Irc::Server::Server()
{
        service.run();
}

Irc::Server::~Server()
{
        m_clients.clear();
}

void Irc::Server::start()
{
        m_acceptor = new boost::asio::ip::tcp::acceptor(service, boost::asio::ip::tcp::endpoint(
                boost::asio::ip::tcp::v4(), SERVER_PORT));
}

void Irc::Server::startAccept()
{
        SocketPtr s(new boost::asio::ip::tcp::socket(service));
        m_acceptor->accept(*s);
        Client *client = new Client(s);
        client->setIoService(&service);
        ClientPtr ptr(client);
        m_clients.insert(std::make_pair(s, ptr));
}

这会产生编译错误:

  

g ++。exe -c src / server.cpp -o src / server.o -I“D:/ Dev-Cpp / include”-g -ggdb -I“include”-fexpensive-optimizations -O1 D:/ Dev-Cpp / include / boost / smart_ptr / shared_ptr.hpp:初始化中的构造函数boost::shared_ptr< <template-parameter-1-1> >::shared_ptr(Y*) [with Y = Irc::Client, T = Client]': src/server.cpp:27: instantiated from here D:/Dev-Cpp/include/boost/smart_ptr/shared_ptr.hpp:179: error: cannot convert Irc :: Client *'到`Client *'

3 个答案:

答案 0 :(得分:2)

您应该将前置声明class Client 放在命名空间Irc内。

namespace Irc {
   class Client;  // put it here.
   ...

否则,ClientPtr的typedef将引用没有命名空间的Client(因为它是Client找到的最接近的声明),而不是您想要的Irc::Client

   typedef boost::shared_ptr<Client> ClientPtr;

答案 1 :(得分:0)

或者,放     使用namespace_something;

但是,做kennytm说的方式是可取的

答案 2 :(得分:0)

您可能希望将Client的前瞻声明放在namespace Irc中的server.h

namespace Irc {
  class Client;
  ...
}