简体   繁体   中英

class's friend function are incompatible?

IDE throws warning that the class's friend function are not compatible with the function's declaration outside of class. What is the cause for the warning?

namespace CommonUtility
{
    Interface::CellType Foo(int);
}

// when placed as friend of class Interface
class Interface
{
public:
    static enum class CellType
    {
        Group,
        NoSuchType
    };
    friend Interface::CellType CommonUtility::Foo(int); // IDE warning not compatible to the declaration 
}

// definition
Interface::CellType CommonUtility::Foo(int i)
{
  if (i == 1)
   return Interface::CellType::Group;
  else
   return Interface::CellType::NoSuchType;
}
  1. For Interface::CellType Foo(int); the Interface::CellType is unknown at that point and should result in a compiler error.
  2. static enum class CellType would also result in a compiler error, because static is not correct here.

And finally:

The declaration of Interface::CellType CommonUtility::Foo(int); has to exists before friend Interface::CellType CommonUtility::Foo(int); can be used. But Interface::CellType Foo(int); can only be declared as soons as Interface::CellType is known. CellType is a nested type and cannot be forward decaled.

And these conditions conflict with each other.

You would need to move the whole enum class CellType {} outside of class Interface to get that working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM