c++ - Why can't I define a forward declared class by aliasing it? -


trying compile code below fails due "conflicting declaration". why can't define forward declared class this?

i did hide implementation uses library. although i'll admit not abstract - you'll still need know implementation uses make correct call - still interested in why doesn't work.

bar.cpp:

#include "bar.hpp" #include "foo.hpp"  using foo = ns::foo;  void bar::foo(foo f) { } 

bar.hpp:

class foo;  class bar {     void foo(foo f); }; 

foo.hpp:

namespace ns {      class foo {     }; } 

to clear, want know why can't define declared class aliasing - in other words saying "use definition on there has different name"

you declaring foo twice conflicting types. first, declare foo in bar.hpp as:

class foo; 

subsequently, declare foo in bar.cpp as:

using foo = ns::foo; 

you cannot put forward declaration if define in source file alias same name, because declare 2 different types same name.

based on question assume want use foo, without namespace in bar.cpp. solution following:

bar.cpp

#include "bar.hpp" #include "foo.hpp"  using ns::foo;  void bar::foo(foo f) {} 

bar.hpp

namespace ns {     class foo; }  class bar  {     void foo(ns::foo f); }; 

foo.hpp

namespace ns {     class foo     {}; } 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

python - pip wont install .WHL files -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -