C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Template compilation question

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Ash
Guest





PostPosted: Tue Aug 17, 2004 10:04 am    Post subject: Template compilation question Reply with quote



Hello all,
I am trying to write a templated class that stores pointers to
auto_ptr in its data structure and another method invokes certain
methods on the pointer. But I am running into some compilation errors
and I am wondering if what I am doing is legal or not and how should I
go about doing this. Here is the simplest code that does reproduce the
problem.

<CODE>
#include <iostream>
#include <memory>
#include <vector>
using namespace std;

class Item
{
public:
void Bar () { cout << "Item::Bar" << endl; };
void Foo () { cout << "Item::Foo" << endl; };
};

template {
public:
Reply () {};
~Reply () {};

// insert an element into the list
void Foo ()
{
auto_ptr <T> *pD = new auto_ptr <T> (new T);
m_Items.push_back (pD);
};

// get the first element and call a method on it
void Bar ()
{
auto_ptr <T> *pT = m_Items [0];
pT->Bar ();
}

private:
vector < auto_ptr m_Items;
};


int main ()
{
Reply <Item> Obj;

Obj.Foo ();
Obj.Bar ();
}
</CODE>

I get the following error from Visual studio:

<COMPILATION>
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.00.9466 for
80x86
Copyright (C) Microsoft Corporation 1984-2001. All rights reserved.

tst.cpp
tst.cpp(30) : error C2039: 'Bar' : is not a member of
'std::auto_ptr<_Ty>'
with
[
_Ty=Item
]
tst.cpp(2Cool : while compiling class-template member function
'void Reply<T>::Bar(void)'
with
[
T=Item
]
tst.cpp(40) : see reference to class template instantiation
'Reply<T>' being compiled
with
[
T=Item
]
</COMPILATION>

I cannot seem to understand why this is not allowed by the standard.
Any comments/suggestions would be appreciated.

Thanks,
/ash

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Ulrich Eckhardt
Guest





PostPosted: Tue Aug 17, 2004 7:20 pm    Post subject: Re: Template compilation question Reply with quote



Ash wrote:
Quote:
I am trying to write a templated class that stores pointers to
auto_ptr in its data structure and another method invokes certain
methods on the pointer. But I am running into some compilation errors
and I am wondering if what I am doing is legal or not and how should I
go about doing this. Here is the simplest code that does reproduce the
problem.

auto_ptr <T> *pT = m_Items [0];
pT->Bar ();
[...]
tst.cpp(30) : error C2039: 'Bar' : is not a member of
'std::auto_ptr<_Ty>'

pT is a pointer to an auto_ptr<T>. In order to invoke a memberfunction of
class T, you need to get at the auto_ptr first and then you can get at the
object: (*pT)->Bar();

About the idea of storing pointers to smart pointers: Why? Why? Why?
The point is, why do people use smart pointers in the first place? And why
don't you use a smart pointer to the auto_ptr<>s?
If you want to use auto_ptr at the surface of your container, in order to
make clear who owns the objects, using hand-made handling of pointers to
autopointers(think of allocation overhead!) is neither secure nor easy. In
that case, you can as well handle raw pointers, converting to auto_ptr when
passing out and release()ing when passing in. Getting this right in the
presence of exceptions is an art though.

My advice: take a look at boost::shared_ptr<> instead. Several things are so
much easier that way.

Uli

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

/* bittersweet C++ */
default: break;

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Motti Lanzkron
Guest





PostPosted: Tue Aug 17, 2004 7:34 pm    Post subject: Re: Template compilation question Reply with quote



Ash wrote:
Quote:
Hello all,
I am trying to write a templated class that stores pointers to
auto_ptr in its data structure and another method invokes certain
methods on the pointer.

Why would you want to dynamically allocate an auto_ptr?
The whole point of auto_ptrs is that you don't have to manage the
memory explicitly and now you have to delete the auto_ptr. Nothing is
solved, instead of dealing with raw pointers to Item you're dealing
with raw pointers to auto_ptr<Item>.

Quote:
But I am running into some compilation errors
and I am wondering if what I am doing is legal or not and how should I
go about doing this. Here is the simplest code that does reproduce the
problem.

....

Quote:
// get the first element and call a method on it
void Bar ()
{
auto_ptr <T> *pT = m_Items [0];
pT->Bar (); // line 30
}
tst.cpp(30) : error C2039: 'Bar' : is not a member of
'std::auto_ptr<_Ty>'
with
[
_Ty=Item
]

pT->Bar() (when dealing with raw pointers) is equivalent to
(*pT).Bar(), the type of *pT is a reference to a auto_ptr<Item>& and
auto_ptr doesn't have a method Bar.

What's probably confusing you is that auto_ptr defines operator->
which returns a T* and language automatically "adds" a -> to the
pointer and you get the same behaviour as if you called
(&*ptr)->Bar(); This chain of events (I'm guessing wildly here) stops
when the return type of operator-> is a raw pointer and not a class.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Victor Bazarov
Guest





PostPosted: Tue Aug 17, 2004 7:47 pm    Post subject: Re: Template compilation question Reply with quote

Ash wrote:
Quote:
Hello all,
I am trying to write a templated class that stores pointers to
auto_ptr in its data structure and another method invokes certain
methods on the pointer. But I am running into some compilation errors
and I am wondering if what I am doing is legal or not and how should I
go about doing this. Here is the simplest code that does reproduce the
problem.

CODE
#include <iostream
#include #include using namespace std;

class Item
{
public:
void Bar () { cout << "Item::Bar" << endl; };
void Foo () { cout << "Item::Foo" << endl; };
};

template {
public:
Reply () {};
~Reply () {};

Semicolons are superfluous. BTW, what do you do to clean up the mess
that is your vector of pointers to auto_ptr?

Quote:

// insert an element into the list
void Foo ()
{
auto_ptr <T> *pD = new auto_ptr <T> (new T);
m_Items.push_back (pD);
};

// get the first element and call a method on it
void Bar ()
{
auto_ptr <T> *pT = m_Items [0];
pT->Bar ();

'pT' is a _pointer_ to auto_ptr. In order to call auto_ptr::operator->
(to get to 'Bar' member of 'T') you need to dereference it first:

(*pT)->Bar();

Now, (*pT) is an auto_ptr<T>, and you can access members of T by using
the operator-> of the auto_ptr<T> class.

Quote:
}

private:
vector < auto_ptr m_Items;
};


int main ()
{
Reply <Item> Obj;

Obj.Foo ();
Obj.Bar ();
}
/CODE

I get the following error from Visual studio:

COMPILATION
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.00.9466 for
80x86
Copyright (C) Microsoft Corporation 1984-2001. All rights reserved.

tst.cpp
tst.cpp(30) : error C2039: 'Bar' : is not a member of
'std::auto_ptr<_Ty>'
with
[
_Ty=Item
]
tst.cpp(2Cool : while compiling class-template member function
'void Reply<T>::Bar(void)'
with
[
T=Item
]
tst.cpp(40) : see reference to class template instantiation
'Reply<T>' being compiled
with
[
T=Item
]
/COMPILATION

I cannot seem to understand why this is not allowed by the standard.

'auto_ptr' itself doesn't have a member 'Bar'. That's why it's not
allowed.

Quote:
Any comments/suggestions would be appreciated.

Two comments: since you're managing dynamic memory, your implementation
lacks proper destructor, copy constructor, and the copy assignment op.
Read bout "the Rule of Three". And having _pointers_ to 'auto_ptr' just
defeats the purpose of using it, really. What you want is to use the
'shared_ptr' from Boost and store it directly instead of storing the
pointers to auto_ptr.

Victor

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Florian Preknya
Guest





PostPosted: Tue Aug 17, 2004 7:50 pm    Post subject: Re: Template compilation question Reply with quote

The problem is you do not dereferate the auto_ptr pointer.
In the line
pT->Bar ();
where the pT is a pointer to an auto_ptr object, the -> operator is aplied
to dereferate the auto_ptr pointer.

So instead of...
void Bar ()
{
auto_ptr <T> *pT = m_Items [0];
pT->Bar ();
}

you have to change it this way
void Bar ()
{
auto_ptr <T> *pT = m_Items [0];
(*pT)->Bar ();
}


--
Good luck,
Florian.


"Ash" <ash4code (AT) yahoo (DOT) com> wrote

Quote:
Hello all,
I am trying to write a templated class that stores pointers to
auto_ptr in its data structure and another method invokes certain
methods on the pointer. But I am running into some compilation errors
and I am wondering if what I am doing is legal or not and how should I
go about doing this. Here is the simplest code that does reproduce the
problem.

CODE
#include <iostream
#include #include using namespace std;

class Item
{
public:
void Bar () { cout << "Item::Bar" << endl; };
void Foo () { cout << "Item::Foo" << endl; };
};

template {
public:
Reply () {};
~Reply () {};

// insert an element into the list
void Foo ()
{
auto_ptr <T> *pD = new auto_ptr <T> (new T);
m_Items.push_back (pD);
};

// get the first element and call a method on it
void Bar ()
{
auto_ptr <T> *pT = m_Items [0];
pT->Bar ();
}

private:
vector < auto_ptr m_Items;
};


int main ()
{
Reply <Item> Obj;

Obj.Foo ();
Obj.Bar ();
}
/CODE

I get the following error from Visual studio:

COMPILATION
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.00.9466 for
80x86
Copyright (C) Microsoft Corporation 1984-2001. All rights reserved.

tst.cpp
tst.cpp(30) : error C2039: 'Bar' : is not a member of
'std::auto_ptr<_Ty>'
with
[
_Ty=Item
]
tst.cpp(2Cool : while compiling class-template member function
'void Reply<T>::Bar(void)'
with
[
T=Item
]
tst.cpp(40) : see reference to class template instantiation
'Reply<T>' being compiled
with
[
T=Item
]
/COMPILATION

I cannot seem to understand why this is not allowed by the standard.
Any comments/suggestions would be appreciated.

Thanks,
/ash



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
John Pazarzis
Guest





PostPosted: Tue Aug 17, 2004 8:02 pm    Post subject: Re: Template compilation question Reply with quote

This will compile fine
#include <iostream>
#include <memory>
#include <vector>
using namespace std;

class Item
{
public:
void Bar () { cout << "Item::Bar" << endl; };
void Foo () { cout << "Item::Foo" << endl; };
};

template {
public:
Reply () {};
~Reply () {};

// insert an element into the list
void Foo ()
{
auto_ptr <T> *pD = new auto_ptr <T> (new T);
m_Items.push_back (pD);
};

// get the first element and call a method on it
void Bar ()
{
auto_ptr <T> *pT = m_Items [0];
(*pT)->Bar ();
}

private:
vector < auto_ptr m_Items;
};


int main ()
{
Reply <Item> Obj;

Obj.Foo ();
Obj.Bar ();
}

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
P G
Guest





PostPosted: Tue Aug 17, 2004 10:04 pm    Post subject: Re: Template compilation question Reply with quote

[email]ash4code (AT) yahoo (DOT) com[/email] (Ash) wrote in message news:<e2c8643e.0408161853.5150430 (AT) posting (DOT) google.com>...
Quote:
Hello all,
I am trying to write a templated class that stores pointers to
auto_ptr in its data structure and another method invokes certain
methods on the pointer. But I am running into some compilation errors
and I am wondering if what I am doing is legal or not and how should I
go about doing this. Here is the simplest code that does reproduce the
problem.

I don't know if it's illegal but I'd be interested to know why you'd
want to allocate an auto_ptr on the free store. It would seem that in
doing so one discards most of it's benefits.

Quote:
CODE
#include <iostream
#include #include using namespace std;

class Item
{
public:
void Bar () { cout << "Item::Bar" << endl; };
void Foo () { cout << "Item::Foo" << endl; };
};

template {
public:
Reply () {};
~Reply () {};

// insert an element into the list
void Foo ()
{
auto_ptr <T> *pD = new auto_ptr <T> (new T);
m_Items.push_back (pD);
};

// get the first element and call a method on it
void Bar ()
{
auto_ptr <T> *pT = m_Items [0];
pT->Bar ();
}

pT->Bar() is equivalent to (*pT).Bar. The compiler determined
that(*pT) is of type auto_ptr<T> and complained that auto_ptr<T>
doesn't have a member Bar().



Quote:
tst.cpp(30) : error C2039: 'Bar' : is not a member of
'std::auto_ptr<_Ty>'
with
[
_Ty=Item
]
tst.cpp(2Cool : while compiling class-template member function
'void Reply<T>::Bar(void)'
with
[
T=Item
]
tst.cpp(40) : see reference to class template instantiation
'Reply<T>' being compiled
with
[
T=Item
]
/COMPILATION

I cannot seem to understand why this is not allowed by the standard.
Any comments/suggestions would be appreciated.

Thanks,
/ash

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Arkadiy Vertleyb
Guest





PostPosted: Tue Aug 17, 2004 10:06 pm    Post subject: Re: Template compilation question Reply with quote

[email]ash4code (AT) yahoo (DOT) com[/email] (Ash) wrote

Quote:
auto_ptr <T> *pT = m_Items [0];
pT->Bar ();

tst.cpp(30) : error C2039: 'Bar' : is not a member of
'std::auto_ptr<_Ty>'

You call Bar() on the auto_ptr itself rather than on the object it
points to, and the compiler does tell you about this. Try something
like:

(*pT)->Bar ();

HTH,

Arkadiy

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.