vectors and template types

A

aryan

I need to iterate thru a vector which holds an unknown type. I am
using templates and it looks like this after details are removed.

template <class T>
class set
{
vector<T> set_v;
public:
void insert(const T&);
};

template <class T>
void set<T>::insert(const T &t)
{
vector<T>::iterator itr; //This line does not
compile. error: expected `;' before "itr"
//......
//......
};

Pls let me know what i am doing wrong.
 
K

Kai-Uwe Bux

aryan said:
I need to iterate thru a vector which holds an unknown type. I am
using templates and it looks like this after details are removed.

template <class T>
class set
{
vector<T> set_v;
public:
void insert(const T&);
};

template <class T>
void set<T>::insert(const T &t)
{
vector<T>::iterator itr; //This line does not
compile. error: expected `;' before "itr"

You are running into dependent name lookup. Try:

typename vector said:
//......
//......
};

Pls let me know what i am doing wrong.

Best

Kai-Uwe Bux
 
J

Jim Langston

aryan said:
I need to iterate thru a vector which holds an unknown type. I am
using templates and it looks like this after details are removed.

template <class T>
class set

Bad naming choice. There is already a std::set template in the STL. Watch
out for name collision. using namespace std; and having anything #include
{
vector<T> set_v;
public:
void insert(const T&);
};

template <class T>
void set<T>::insert(const T &t)
{
vector<T>::iterator itr; //This line does not
compile. error: expected `;' before "itr"
//......
//......
};

This compiles for me:

#include <vector>

using std::vector;

template <class T>
class set
{
vector<T> set_v;
public:
void insert(const T&);
};

template <class T>
void set<T>::insert(const T &t)
{
vector<T>::iterator itr; //This line does not compile. error: expected
`;' before "itr"

//......
//......
};

int main()
{
set<int> Data;

Data.insert( 10 );
}

so it's difficult to say what your problem is. Does this code compile for
you?

Again, though, there is already a std::set which is most likely already
doing what you are attempting to do here.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,498
Latest member
log5Sshell/alfa5

Latest Threads

Top