473,382 Members | 1,329 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Problem declaring pointer of netsted class inside template ?

Can anybody tell me what is wrong with declaration of pointer p ?

template<class Tclass Stack {
struct Link {
T* data;
Link* next;
Link(T* dat, Link* nxt)
: data(dat), next(nxt) {}
}* head;
public:
Stack() : head(0) {}
~Stack();
void push(T* dat) {
head = new Link(dat, head);
}
T* peek() const {
return head ? head->data : 0;
}
T* pop();
// Nested iterator class:
class iterator; // Declaration required
friend class iterator; // Make it a friend
class iterator { // Now define it
Stack::Link* p;
public:
iterator(const Stack<T>& tl) : p(tl.head) {}
// Copy-constructor:
iterator(const iterator& tl) : p(tl.p) {}
// The end sentinel iterator:
iterator() : p(0) {}
// operator++ returns boolean indicating end:
bool operator++() {
if(p->next)
p = p->next;
else p = 0; // Indicates end of list
return bool(p);
}
bool operator++(int) { return operator++(); }
T* current() const {
if(!p) return 0;
return p->data;
}
// Pointer dereference operator:
T* operator->() const {
require(p != 0,
"PStack::iterator::operator->returns 0");
return current();
}
T* operator*() const { return current(); }
// bool conversion for conditional test:
operator bool() const { return bool(p); }
// Comparison to test for end:
bool operator==(const iterator&) const {
return p == 0;
}
bool operator!=(const iterator&) const {
return p != 0;
}
};
iterator begin() const {
return iterator(*this);
}
iterator end() const { return iterator(); }
};

template<class TStack<T>::~Stack() {
while(head)
delete pop();
}

template<class TT* Stack<T>::pop() {
if(head == 0) return 0;
T* result = head->data;
Link* oldHead = head;
head = head->next;
delete oldHead;
return result;
}

int main(){
}
Jul 30 '06 #1
8 1954
flamexx7 wrote:
Can anybody tell me what is wrong with declaration of pointer p ?
What does your compiler say about it? I tested your code with Comeau
online test, and it compiles fine.
template<class Tclass Stack {
struct Link {
T* data;
Link* next;
Link(T* dat, Link* nxt)
: data(dat), next(nxt) {}
}* head;
public:
Stack() : head(0) {}
~Stack();
void push(T* dat) {
head = new Link(dat, head);
}
T* peek() const {
return head ? head->data : 0;
}
T* pop();
// Nested iterator class:
class iterator; // Declaration required
friend class iterator; // Make it a friend
class iterator { // Now define it
Stack::Link* p;
I am guessing you're referring to the line above. What is the error
message you get (if any)?
public:
iterator(const Stack<T>& tl) : p(tl.head) {}
// Copy-constructor:
iterator(const iterator& tl) : p(tl.p) {}
// The end sentinel iterator:
iterator() : p(0) {}
// operator++ returns boolean indicating end:
bool operator++() {
if(p->next)
p = p->next;
else p = 0; // Indicates end of list
return bool(p);
}
bool operator++(int) { return operator++(); }
T* current() const {
if(!p) return 0;
return p->data;
}
// Pointer dereference operator:
T* operator->() const {
require(p != 0,
"PStack::iterator::operator->returns 0");
return current();
}
T* operator*() const { return current(); }
// bool conversion for conditional test:
operator bool() const { return bool(p); }
// Comparison to test for end:
bool operator==(const iterator&) const {
return p == 0;
}
bool operator!=(const iterator&) const {
return p != 0;
}
};
iterator begin() const {
return iterator(*this);
}
iterator end() const { return iterator(); }
};

template<class TStack<T>::~Stack() {
while(head)
delete pop();
}

template<class TT* Stack<T>::pop() {
if(head == 0) return 0;
T* result = head->data;
Link* oldHead = head;
head = head->next;
delete oldHead;
return result;
}

int main(){
You might want to try to use your template so that the compiler
actually attempts to instantiate the class.
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 30 '06 #2
In article <44***********************@news.sunsite.dk>,
"flamexx7" <none@nonewrote:
Can anybody tell me what is wrong with declaration of pointer p ?
Stack is a templated type and you don't use the template parameter when
declaring p. Also, Stack<T>::Link is a dependent type, so you have to
use the "typename" keyword. So declare it like this:

typename Stack<T>::Link* p;
Jul 30 '06 #3
Daniel T. wrote:
In article <44***********************@news.sunsite.dk>,
"flamexx7" <none@nonewrote:
>Can anybody tell me what is wrong with declaration of pointer p ?

Stack is a templated type and you don't use the template parameter
when declaring p. Also, Stack<T>::Link is a dependent type, so you
have to use the "typename" keyword. So declare it like this:

typename Stack<T>::Link* p;
Really? Comeau compiles the original program without a peep.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 30 '06 #4
I use Bloodshed 4.9.9.2 and after I applied solution provided by Daniel, my
code compiles fine.
"Victor Bazarov" <v.********@comAcast.netwrote in message
news:zI******************************@comcast.com. ..
Daniel T. wrote:
>In article <44***********************@news.sunsite.dk>,
"flamexx7" <none@nonewrote:
>>Can anybody tell me what is wrong with declaration of pointer p ?

Stack is a templated type and you don't use the template parameter
when declaring p. Also, Stack<T>::Link is a dependent type, so you
have to use the "typename" keyword. So declare it like this:

typename Stack<T>::Link* p;

Really? Comeau compiles the original program without a peep.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Jul 30 '06 #5
Ok I found it. It's discuessed in section 14.6 of C++ Standard 2nd edtiion.
So after reading that, I figured out 3 simple ways :-

1. typename Stack<T>::Link* p;

2. We can skip template parametres.
typename Stack::Link* p;

3. We even need to provide name of class in which nested class is declared.
Link* p;
"flamexx7" <none@nonewrote in message
news:44***********************@news.sunsite.dk...
Can anybody tell me what is wrong with declaration of pointer p ?

template<class Tclass Stack {
struct Link {
T* data;
Link* next;
Link(T* dat, Link* nxt)
: data(dat), next(nxt) {}
}* head;
public:
Stack() : head(0) {}
~Stack();
void push(T* dat) {
head = new Link(dat, head);
}
T* peek() const {
return head ? head->data : 0;
}
T* pop();
// Nested iterator class:
class iterator; // Declaration required
friend class iterator; // Make it a friend
class iterator { // Now define it
Stack::Link* p;
public:
iterator(const Stack<T>& tl) : p(tl.head) {}
// Copy-constructor:
iterator(const iterator& tl) : p(tl.p) {}
// The end sentinel iterator:
iterator() : p(0) {}
// operator++ returns boolean indicating end:
bool operator++() {
if(p->next)
p = p->next;
else p = 0; // Indicates end of list
return bool(p);
}
bool operator++(int) { return operator++(); }
T* current() const {
if(!p) return 0;
return p->data;
}
// Pointer dereference operator:
T* operator->() const {
require(p != 0,
"PStack::iterator::operator->returns 0");
return current();
}
T* operator*() const { return current(); }
// bool conversion for conditional test:
operator bool() const { return bool(p); }
// Comparison to test for end:
bool operator==(const iterator&) const {
return p == 0;
}
bool operator!=(const iterator&) const {
return p != 0;
}
};
iterator begin() const {
return iterator(*this);
}
iterator end() const { return iterator(); }
};

template<class TStack<T>::~Stack() {
while(head)
delete pop();
}

template<class TT* Stack<T>::pop() {
if(head == 0) return 0;
T* result = head->data;
Link* oldHead = head;
head = head->next;
delete oldHead;
return result;
}

int main(){
}


Jul 30 '06 #6
In article <zI******************************@comcast.com>,
"Victor Bazarov" <v.********@comAcast.netwrote:
Daniel T. wrote:
In article <44***********************@news.sunsite.dk>,
"flamexx7" <none@nonewrote:
Can anybody tell me what is wrong with declaration of pointer p ?
Stack is a templated type and you don't use the template parameter
when declaring p. Also, Stack<T>::Link is a dependent type, so you
have to use the "typename" keyword. So declare it like this:

typename Stack<T>::Link* p;

Really? Comeau compiles the original program without a peep.
Gnu doesn't. I wonder what the standard says...
Jul 30 '06 #7
flamexx7 wrote:
I use Bloodshed 4.9.9.2 and after I applied solution provided by
Daniel, my code compiles fine.
Please don't top-post. See the FAQ entry at:
<http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.4>

Brian
Jul 30 '06 #8
Daniel T. wrote:
In article <zI******************************@comcast.com>,
"Victor Bazarov" <v.********@comAcast.netwrote:
>Daniel T. wrote:
>>In article <44***********************@news.sunsite.dk>,
"flamexx7" <none@nonewrote:

Can anybody tell me what is wrong with declaration of pointer p ?

Stack is a templated type and you don't use the template parameter
when declaring p. Also, Stack<T>::Link is a dependent type, so you
have to use the "typename" keyword. So declare it like this:

typename Stack<T>::Link* p;

Really? Comeau compiles the original program without a peep.

Gnu doesn't. I wonder what the standard says...
It's a Gnu's problem. In the original post, 'iterator' is a member,
that means 'Stack' in it refers to the outer template Stack. That
means that Stack::Link is _not_ a dependent name since 'iterator' is
not translated _independently_ of 'Stack'.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 30 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: r5 | last post by:
I'm using the MIPSpro Compiler and having trouble defining a function template (involving array size specifiers as template arguments) inside a class. The same definition compiles fine outside...
2
by: Brandon | last post by:
In a templated class I wrote, there is a public typedef for a function pointer. The class has an instance of the function pointer as a private member. That pointer is declared as static so I need...
2
by: J. Wolfram | last post by:
Hi! I need to write a template function which accepts a pointer (and a parameter for the byte-offset between the elements and a parameter for the number of elements.). The different...
11
by: Milind | last post by:
Hi, I was trying to implement a composition relation, somthing of the following type: class A { public: class B {
0
by: Adriano Coser | last post by:
Hello. I'm trying to compile a DLL with Visual C++ .NET 2003 and I'm facing an error declaring template variables with prototypes. I donīt know if I'll be able to explain my scenario, but...
5
by: Wayne Shu | last post by:
Hi, guys I am reading Vandevoorde and Josuttis 's "C++ Template The Complete Guide" these days. When I read the chapter 15: Traits and Policy classes. I copy the code in 15.2.2 that use to...
2
weaknessforcats
by: weaknessforcats | last post by:
Handle Classes Handle classes, also called Envelope or Cheshire Cat classes, are part of the Bridge design pattern. The objective of the Bridge pattern is to separate the abstraction from the...
4
by: Immortal_Nephi | last post by:
You have written several global functions inside header code. Then, you create either static library or dynamic linked library. All global functions can be reuseable to the main() function when...
7
by: ghulands | last post by:
I am having trouble implementing some function pointer stuff in c++ An object can register itself for many events void addEventListener(CFObject *target, CFEventHandler callback, uint8_t...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.