开发者

cannot convert namespace_something::some_class to some_class

开发者 https://www.devze.com 2023-01-24 04:39 出处:网络
And both classes are same, example: ///////////////server.h////////////// #ifndef SERVER_H #define SERVER_H

And both classes are same, example:

///////////////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));
}

This produces the compilation error:

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: In constructor 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 convertIrc::Client*' to `Client*' in initialization


You should put the forward declaration class Client inside the namespace Irc.

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

Otherwise, the ClientPtr's typedef will be referring to the Client without a namespace (since it is the closest declaration of Client found), not the Irc::Client you want.

   typedef boost::shared_ptr<Client> ClientPtr;


or, put using namespace_something;

but, is desirable to do the way kennytm said


You probably want to put the forward declaration of Client inside the namespace Irc in server.h

namespace Irc {
  class Client;
  ...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消