 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Michal Augustyniak Guest
|
Posted: Mon Dec 20, 2004 12:43 am Post subject: E2303 |
|
|
Here's the thing:
I'm supposed to specialize a templated class so that it takes a certain
datatype. Say I want it to take integer data.
Day I get that right but my class functions all take Object data. Here's
the deal, how can I make the templated class
accept Object type data without the compile barking at me to specialize
the datatype? Otherwise, I keep getting an e2303 on all my class
functions, and my driver can't see my class functions.
Here's the code
//driver
//the driver for my ArrayStack
#include <iostream>
#include <string>
#include <stdlib.h>
#include "ArrayStack.h"
//#include "StackEmptyException.h"
//#include "StackFullException.h"
using namespace std;
//line 10
int main ()
{
//What I want this program to do is to fill two stack arrays: the first
one will be user-defined and will fill it //properly
//within the specified range. The stack will then be popped. The second
array will then be filled improperly and will
//throw an exception. Since the stuff doesn't work properly yet, I'll
avoid using pointers
//I need to use the full pathname to use ArrayStack to create objects
of its type
// typedef ArrayStack<int> ArrayStack; //I've used typedef to define the
datatype ArrayStack as the type for AS,
//I think I have to pass it a specific datatype to specialize
int size1;
int size2 = 3; //the respective array's sizes, 20
ArrayStack as2(size2);
int input; //I'd rather input Object data, later when I get it working
cout<<"What size of array stack you want to create? "<
cin>>size1;
ArrayStack as1(size1); //I could just develop setters for the size,
once I get my program running
cout<<"Now we'll fill your array"<
for( int i = 0; i < size1; i++) //30
{
cout<<"Please enter the next integer"<
cin>>input;
as1.push(input);
}
cout<<"Now we'll pop your stack"<
while(!(as1.isEmpty()))
{
cout<
}
cout<<"Now that we've finished with array 1, we'll try to overload
array 2"<
for (int i = 0; i <= size2; i++)
{
cout<<"You can enter element "<
cin>>input;
as2.push(input);
}
cout<<"Now you've seen a working array an a failed one."<
return 0;
}
//class file
//template
class ArrayStack {
private: // member data
enum { CAPACITY = 1000 }; // default capacity of stack
int capacity; // actual length of stack array
Object* S; // the stack array
int t; // index of the top of the stack
public:
// constructor given max capacity
ArrayStack(int cap = CAPACITY) { //10
capacity = cap;
S = new Object[capacity];
t = -1;
}
int size() const // number of elements in the stack
{ return (t + 1); }
bool isEmpty() const // is the stack empty?
{ return (t < 0); }
Object& top() throw(StackEmptyException) //20, Keeps giving me a Type
name expected
{
if (isEmpty())
throw StackEmptyException("Access to empty stack");
return [t];
}
// push object onto the stack, also gives me a Type name expected
void push(const Object& elem) throw(StackFullException)
{
if (size() == capacity)
throw StackFullException("Stack overflow");
S[++t] = elem;
}
// pop the stack
Object pop() throw(StackEmptyException)
{
if (isEmpty())
throw StackEmptyException("Access to empty stack");
return S[t--];
}
ArrayStack(const ArrayStack& st); // copy constructor
ArrayStack& operator=(const ArrayStack& st); // assignment operator
constructor
~ArrayStack() // destructor
{ delete [] S; }
};
/*template
ArrayStack<Object>::
ArrayStack(const ArrayStack& st) {
capacity = st.capacity;
t = st.t;
S = new Object[capacity];
for (int i = 0; i <= t; i++) { // copy contents
S[i] = st.S[i];
}
}*/
template
ArrayStack<Object>& ArrayStack<Object>::
operator=(const ArrayStack& st) {
if (this != &st) { // avoid self copy (x = x)
delete [] S; // delete old contents
capacity = st.capacity;
t = st.t;
S = new Object[capacity];
for (int i = 0; i <= t; i++) { // copy contents
S[i] = st.S[i];
}
}
return *this;
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alberto Barbati Guest
|
Posted: Mon Dec 20, 2004 10:33 am Post subject: Re: E2303 |
|
|
Michal Augustyniak wrote:
| Quote: | Here's the thing:
I'm supposed to specialize a templated class so that it takes a certain
datatype. Say I want it to take integer data.
Day I get that right but my class functions all take Object data. Here's
the deal, how can I make the templated class
accept Object type data without the compile barking at me to specialize
the datatype? Otherwise, I keep getting an e2303 on all my class
functions, and my driver can't see my class functions.
|
Your explanation is very confusing. I could not understand a word.
What's the problem exactly?
Moreover:
1) Saying "E2303" is not useful at all if we don't know the compiler
which compiler you are using. At least write the entire message in full.
2) Please attach a code snippet that we can really (try to) compile.
Your code is missing too many pieces (for example StackEmptyException.h
and StackFullException.h).
3) Please try to post the *smallest possible* code that reproduces your
problem. If you put so much code you show that you did not put any
effort in trying to make you understood and that really irritates people.
Alberto
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Pavel Vozenilek Guest
|
Posted: Mon Dec 20, 2004 10:46 am Post subject: Re: E2303 |
|
|
"Michal Augustyniak" wrote:
| Quote: | Otherwise, I keep getting an e2303 ....
[snip code example] |
1. This looks like Borland C++ Builder specific.
provblem. You may have more luck in
borland.public.cppbuilder.language newsgroup.
2. The code example could be surely smaller
and contained in just one file.
/Pavel
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|