相关疑难解决方法(0)

如何转发声明名称空间中的类

我试图在头文件中使用前向声明来减少使用的#includes,从而减少用户包含我的头文件的依赖关系.

但是,我无法转发使用命名空间的decalre.见下面的例子.

a.hpp file:
#ifndef __A_HPP__
#define __A_HPP__

namespace ns1 {

   class a {
   public:
      a(const char* const msg);

      void talk() const;

   private:
      const char* const msg_;
   };
}

#endif //__A_HPP__

a.cpp file:
#include <iostream>

#include "a.hpp"

using namespace ns1;

a::a(const char* const msg) : msg_(msg) {}

void a::talk() const { 
   std::cout << msg_ << std::endl; 
}



consumer.hpp file:
#ifndef __CONSUMER_HPP__
#define __CONSUMER_HPP__

// How can I forward declare a class which uses a namespace
//doing this below results in …
Run Code Online (Sandbox Code Playgroud)

c++ namespaces

38
推荐指数
2
解决办法
4万
查看次数

标签 统计

c++ ×1

namespaces ×1