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::push(void* x){
data[size] = x;
size++;
}
void StackArr::pop(){
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::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::topAndPop()':
Why???????
Thanks
Yuri 14 4288
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::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::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.
or use std::stack template class instead:-)
"yuri" <yu**@nospam.it> wrote in message
news:pa****************************@nospam.it... 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::push(void* x){ data[size] = x; size++; }
void StackArr::pop(){ 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::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::topAndPop()':
Why??????? Thanks Yuri
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.
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
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.
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.
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)
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.
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)
"Kevin Goodsell" <us*********************@neverbox.com> wrote in message
news:JU*****************@newsread4.news.pas.earthl ink.net... 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.
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_UNDERFLOW_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
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.tcc: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
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"<<endl;
}
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
yuri wrote: 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"<<endl; }
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?
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.
Kevin Goodsell wrote: 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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Holden |
last post by:
Hello,
I am using Visual C++ 6.0 with Service Pack 5 installed and I have run
into a problem that I haven't been able to figure out. I have written
two classes and I want them to have pointers to...
|
by: Mario T. Lanza |
last post by:
If you're a web designer, you have undoubtedly used cascading style
sheets in order to improve your sites separation of form and content.
You probably also use Server-side include files to include...
|
by: juglesh |
last post by:
please comment on the following methods of preventing cross site
scripting and/or other nastiness:
1:
$pages = array('home','contact','about','links' );
// could also build this array with...
|
by: atv |
last post by:
Alright, i have some questions concerning include files en global
variables.I hope someone is willing to answer these.
1).Why is it that if i define a global variable in a file, say main.c,
and...
|
by: Derrick Coetzee |
last post by:
It seems like, in every C source file I've ever seen, there has been a
very definite include order, as follows:
- include system headers
- include application headers
- include the header...
| |
by: David Mathog |
last post by:
One thing that can make porting C code from one
platform to another miserable is #include. In particular,
the need to either place the path to an included file within
the #include statement or to...
|
by: Ben Taylor |
last post by:
I've asked about includes before in this group in order to
stop files being #included either twice or not at all, and
somebody said to use extern keyword for global variables
that need to be used...
|
by: Arpi Jakab |
last post by:
I have a main project that depends on projects A and B. The main project's
additional include directories list is:
...\ProjectA\Dist\Include
...\ProjectB\Dist\Include
Each of the include...
|
by: Jim Bancroft |
last post by:
Hi,
I've used VC++ 6 for my previous C++ work and am having a little trouble
acclimating to VS .Net 2003, mainly in establishing where to look for
additional header and library files. I'm...
|
by: Marco Spatz |
last post by:
Hi,
I used PCLint to check my code yesterday and got some warnings about
"Repeated include files" and "Redundant declaration for symbol
'CLASSNAME'". I know the reasons for these warnings and I...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |