Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
defining a iterator has input to function in c++ [solved]
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
DaggyStyle
Watchman
Watchman


Joined: 22 Mar 2006
Posts: 5909

PostPosted: Fri Jul 11, 2008 11:29 am    Post subject: defining a iterator has input to function in c++ [solved] Reply with quote

I have this code:
Code:

private:
   vector<T> mVector;
.
.
public:
   void handle(var& position);
.
.
implemetation:
template<class T>
void handle(var& position) {
   .
   .
   mVector.erase(position);
   .
   .
}

with what should I replace var& so position will be an iterator type for generic vector?

thanks
_________________
Only two things are infinite, the universe and human stupidity and I'm not sure about the former - Albert Einstein


Last edited by DaggyStyle on Sat Jul 12, 2008 9:18 am; edited 1 time in total
Back to top
View user's profile Send private message
m.wales
n00b
n00b


Joined: 06 Sep 2006
Posts: 18

PostPosted: Fri Jul 11, 2008 12:30 pm    Post subject: Reply with quote

vector<T>::iterator I believe.

Try using http://www.cplusplus.com/reference/stl/vector/
_________________
- Mark W -
Back to top
View user's profile Send private message
DaggyStyle
Watchman
Watchman


Joined: 22 Mar 2006
Posts: 5909

PostPosted: Fri Jul 11, 2008 1:49 pm    Post subject: Reply with quote

m.wales wrote:
vector<T>::iterator I believe.

Try using http://www.cplusplus.com/reference/stl/vector/


tried that, compiler spits this error:
Code:
class std::vector<T, std::allocator<T> >::iterator is not a type

_________________
Only two things are infinite, the universe and human stupidity and I'm not sure about the former - Albert Einstein
Back to top
View user's profile Send private message
DaggyStyle
Watchman
Watchman


Joined: 22 Mar 2006
Posts: 5909

PostPosted: Fri Jul 11, 2008 1:56 pm    Post subject: Reply with quote

ok, got somthing, iterator is a reference to memory, so I've defined this:
Code:
void handle(T& position);


and
Code:
void handle(T& position) {
   .
   .
   mVector.erase(position);
   .
   .
}


the compiler oks it, question is, will it work logically?
_________________
Only two things are infinite, the universe and human stupidity and I'm not sure about the former - Albert Einstein
Back to top
View user's profile Send private message
DaggyStyle
Watchman
Watchman


Joined: 22 Mar 2006
Posts: 5909

PostPosted: Fri Jul 11, 2008 3:17 pm    Post subject: Reply with quote

I'm trying the class but I get loads of undefined reference errors...
_________________
Only two things are infinite, the universe and human stupidity and I'm not sure about the former - Albert Einstein
Back to top
View user's profile Send private message
dmitchell
Veteran
Veteran


Joined: 17 May 2003
Posts: 1159
Location: Austin, Texas

PostPosted: Sat Jul 12, 2008 1:03 am    Post subject: Reply with quote

Code:
typename std::vector<T>::iterator

You have to tell the compiler that std::vector<T>::iterator is a type. Also, the iterator should be passed by value, not by reference.
Back to top
View user's profile Send private message
dmitchell
Veteran
Veteran


Joined: 17 May 2003
Posts: 1159
Location: Austin, Texas

PostPosted: Sat Jul 12, 2008 1:18 am    Post subject: Reply with quote

DaggyStyle wrote:
I'm trying the class but I get loads of undefined reference errors...

I am guessing you have put the implementation in a .cpp file. That's wrong. With templates everything has to go into the header. The compiler needs the full definition to generate classes/functions from the template.
Back to top
View user's profile Send private message
DaggyStyle
Watchman
Watchman


Joined: 22 Mar 2006
Posts: 5909

PostPosted: Sat Jul 12, 2008 5:08 am    Post subject: Reply with quote

dmitchell wrote:
DaggyStyle wrote:
I'm trying the class but I get loads of undefined reference errors...

I am guessing you have put the implementation in a .cpp file. That's wrong. With templates everything has to go into the header. The compiler needs the full definition to generate classes/functions from the template.


you mean all inline?
_________________
Only two things are infinite, the universe and human stupidity and I'm not sure about the former - Albert Einstein
Back to top
View user's profile Send private message
dmitchell
Veteran
Veteran


Joined: 17 May 2003
Posts: 1159
Location: Austin, Texas

PostPosted: Sat Jul 12, 2008 5:29 am    Post subject: Reply with quote

DaggyStyle wrote:
you mean all inline?

Not necessarily.

Code:
// foo.h

template<class T>
struct foo {
        void f() { } // inline

        void g(); // not inline
};

template<class T>
void foo<T>::g()
{
}
Back to top
View user's profile Send private message
DaggyStyle
Watchman
Watchman


Joined: 22 Mar 2006
Posts: 5909

PostPosted: Sat Jul 12, 2008 6:47 am    Post subject: Reply with quote

so just move the entire content of the cpp file to the header?
_________________
Only two things are infinite, the universe and human stupidity and I'm not sure about the former - Albert Einstein
Back to top
View user's profile Send private message
dmitchell
Veteran
Veteran


Joined: 17 May 2003
Posts: 1159
Location: Austin, Texas

PostPosted: Sat Jul 12, 2008 6:51 am    Post subject: Reply with quote

Correct.
Back to top
View user's profile Send private message
DaggyStyle
Watchman
Watchman


Joined: 22 Mar 2006
Posts: 5909

PostPosted: Sat Jul 12, 2008 7:18 am    Post subject: Reply with quote

why there is a struct before the foo?
_________________
Only two things are infinite, the universe and human stupidity and I'm not sure about the former - Albert Einstein
Back to top
View user's profile Send private message
dmitchell
Veteran
Veteran


Joined: 17 May 2003
Posts: 1159
Location: Austin, Texas

PostPosted: Sat Jul 12, 2008 7:35 am    Post subject: Reply with quote

DaggyStyle wrote:
why there is a struct before the foo?

You may use either keyword (struct or class) to define a class. The only difference is that members of a struct are by default public, while members of a class are by default private. So

Code:
struct foo {


is identical to

Code:
class foo {
public:


while

Code:
class foo {


is identical to

Code:
struct foo {
private:


You should as soon as possible obtain a copy of Accelerated C++ by Koenig and Moo. It is indispensable.
Back to top
View user's profile Send private message
DaggyStyle
Watchman
Watchman


Joined: 22 Mar 2006
Posts: 5909

PostPosted: Sat Jul 12, 2008 7:55 am    Post subject: Reply with quote

unfortunately, I've got no money currently.. I'll add it to my list along with TAOCP...

I've moved the entire cpp file into the header but now I get previously declared error.

I dont follow this:
Code:
typename std::vector<T>::iterator

where should I declare this?
_________________
Only two things are infinite, the universe and human stupidity and I'm not sure about the former - Albert Einstein
Back to top
View user's profile Send private message
dmitchell
Veteran
Veteran


Joined: 17 May 2003
Posts: 1159
Location: Austin, Texas

PostPosted: Sat Jul 12, 2008 8:24 am    Post subject: Reply with quote

That is the type of argument for handle().

Code:
template<class T>
class MyClass {

private:
    vector<T> mVector;

public:
    void handle(typename vector<T>::iterator position);
};

template<class T>
void MyClass<T>::handle(typename vector<T>::iterator position)
{
    mVector.erase(position);
}
Back to top
View user's profile Send private message
DaggyStyle
Watchman
Watchman


Joined: 22 Mar 2006
Posts: 5909

PostPosted: Sat Jul 12, 2008 8:28 am    Post subject: Reply with quote

dmitchell wrote:
That is the type of argument for handle().

Code:
template<class T>
class MyClass {

private:
    vector<T> mVector;

public:
    void handle(typename vector<T>::iterator position);
};

template<class T>
void MyClass<T>::handle(typename vector<T>::iterator position)
{
    mVector.erase(position);
}


ok, I think I've got it, thanks
_________________
Only two things are infinite, the universe and human stupidity and I'm not sure about the former - Albert Einstein
Back to top
View user's profile Send private message
dmitchell
Veteran
Veteran


Joined: 17 May 2003
Posts: 1159
Location: Austin, Texas

PostPosted: Sat Jul 12, 2008 8:36 am    Post subject: Reply with quote

You are welcome. Please remember Accelerated C++. It really is essential.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming 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