include order

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • yuri

    include order

    Hello,
    I wrote this simple stack:


    ---START---------------
    #include "stackArr.h "
    #include "underflow. h"
    #include <iostream>

    StackArr::Stack Arr(int initialSize){
    data = new void*[initialSize];
    size = 0;
    };

    StackArr::~Stac kArr(){
    delete []data;
    }

    void StackArr::push( void* x){
    data[size] = x;
    size++;
    }

    void StackArr::pop() {
    if(size==0) throw new Underflow("Stac k empty");
    size--;
    }

    void* StackArr::top() const{
    if(size==0) throw new Underflow("Stac k empty");
    return data[size-1];
    }

    void* StackArr::topAn dPop(){
    if(size==0) throw new Underflow("Stac k empty");
    return data[size--];
    }

    bool StackArr::isEmp ty() const{
    return size==0;
    }

    void StackArr::makeE mpty(){
    delete []data;
    data = new void*[10];
    size=0;
    }

    int main(){
    cout<<"StackArr "<<endl;
    }

    ---END-----------------

    When I compile this version there isn't problem, but when I change the
    include order (first iostream)

    #include <iostream>
    #include "stackArr.h "
    #include "underflow. h"

    I have compile errors:
    stackArr.cpp: In member function `virtual void StackArr::pop() ':
    stackArr.cpp:23 : parse error before `(' token
    stackArr.cpp: In member function `virtual void* StackArr::top() const':
    stackArr.cpp:28 : parse error before `(' token
    stackArr.cpp: In member function `virtual void* StackArr::topAn dPop()':

    Why???????
    Thanks
    Yuri
  • Kevin Goodsell

    #2
    Re: include order

    yuri wrote:[color=blue]
    > Hello,
    > I wrote this simple stack:[/color]

    <snip>
    [color=blue]
    >
    > When I compile this version there isn't problem, but when I change the
    > include order (first iostream)
    >
    > #include <iostream>
    > #include "stackArr.h "
    > #include "underflow. h"
    >
    > I have compile errors:
    > stackArr.cpp: In member function `virtual void StackArr::pop() ':
    > stackArr.cpp:23 : parse error before `(' token
    > stackArr.cpp: In member function `virtual void* StackArr::top() const':
    > stackArr.cpp:28 : parse error before `(' token
    > stackArr.cpp: In member function `virtual void* StackArr::topAn dPop()':
    >
    > Why???????[/color]

    Probably because you've done something wrong in your header files. We
    don't know what, since you didn't bother to include the code from those
    files.



    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.

    Comment

    • Marcin Vorbrodt

      #3
      Re: include order

      or use std::stack template class instead:-)


      "yuri" <yuri@nospam.it > wrote in message
      news:pan.2003.0 9.28.00.26.16.1 40652@nospam.it ...[color=blue]
      > Hello,
      > I wrote this simple stack:
      >
      >
      > ---START---------------
      > #include "stackArr.h "
      > #include "underflow. h"
      > #include <iostream>
      >
      > StackArr::Stack Arr(int initialSize){
      > data = new void*[initialSize];
      > size = 0;
      > };
      >
      > StackArr::~Stac kArr(){
      > delete []data;
      > }
      >
      > void StackArr::push( void* x){
      > data[size] = x;
      > size++;
      > }
      >
      > void StackArr::pop() {
      > if(size==0) throw new Underflow("Stac k empty");
      > size--;
      > }
      >
      > void* StackArr::top() const{
      > if(size==0) throw new Underflow("Stac k empty");
      > return data[size-1];
      > }
      >
      > void* StackArr::topAn dPop(){
      > if(size==0) throw new Underflow("Stac k empty");
      > return data[size--];
      > }
      >
      > bool StackArr::isEmp ty() const{
      > return size==0;
      > }
      >
      > void StackArr::makeE mpty(){
      > delete []data;
      > data = new void*[10];
      > size=0;
      > }
      >
      > int main(){
      > cout<<"StackArr "<<endl;
      > }
      >
      > ---END-----------------
      >
      > When I compile this version there isn't problem, but when I change the
      > include order (first iostream)
      >
      > #include <iostream>
      > #include "stackArr.h "
      > #include "underflow. h"
      >
      > I have compile errors:
      > stackArr.cpp: In member function `virtual void StackArr::pop() ':
      > stackArr.cpp:23 : parse error before `(' token
      > stackArr.cpp: In member function `virtual void* StackArr::top() const':
      > stackArr.cpp:28 : parse error before `(' token
      > stackArr.cpp: In member function `virtual void* StackArr::topAn dPop()':
      >
      > Why???????
      > Thanks
      > Yuri[/color]


      Comment

      • Kevin Goodsell

        #4
        Re: include order

        Marcin Vorbrodt wrote:[color=blue]
        > or use std::stack template class instead:-)[/color]

        Please don't top-post, and please trim when replying. Read section 5 of
        the FAQ for posting guidelines.



        -Kevin
        --
        My email address is valid, but changes periodically.
        To contact me please use the address from a recent posting.

        Comment

        • yuri

          #5
          Re: include order

          Sorry, I have forgotten the header!!!

          ---underflow.h---
          #ifndef UNDERFLOW
          #define UNDERFLOW

          class Underflow{
          public:
          Underflow(char* e="Underflow Exception"): err(e){};
          private:
          char* err;
          };

          #endif //UNDERFLOW


          ---stackArr.h---
          #include "stack.h"

          class StackArr: public Stack{
          public:
          StackArr(int initialSize=10) ;
          ~StackArr();
          void push(void*);
          void pop();
          void* top() const;
          void* topAndPop();
          bool isEmpty() const;
          void makeEmpty();
          private:
          void** data;
          int size;
          };

          ---stack.h--
          #ifndef STACK
          #define STACK

          class Stack{
          public:
          //Stack(){};
          virtual void push(void*) = 0;
          virtual void pop() = 0;
          virtual void* top() const = 0;
          virtual void* topAndPop() = 0;
          virtual bool isEmpty() const = 0;
          virtual void makeEmpty() = 0;
          };

          #endif

          But I think I find the error. In iostream.h there is the definition of
          macro UNDERFLOW (I think...) so when iostream is include at the begining,
          the macro UNDERFLOW is define and the definition of class Underflow is not
          included because I use the same macro name. Is it ok?
          Thanks
          Yuri


          Comment

          • Kevin Goodsell

            #6
            Re: include order

            yuri wrote:[color=blue]
            > Sorry, I have forgotten the header!!!
            >[/color]

            <headers snipped>
            [color=blue]
            >
            > But I think I find the error. In iostream.h[/color]

            There is no header called <iostream.h> in standard C++. Standard C++
            uses <iostream>.
            [color=blue]
            > there is the definition of
            > macro UNDERFLOW (I think...)[/color]

            This would be illegal. Standard headers are required to use identifiers
            that don't conflict with identifiers the programmer may use. So, the
            standard header could use _UNDERFLOW because identifiers beginning with
            an underscore followed by an uppercase letter are reserved for that
            purpose. But it may not use UNDERFLOW - that identifier is reserved for
            the programmer's use.
            [color=blue]
            > so when iostream is include at the begining,
            > the macro UNDERFLOW is define and the definition of class Underflow is not
            > included because I use the same macro name. Is it ok?[/color]

            If that's the problem, your implementation is broken. I haven't taken a
            close look at the headers you posted yet. I'll let you know if I see a
            problem there.

            -Kevin
            --
            My email address is valid, but changes periodically.
            To contact me please use the address from a recent posting.

            Comment

            • Kevin Goodsell

              #7
              Re: include order

              yuri wrote:
              [color=blue]
              > Sorry, I have forgotten the header!!!
              >
              > ---underflow.h---
              > #ifndef UNDERFLOW
              > #define UNDERFLOW
              >
              > class Underflow{
              > public:
              > Underflow(char* e="Underflow Exception"): err(e){};[/color]

              Implicit conversion from a string literal to a (non-const) char pointer
              is dangerous and deprecated. Always use const char* for this purpose.
              [color=blue]
              > private:
              > char* err;
              > };
              >
              > #endif //UNDERFLOW
              >
              >
              > ---stackArr.h---
              > #include "stack.h"
              >
              > class StackArr: public Stack{[/color]

              No include guards for this file? That's asking for trouble.

              Other than this, I was not able to find a problem in the code or
              reproduce your error. Perhaps if you told us what compiler and library
              you are using (including version numbers) and posted the exact code that
              gave the error (the posted code is illegal as is - 'cout' and 'endl' are
              undeclared) we could be of more help.

              -Kevin
              --
              My email address is valid, but changes periodically.
              To contact me please use the address from a recent posting.

              Comment

              • Pete Becker

                #8
                Re: include order

                Kevin Goodsell wrote:[color=blue]
                >
                > yuri wrote:[color=green]
                > > Sorry, I have forgotten the header!!!
                > >[/color]
                >
                > <headers snipped>
                >[color=green]
                > >
                > > But I think I find the error. In iostream.h[/color]
                >
                > There is no header called <iostream.h> in standard C++. Standard C++
                > uses <iostream>.
                >[color=green]
                > > there is the definition of
                > > macro UNDERFLOW (I think...)[/color]
                >
                > This would be illegal. Standard headers are required to use identifiers
                > that don't conflict with identifiers the programmer may use.[/color]

                You can't have it both ways. If <iostream.h> isn't a standard header, it
                shouldn't use names in the implementors' namespace.

                --

                Pete Becker
                Dinkumware, Ltd. (http://www.dinkumware.com)

                Comment

                • Kevin Goodsell

                  #9
                  Re: include order

                  Pete Becker wrote:
                  [color=blue]
                  > Kevin Goodsell wrote:[color=green]
                  >>
                  >>This would be illegal. Standard headers are required to use identifiers
                  >>that don't conflict with identifiers the programmer may use.[/color]
                  >
                  >
                  > You can't have it both ways. If <iostream.h> isn't a standard header, it
                  > shouldn't use names in the implementors' namespace.
                  >[/color]

                  True, but the original code actually used <iostream>. So, if the poster
                  actually meant <iostream> rather than <iostream.h>, what I said is true
                  (unless I've made a mistake).

                  -Kevin
                  --
                  My email address is valid, but changes periodically.
                  To contact me please use the address from a recent posting.

                  Comment

                  • Pete Becker

                    #10
                    Re: include order

                    Kevin Goodsell wrote:[color=blue]
                    >
                    > Pete Becker wrote:
                    >[color=green]
                    > > Kevin Goodsell wrote:[color=darkred]
                    > >>
                    > >>This would be illegal. Standard headers are required to use identifiers
                    > >>that don't conflict with identifiers the programmer may use.[/color]
                    > >
                    > >
                    > > You can't have it both ways. If <iostream.h> isn't a standard header, it
                    > > shouldn't use names in the implementors' namespace.
                    > >[/color]
                    >
                    > True, but the original code actually used <iostream>. So, if the poster
                    > actually meant <iostream> rather than <iostream.h>, what I said is true
                    > (unless I've made a mistake).
                    >[/color]

                    He said he found the problem, and he described it. It involved a macro
                    defined in <iostream.h>. There's nothing more to say.

                    --

                    Pete Becker
                    Dinkumware, Ltd. (http://www.dinkumware.com)

                    Comment

                    • Ron Natalie

                      #11
                      Re: include order


                      "Kevin Goodsell" <usenet1.spamfr ee.fusion@never box.com> wrote in message
                      news:JUHdb.6905 $RW4.1838@newsr ead4.news.pas.e arthlink.net...[color=blue]
                      > If that's the problem, your implementation is broken. I haven't taken a
                      > close look at the headers you posted yet. I'll let you know if I see a
                      > problem there.[/color]

                      It looks like that may indeed be his problem. For whatever reason it looks like
                      Underflow is not defined at the point of the error. I suggest he use a more elaborate
                      include guard variable. Even if the standard headers don't use (legitimately) names
                      of that form, UNDERFLOW, is a symbol that's likely to collide with somebodies
                      header or program at somepoint. I tend to do things like
                      INCLUDE_UNDERFL OW_H
                      not fool proof, but less likely to be anything other than an include guard.

                      A cute hack to determine whether UNDERFLOW is infact being defined prematurely
                      is to insert a definition for it prior to any #includes:


                      #define UNDERFLOW 1.2.3.4 // almost sure to be a syntax error when used.
                      #include "stackArr.h "
                      #include "...

                      You will most likely get an error from the preprocessor the first time UNDERFLOW is defined
                      or used after it's definition above.

                      -Ron


                      Comment

                      • yuri

                        #12
                        Re: include order

                        Thanks Kevin for your useful help and attention!
                        I have found the definition of UNDERFLOW macro! I wrote a simple program
                        using the Ron's idea and I found the macro UNDERFLOW's definition.
                        It's in /usr/include/math.h and the error output of compiler is (x.cpp is
                        my test program:

                        x.cpp:2:1: warning: "UNDERFLOW" redefined
                        In file included from /usr/include/c++/3.2.2/cmath:51,
                        from /usr/include/c++/3.2.2/bits/locale_facets.t cc:41,
                        from /usr/include/c++/3.2.2/locale:46,
                        from /usr/include/c++/3.2.2/bits/ostream.tcc:37,
                        from /usr/include/c++/3.2.2/ostream:275,
                        from /usr/include/c++/3.2.2/iostream:45,
                        from x.cpp:1:
                        /usr/include/math.h:299:1: warning: this is the location of the previous definition

                        Now I will use only complicated header :-))
                        Bye
                        Yuri

                        Comment

                        • yuri

                          #13
                          Re: include order

                          I have another problem!
                          My x.cpp file is:

                          #include <iostream>
                          #define UNDERFLOW 2

                          using namespace std;

                          int main(){
                          #ifdef UNDERFLOW
                          cout<<UNDERFLOW <<endl;
                          #endif
                          cout<<"end"<<en dl;
                          }

                          Compiling I have warning (see my last post) that tell me UNDERFLOW is
                          define in math.h (/usr/include/math.h) and it's value is 4.
                          But if I swap the first two rows:

                          #define UNDERFLOW 2
                          #include <iostream>
                          ....
                          I haven't compiling error or warning and cout prints the math.h value (4).
                          Why? Maybe the macros of math.h have precedence over mine?
                          Thanks
                          Yuri

                          Comment

                          • Kevin Goodsell

                            #14
                            Re: include order

                            yuri wrote:[color=blue]
                            > I have another problem!
                            > My x.cpp file is:
                            >
                            > #include <iostream>
                            > #define UNDERFLOW 2
                            >
                            > using namespace std;
                            >
                            > int main(){
                            > #ifdef UNDERFLOW
                            > cout<<UNDERFLOW <<endl;
                            > #endif
                            > cout<<"end"<<en dl;
                            > }
                            >
                            > Compiling I have warning (see my last post) that tell me UNDERFLOW is
                            > define in math.h (/usr/include/math.h) and it's value is 4.
                            > But if I swap the first two rows:
                            >
                            > #define UNDERFLOW 2
                            > #include <iostream>
                            > ...
                            > I haven't compiling error or warning and cout prints the math.h value (4).
                            > Why? Maybe the macros of math.h have precedence over mine?[/color]

                            It could do

                            #undef UNDERFLOW
                            #define UNDERFLOW 4

                            easily enough.

                            But the main issue here is that UNDERFLOW is reserved for the
                            programmer's use - your standard library SHOULD NOT #define it. The
                            implementation is broken. You should check if a fix is available.

                            -Kevin
                            --
                            My email address is valid, but changes periodically.
                            To contact me please use the address from a recent posting.

                            Comment

                            • Kevin Goodsell

                              #15
                              Re: include order

                              Kevin Goodsell wrote:
                              [color=blue]
                              > But the main issue here is that UNDERFLOW is reserved for the
                              > programmer's use - your standard library SHOULD NOT #define it. The
                              > implementation is broken. You should check if a fix is available.[/color]

                              Actually, I bet the fix is to invoke your compiler correctly. Try
                              compiling with the following options:

                              -W -Wall -ansi -pedantic

                              If I am reading the docs right, -ansi applies for C++ mode also (which
                              is a little weird I think, since ISO, not ANSI, standardized C++ (OK, it
                              was a joint ANSI/ISO group, but ISO takes precedence, IMO)).

                              I looked at the math.h header, and it looks like UNDERFLOW is
                              conditionally added as part of some kind of System V compatibility
                              thing. My guess is that the options I listed will fix it.

                              -Kevin
                              --
                              My email address is valid, but changes periodically.
                              To contact me please use the address from a recent posting.

                              Comment

                              Working...