expected `;' before "it"

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • curious2007
    New Member
    • Jun 2007
    • 71

    expected `;' before "it"

    Hello,

    In a template implementation, I am trying to define a list iterator using the following line:

    list<AI>::const _iterator it;

    However, I get the following error:

    expected `;' before "it"

    I have included #include <list>
    and using namespace std at the beginning of my code. But the problem did not go away. I would apprecate any insight. Thanks.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You forgot to #include <list>.

    Comment

    • curious2007
      New Member
      • Jun 2007
      • 71

      #3
      no, I have #include<list>
      but I still get this error.

      Comment

      • Benny the Guard
        New Member
        • Jun 2007
        • 92

        #4
        Are you using:

        Code:
        using namespace std;
        If not try the following in your declaration.

        Code:
        std::list

        Comment

        • curious2007
          New Member
          • Jun 2007
          • 71

          #5
          Sorry for not writing it clearly but as can be seen from my inital post, I have

          #include<list>
          using namespace std;

          at the beginning of my code. But to no avail, I am still getting the error.

          Comment

          • Meetee
            Recognized Expert Contributor
            • Dec 2006
            • 928

            #6
            Originally posted by curious2007
            Hello,

            In a template implementation, I am trying to define a list iterator using the following line:

            list<AI>::const _iterator it;

            However, I get the following error:

            expected `;' before "it"

            I have included #include <list>
            and using namespace std at the beginning of my code. But the problem did not go away. I would apprecate any insight. Thanks.
            Try this:

            typedef typename list<AI>::const _iterator observerIterato r;
            observerIterato r it;

            OR

            typename list<AI>::const _iterator it;

            It may help..!

            Regards

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              This compiles and links with Visual Studio.NET 2005
              [code=cpp]
              #include <iostream>
              using namespace std;
              #include <list>
              class AI
              {

              };
              int main()
              {
              list<AI>::const _iterator it;
              }
              [/code]

              Comment

              • curious2007
                New Member
                • Jun 2007
                • 71

                #8
                Thanks,

                typename list<AI>::const _iterator it;

                did the trick.

                I am still not familiar with the subtleties of these but weaknessforcats , somehow your post#7 seems to me a different thing. Because in your example you already declare and define a class AI. So computer has no problem, recognizinf list<AI>. Whereas when AI is going to be determined when a template is initiated we are having additional problems when we write list<AI>.

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  Originally posted by curious2007
                  I am still not familiar with the subtleties of these but weaknessforcats , somehow your post#7 seems to me a different thing. Because in your example you already declare and define a class AI. So computer has no problem, recognizinf list<AI>. Whereas when AI is going to be determined when a template is initiated we are having additional problems when we write list<AI>.
                  The originakl problem was that:
                  Originally posted by curious2007
                  list<AI>::const _iterator it;
                  was not compiling.

                  This looks like you want to create an const iterator object, it, for a list<AI>. You know, so you can:
                  [code=cpp]
                  list<AI> mylist;
                  list<AI>::const _iterator it = mylist.begin();
                  [/code]

                  That is what my post #7 was about.

                  This:
                  [code=cpp]
                  typename list<AI>::const _iterator it;
                  [/code]

                  only works if it is inside a template. You can't use typename outside a template. The original problem said nothing about the code being inside a template.

                  Comment

                  Working...