473,799 Members | 3,052 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::iterat or::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==(cons t iterator&) const {
return p == 0;
}
bool operator!=(cons t iterator&) const {
return p != 0;
}
};
iterator begin() const {
return iterator(*this) ;
}
iterator end() const { return iterator(); }
};

template<class TStack<T>::~Sta ck() {
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 1979
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::iterat or::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==(cons t iterator&) const {
return p == 0;
}
bool operator!=(cons t iterator&) const {
return p != 0;
}
};
iterator begin() const {
return iterator(*this) ;
}
iterator end() const { return iterator(); }
};

template<class TStack<T>::~Sta ck() {
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************ ***********@new s.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************ ***********@new s.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.********@com Acast.netwrote in message
news:zI******** *************** *******@comcast .com...
Daniel T. wrote:
>In article <44************ ***********@new s.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@nonewro te in message
news:44******** *************** @news.sunsite.d k...
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::iterat or::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==(cons t iterator&) const {
return p == 0;
}
bool operator!=(cons t iterator&) const {
return p != 0;
}
};
iterator begin() const {
return iterator(*this) ;
}
iterator end() const { return iterator(); }
};

template<class TStack<T>::~Sta ck() {
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.********@com Acast.netwrote:
Daniel T. wrote:
In article <44************ ***********@new s.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.c om/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.********@com Acast.netwrote:
>Daniel T. wrote:
>>In article <44************ ***********@new s.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
1159
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 the class. I have listed a short sample code and resulting error messages at the end of this posting. Is there a known work-around that fixes this "inside the class" problem for SGI compilers -- short of upgrading beyond v7.2.1?
2
4001
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 to declare an instance of the pointer. So, I tried using the keyword typename to declare the static member. I don't get any syntax errors but the pointer's name shows up in an "undefined symbol" error at compile time. Have a look at this...
2
280
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 possible types which the pointer is pointing at can be for example:
11
2812
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
1112
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 let's try. The class TSample has a pointer (my smart pointer implementation) to a IList (an indirect list based on the same smart pointers) to the type TFontCache.
5
2541
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 determining the class type. The code is below:
2
35631
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 implementation so the two can vary independently. Handle classes usually contain a pointer to the object implementation. The Handle object is used rather than the implemented object. This leaves the implemented object free to change without affecting...
4
1674
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 you include header code. One problem is that you have created one function. You want to place it in the main's source code. You may not want to move it back to the header code. The C++ Compiler will compile and link without any problems if...
7
3829
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 event); so I declared a function pointer like typedef void (CFObject::*CFEventHandler)(CFEvent *theEvent);
0
9541
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10482
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10251
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10225
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9072
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7564
isladogs
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2938
bsmnconsultancy
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.