简体   繁体   中英

Was not declared in this scope error when using private class in C++

I've a class

class myClass
{
    private:

       std::list <myInnerClass> mylists;

       class myInnerClass
       {
            // Design for this private class
       }
}

I can't understand why the line std::list <myInnerClass> mylists gives me the following error:

  - Type 'myInnerClass' was not declared in this scope
  - Type 'myInnerClass' could not be resolved

Where am I wrong?

You use myInnerClass before you declare it.

This code will compile:

class myClass
{
    private:
       class myInnerClass
       {
            // Design for this private class
       }
       std::list <myInnerClass> mylists;
}

You are using your inner class before declaring it

fix:

   class myInnerClass
   {
        // Design for this private class
   };

   std::list <myInnerClass> mylists;

first you have to declare inner class and then You can use it

Your code

class myClass
{
private:
   std::list <myInnerClass> mylists;
   class myInnerClass
   {
        // Design for this private class
   }
}

Correct code:

class myClass
{
    private:
    class myInnerClass
       {
            // Design for this private class
       }
           std::list <myInnerClass> mylists;
    }

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