View previous topic :: View next topic |
Author |
Message |
DaggyStyle Watchman
Joined: 22 Mar 2006 Posts: 5909
|
Posted: Fri Jul 11, 2008 11:29 am Post subject: defining a iterator has input to function in c++ [solved] |
|
|
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 |
|
|
m.wales n00b
Joined: 06 Sep 2006 Posts: 18
|
|
Back to top |
|
|
DaggyStyle Watchman
Joined: 22 Mar 2006 Posts: 5909
|
Posted: Fri Jul 11, 2008 1:49 pm Post subject: |
|
|
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 |
|
|
DaggyStyle Watchman
Joined: 22 Mar 2006 Posts: 5909
|
Posted: Fri Jul 11, 2008 1:56 pm Post subject: |
|
|
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 |
|
|
DaggyStyle Watchman
Joined: 22 Mar 2006 Posts: 5909
|
Posted: Fri Jul 11, 2008 3:17 pm Post subject: |
|
|
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 |
|
|
dmitchell Veteran
Joined: 17 May 2003 Posts: 1159 Location: Austin, Texas
|
Posted: Sat Jul 12, 2008 1:03 am Post subject: |
|
|
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 |
|
|
dmitchell Veteran
Joined: 17 May 2003 Posts: 1159 Location: Austin, Texas
|
Posted: Sat Jul 12, 2008 1:18 am Post subject: |
|
|
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 |
|
|
DaggyStyle Watchman
Joined: 22 Mar 2006 Posts: 5909
|
Posted: Sat Jul 12, 2008 5:08 am Post subject: |
|
|
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 |
|
|
dmitchell Veteran
Joined: 17 May 2003 Posts: 1159 Location: Austin, Texas
|
Posted: Sat Jul 12, 2008 5:29 am Post subject: |
|
|
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 |
|
|
DaggyStyle Watchman
Joined: 22 Mar 2006 Posts: 5909
|
Posted: Sat Jul 12, 2008 6:47 am Post subject: |
|
|
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 |
|
|
dmitchell Veteran
Joined: 17 May 2003 Posts: 1159 Location: Austin, Texas
|
Posted: Sat Jul 12, 2008 6:51 am Post subject: |
|
|
Correct. |
|
Back to top |
|
|
DaggyStyle Watchman
Joined: 22 Mar 2006 Posts: 5909
|
Posted: Sat Jul 12, 2008 7:18 am Post subject: |
|
|
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 |
|
|
dmitchell Veteran
Joined: 17 May 2003 Posts: 1159 Location: Austin, Texas
|
Posted: Sat Jul 12, 2008 7:35 am Post subject: |
|
|
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
is identical to
Code: | class foo {
public: |
while
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 |
|
|
DaggyStyle Watchman
Joined: 22 Mar 2006 Posts: 5909
|
Posted: Sat Jul 12, 2008 7:55 am Post subject: |
|
|
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 |
|
|
dmitchell Veteran
Joined: 17 May 2003 Posts: 1159 Location: Austin, Texas
|
Posted: Sat Jul 12, 2008 8:24 am Post subject: |
|
|
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 |
|
|
DaggyStyle Watchman
Joined: 22 Mar 2006 Posts: 5909
|
Posted: Sat Jul 12, 2008 8:28 am Post subject: |
|
|
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 |
|
|
dmitchell Veteran
Joined: 17 May 2003 Posts: 1159 Location: Austin, Texas
|
Posted: Sat Jul 12, 2008 8:36 am Post subject: |
|
|
You are welcome. Please remember Accelerated C++. It really is essential. |
|
Back to top |
|
|
|