STL <list> declaration in header file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • G?nter Omer

    STL <list> declaration in header file

    Hi there!

    I'm just trying to compile a header file (unsing Borland C++ Builder
    10)implementing a class, containing the declaration of the STL <list>
    but it
    refuses to work.

    The following errors occure:
    "test.h" line 29 type name expected
    "test.h" line 29 declaration missing

    Interesting is that it works if I copy the declaration into the
    main.cpp
    file!

    Here is the source:

    <snip fileName="test. h">
    class xy
    {
    public:
    void getI(void);
    void setI(int);

    private:
    int i;
    };

    class xyy
    {
    public:
    void do_something(vo id);

    private:
    list <xy *> myList;
    };
    </snip>


    Thanks in Advance!
    Günter
  • Rolf Magnus

    #2
    Re: STL &lt;list&gt; declaration in header file

    G?nter Omer wrote:
    [color=blue]
    > Hi there!
    >
    > I'm just trying to compile a header file (unsing Borland C++ Builder
    > 10)implementing a class, containing the declaration of the STL <list>
    > but it refuses to work.[/color]

    Header files aren't supposed to be compiled separately.
    [color=blue]
    > The following errors occure:
    > "test.h" line 29 type name expected
    > "test.h" line 29 declaration missing
    >[/color]

    Which line is line 29?
    [color=blue]
    > Interesting is that it works if I copy the declaration into the
    > main.cpp file![/color]

    What main.cpp file? Your example doesn't show that file. Without
    context, there is nothing wrong with that header file, though I'd
    recommend making headers self contained, i.e. add every #include the
    header needs. So I'd add:

    #include <list>

    at the top and replace

    list <xy *> myList;

    with

    std::list <xy *> myList;
    [color=blue]
    > Here is the source:
    >
    > <snip fileName="test. h">
    > class xy
    > {
    > public:
    > void getI(void);
    > void setI(int);
    >
    > private:
    > int i;
    > };
    >
    > class xyy
    > {
    > public:
    > void do_something(vo id);
    >
    > private:
    > list <xy *> myList;
    > };
    > </snip>[/color]

    Comment

    • G?nter Omer

      #3
      Re: STL &lt;list&gt; declaration in header file

      Thanks for your fast answer and excuse my uncomplete post!

      Here are all three files of the testing program, line
      29 is marked:

      test.h
      ----
      /*





      */


      #ifndef TEST_HEADER
      #define TEST_HEADER

      class xy
      {
      public:
      void getI(void);
      void setI(int);

      private:
      int i;
      };

      class xyy
      {
      public:
      void do_something(vo id);

      private:
      list <xy *> myList; // -> this is line 29!
      };

      #endif
      ----


      /*





      */

      #include <iostream>
      #include "test.h"
      using namespace std;

      int main(int argc, char *argv[])
      {

      return 1;
      }

      ----

      test.cpp
      ----
      /*





      */

      #include <iostream>
      #include <list>
      #include "test.h"

      using namespace std;

      void xy::getI(void)
      {
      cout << i << endl;
      }

      void xy::setI(int iIN)
      {
      i = iIN;
      }


      void xyy::do_somethi ng(void)
      {
      xy *xytemp;
      xy *myXY = new xy;
      myList.push_fro nt(myXY);

      list <xy *>::iterator myIterator;
      myIterator = myList.begin();

      xytemp = *myIterator;
      delete myXY;

      }

      void xyy::output(voi d)
      {
      myIterator = myList.begin();
      xy *myXY = *myIterator;
      cout << myXY->getI() << endl;
      myList.pop_back ();

      }


      /**/

      ----

      Tanks!
      -Günter

      Comment

      • Thomas Wintschel

        #4
        Re: STL &lt;list&gt; declaration in header file


        "G?nter Omer" <guenter@omer.a t> wrote in message
        news:50a28a60.0 311212356.28226 2a5@posting.goo gle.com...[color=blue]
        > Thanks for your fast answer and excuse my uncomplete post!
        >
        > Here are all three files of the testing program, line
        > 29 is marked:
        >
        > test.h
        > ----
        > /*
        >
        >
        >
        >
        >
        > */
        >
        >
        > #ifndef TEST_HEADER
        > #define TEST_HEADER
        >
        > class xy
        > {
        > public:
        > void getI(void);
        > void setI(int);
        >
        > private:
        > int i;
        > };
        >
        > class xyy
        > {
        > public:
        > void do_something(vo id);
        >
        > private:
        > list <xy *> myList; // -> this is line 29!
        > };
        >
        > #endif
        > ----
        >
        >
        > /*
        >
        >
        >
        >
        >
        > */
        >
        > #include <iostream>
        > #include "test.h"
        > using namespace std;
        >
        > int main(int argc, char *argv[])
        > {
        >
        > return 1;
        > }
        >
        > ----
        >
        > test.cpp
        > ----
        > /*
        >
        >
        >
        >
        >
        > */
        >
        > #include <iostream>
        > #include <list>
        > #include "test.h"
        >
        > using namespace std;
        >
        > void xy::getI(void)
        > {
        > cout << i << endl;
        > }
        >
        > void xy::setI(int iIN)
        > {
        > i = iIN;
        > }
        >
        >
        > void xyy::do_somethi ng(void)
        > {
        > xy *xytemp;
        > xy *myXY = new xy;
        > myList.push_fro nt(myXY);
        >
        > list <xy *>::iterator myIterator;
        > myIterator = myList.begin();
        >
        > xytemp = *myIterator;
        > delete myXY;
        >
        > }
        >
        > void xyy::output(voi d)
        > {
        > myIterator = myList.begin();
        > xy *myXY = *myIterator;
        > cout << myXY->getI() << endl;
        > myList.pop_back ();
        >
        > }
        >
        >
        > /**/
        >
        > ----
        >
        > Tanks!
        > -Günter[/color]

        You should add the lines:

        #include <list>
        using namespace std;

        to test.h so that the compiler will know what you mean by 'list'.

        You could get away with having these two lines in the source file *before*
        including test.h but then you would need them in every source file that
        includes test.h which would be ugly, messy and bad.

        Tom


        Comment

        • Peter Koch Larsen

          #5
          Re: STL &lt;list&gt; declaration in header file


          "Thomas Wintschel" <thomwint@telus .net> skrev i en meddelelse
          news:c4Fvb.1189 75$jy.61847@clg rps13...[color=blue]
          >
          > "G?nter Omer" <guenter@omer.a t> wrote in message
          > news:50a28a60.0 311212356.28226 2a5@posting.goo gle.com...[color=green]
          > > Thanks for your fast answer and excuse my uncomplete post!
          > >
          > > Here are all three files of the testing program, line
          > > 29 is marked:
          > >
          > > test.h
          > > ----
          > > /*
          > >
          > >
          > >
          > >
          > >
          > > */
          > >
          > >
          > > #ifndef TEST_HEADER
          > > #define TEST_HEADER
          > >
          > > class xy
          > > {
          > > public:
          > > void getI(void);
          > > void setI(int);
          > >
          > > private:
          > > int i;
          > > };
          > >
          > > class xyy
          > > {
          > > public:
          > > void do_something(vo id);
          > >
          > > private:
          > > list <xy *> myList; // -> this is line 29!
          > > };
          > >
          > > #endif
          > > ----
          > >
          > >
          > > /*
          > >
          > >
          > >
          > >
          > >
          > > */
          > >
          > > #include <iostream>
          > > #include "test.h"
          > > using namespace std;
          > >
          > > int main(int argc, char *argv[])
          > > {
          > >
          > > return 1;
          > > }
          > >
          > > ----
          > >
          > > test.cpp
          > > ----
          > > /*
          > >
          > >
          > >
          > >
          > >
          > > */
          > >
          > > #include <iostream>
          > > #include <list>
          > > #include "test.h"
          > >
          > > using namespace std;
          > >
          > > void xy::getI(void)
          > > {
          > > cout << i << endl;
          > > }
          > >
          > > void xy::setI(int iIN)
          > > {
          > > i = iIN;
          > > }
          > >
          > >
          > > void xyy::do_somethi ng(void)
          > > {
          > > xy *xytemp;
          > > xy *myXY = new xy;
          > > myList.push_fro nt(myXY);
          > >
          > > list <xy *>::iterator myIterator;
          > > myIterator = myList.begin();
          > >
          > > xytemp = *myIterator;
          > > delete myXY;
          > >
          > > }
          > >
          > > void xyy::output(voi d)
          > > {
          > > myIterator = myList.begin();
          > > xy *myXY = *myIterator;
          > > cout << myXY->getI() << endl;
          > > myList.pop_back ();
          > >
          > > }
          > >
          > >
          > > /**/
          > >
          > > ----
          > >
          > > Tanks!
          > > -Günter[/color]
          >
          > You should add the lines:
          >
          > #include <list>
          > using namespace std;
          >
          > to test.h so that the compiler will know what you mean by 'list'.
          >
          > You could get away with having these two lines in the source file *before*
          > including test.h but then you would need them in every source file that
          > includes test.h which would be ugly, messy and bad.
          >
          > Tom
          >
          >[/color]
          Never put a using directive in a header-file. This is a potential source of
          problems.

          Kind regards
          Peter Koch Larsen


          Comment

          • Unforgiven

            #6
            Re: STL &lt;list&gt; declaration in header file

            Thomas Wintschel wrote:[color=blue]
            > You should add the lines:
            >
            > #include <list>
            > using namespace std;
            >
            > to test.h so that the compiler will know what you mean by 'list'.[/color]

            You should just add
            #include <list>

            Then use std::list.

            Don't put 'using' in a header.

            --
            Unforgiven

            "Most people make generalisations "
            Freek de Jonge


            Comment

            Working...