Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > include order

Reply
Thread Tools

include order

 
 
yuri
Guest
Posts: n/a
 
      09-27-2003
Hello,
I wrote this simple stack:


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

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

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

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

void StackArr:op(){
if(size==0) throw new Underflow("Stack empty");
size--;
}

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

void* StackArr::topAndPop(){
if(size==0) throw new Underflow("Stack empty");
return data[size--];
}

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

void StackArr::makeEmpty(){
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:op()':
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::topAndPop()':

Why???????
Thanks
Yuri
 
Reply With Quote
 
 
 
 
Kevin Goodsell
Guest
Posts: n/a
 
      09-27-2003
yuri wrote:
> Hello,
> I wrote this simple stack:


<snip>

>
> 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:op()':
> 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::topAndPop()':
>
> Why???????


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.

http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

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

 
Reply With Quote
 
 
 
 
Marcin Vorbrodt
Guest
Posts: n/a
 
      09-28-2003
or use std::stack template class instead


"yuri" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> Hello,
> I wrote this simple stack:
>
>
> ---START---------------
> #include "stackArr.h"
> #include "underflow.h"
> #include <iostream>
>
> StackArr::StackArr(int initialSize){
> data = new void*[initialSize];
> size = 0;
> };
>
> StackArr::~StackArr(){
> delete []data;
> }
>
> void StackArr:ush(void* x){
> data[size] = x;
> size++;
> }
>
> void StackArr:op(){
> if(size==0) throw new Underflow("Stack empty");
> size--;
> }
>
> void* StackArr::top() const{
> if(size==0) throw new Underflow("Stack empty");
> return data[size-1];
> }
>
> void* StackArr::topAndPop(){
> if(size==0) throw new Underflow("Stack empty");
> return data[size--];
> }
>
> bool StackArr::isEmpty() const{
> return size==0;
> }
>
> void StackArr::makeEmpty(){
> 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:op()':
> 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::topAndPop()':
>
> Why???????
> Thanks
> Yuri



 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      09-28-2003
Marcin Vorbrodt wrote:
> or use std::stack template class instead


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

http://www.parashift.com/c++-faq-lite/

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

 
Reply With Quote
 
yuri
Guest
Posts: n/a
 
      09-28-2003
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


 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      09-28-2003
yuri wrote:
> Sorry, I have forgotten the header!!!
>


<headers snipped>

>
> But I think I find the error. In iostream.h


There is no header called <iostream.h> in standard C++. Standard C++
uses <iostream>.

> there is the definition of
> macro UNDERFLOW (I think...)


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.

> 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?


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.

 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      09-28-2003
yuri wrote:

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


Implicit conversion from a string literal to a (non-const) char pointer
is dangerous and deprecated. Always use const char* for this purpose.

> private:
> char* err;
> };
>
> #endif //UNDERFLOW
>
>
> ---stackArr.h---
> #include "stack.h"
>
> class StackArr: public Stack{


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.

 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      09-28-2003
Kevin Goodsell wrote:
>
> yuri wrote:
> > Sorry, I have forgotten the header!!!
> >

>
> <headers snipped>
>
> >
> > But I think I find the error. In iostream.h

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

>
> This would be illegal. Standard headers are required to use identifiers
> that don't conflict with identifiers the programmer may use.


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)
 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      09-29-2003
Pete Becker wrote:

> Kevin Goodsell wrote:
>>
>>This would be illegal. Standard headers are required to use identifiers
>>that don't conflict with identifiers the programmer may use.

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


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.

 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      09-29-2003
Kevin Goodsell wrote:
>
> Pete Becker wrote:
>
> > Kevin Goodsell wrote:
> >>
> >>This would be illegal. Standard headers are required to use identifiers
> >>that don't conflict with identifiers the programmer may use.

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

>
> 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).
>


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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
/* #include <someyhing.h> */ => include it or do not include it?That is the question .... Andreas Bogenberger C Programming 3 02-22-2008 10:53 AM
#include headers that include this header Aguilar, James C++ 2 07-16-2004 05:56 PM
Re: the use of #include <a_file.h> v/s #include"a_file.cpp" Elie Nader C++ 1 11-28-2003 03:12 PM
Re: the use of #include <a_file.h> v/s #include"a_file.cpp" Rolf Magnus C++ 2 11-28-2003 12:26 PM
#include "bar" negates #include <string> ; how to fix? Danny Anderson C++ 5 08-15-2003 06:38 PM



Advertisments