template iterator: class used without template parameters problem

Hey all,

I've got a problem with a piece of code I'm writing, I'm trying to write a function for STL containers with shape pointers that would iterate the container given as an argument and execute a method.

code usage in main method:
showall(vs); // where vs is a vector
showall(ls); // where ls is a list

the utils.h file has :

using namespace std;

template <typename container>
class utils
{ public:
static void showall(const container &c);
};

and the .cc file

#include <vector>
#include <list>
#include "utils.h"
#include "shape.h"
using namespace std;

template <typename container>
void utils::showall(const container &c) {
for (typename c::iterator it = c.begin(); it != c.end(); ++it) {
it->show();
}
};

the error pointing to the "void utils::showall(const container &c) {" line is:
**template<class container> class utils' used without template parameters

and the errors at the "for (typename c::iterator it = c.begin(); it != c.end(); ++it) {" line is:
**`c' is not a class or namespace
**`iterator' does not name a type
**`it' undeclared (first use this function)
**expected `)' before ';' token
**expected `;' before ')' token

I'm still new to c++, so the answer is probable blatant, but I'm not familiar with the errors I have received from my code. I had (half-heartedly) included the type to the container to see if it would have made a difference, but I also thought that the containers in the STL override their ++ operators so it wouldn't have made a difference. Any pointers?

forgot to mention i'm using dev-c++ 4.9.9.2
[1895 byte] By [earthwyrm] at [2007-11-19 19:59:57]
# 1 Re: template iterator: class used without template parameters problem
try this:

for (container::const_iterator it = c.begin(); it != c.end(); ++it) {
it->show();
}
cilu at 2007-11-9 0:58:07 >
# 2 Re: template iterator: class used without template parameters problem
and this:

template <typename container>
void utils<container>::showall(const container &c) {
for (container::const_iterator it = c.begin(); it != c.end(); ++it) {
it->show();
}
};

If you need a non-const iterator, remove the const keyword form the argument list:

template <typename container>
void utils<container>::showall(container &c) {
for (container::iterator it = c.begin(); it != c.end(); ++it) {
it->show();
}

- petter
wildfrog at 2007-11-9 0:59:07 >
# 3 Re: template iterator: class used without template parameters problem
cool, got rid of a few errors (gonna have to look up const_iterator), but it didn't shake

on the showall line:
"void utils::showall(const container &c) {"
**`template<class container> class utils' used without template parameters
continued **In function 'void showall(const container&)':

on the for loop line:
"for (container::const_iterator it = c.begin(); it != c.end(); ++it) {"

**expected `;' before "it"
**'it' undeclared (first use this function)

any ideas?

(in reply to cilu, I'm tryinhg your code now dead frog)
earthwyrm at 2007-11-9 1:00:11 >
# 4 Re: template iterator: class used without template parameters problem
the utils.h file has :

using namespace std;

template <typename container>
class utils
{ public:
static void showall(const container &c);
};
From what you are writing, you probably do not want to have the util class templated, but just the function. I.e.
class utils
{ public:
template <typename container>
static void showall(const container &c);
};This declaration would than match your original definition (without the <container> after the class name.
treuss at 2007-11-9 1:01:14 >
# 5 Re: template iterator: class used without template parameters problem
sweet, one more piece gone. I'm still left with the errors:
** expected ';' before "it"
** 'it' undeclared (first use this function)

on the for loop line
" for (container::const_iterator it = c.begin(); it != c.end(); ++it) {"

am I missing an includes statement? i've included vector, list, my header file and shape which has the show() method, and I'm using the std namespace.

man I'm slow at replying, trying your last suggestion now
earthwyrm at 2007-11-9 1:02:13 >
# 6 Re: template iterator: class used without template parameters problem
sweet, one more piece gone. I'm still left with the errors:
** expected ';' before "it"
** 'it' undeclared (first use this function)

on the for loop line
" for (container::const_iterator it = c.begin(); it != c.end(); ++it) {"

am I missing an includes statement?No, it's just missing the typename declaration before container::const_iterator. As container is a template parameter, there is no way for the compiler to understand if container::const_iterator refers to a member of container or a type. Therefore the typename keyword.
treuss at 2007-11-9 1:03:17 >
# 7 Re: template iterator: class used without template parameters problem
hey treuss, dead frog,

I've tried the const_iterator with the template declaration within the class:

#ifndef UTILS_H
#define UTILS_H
#include <vector>
using namespace std;
#include "shape.h"

class utils {
public:
template <typename container>
static void showall(const container &c);
}
;
#endif

#include <vector>
#include <list>
#include "utils.h"
#include "shape.h"
using namespace std;

template <typename container>
void utils<container>::showall(const container &c) {
for (container::const_iterator it = c.begin(); it != c.end(); ++it) {
it->show();
}
};

and now I get brand new errors (a bit of a relief actually).

the line: "void utils<container>::showall(const container &c) {"
creates the following errors:
**expected init-declarator before '<' token
**expected `;' before '<' token

however I can't see the formatting error
earthwyrm at 2007-11-9 1:04:19 >
# 8 Re: template iterator: class used without template parameters problem
Hey all,

I've got a problem with a piece of code I'm writing, I'm trying to write a function for STL containers with shape pointers that would iterate the container given as an argument and execute a method. What's wrong with just using std::for_each()?

Regards,

Paul McKenzie
Paul McKenzie at 2007-11-9 1:05:11 >
# 9 Re: template iterator: class used without template parameters problem
lol, all these replies and my hands can't keep up (not complaining, it's jus funny), I've kept the template outside the class declaration in the header file and everything works perfectly. Thank you all so much for your help, I'm gonna look through the thread at all the errors and solutions to see if I can start making sense of these error messages.

N.B. and Paul, I just quickly looked up the for_each chapter in my book, I'm gonna go ahead and give that a try now ... thanks.

kind regards
earthwyrm
earthwyrm at 2007-11-9 1:06:22 >
# 10 Re: template iterator: class used without template parameters problem
I'll give you an example here:

#include <vector>
#include <list>
#include <iostream>
#include <algorithm>

using namespace std;

class shape
{
public:
virtual void show() const { cout << "base shape" << "\n"; }
virtual ~shape() {}
};

class shape2 : public shape
{
public:
void show() const { cout << "shape2" << "\n"; }
};

using namespace std;

void ShowData(const shape* theShape)
{
theShape->show();
}

int main()
{
std::vector<shape*> AllTheShapes;
shape s1;
shape2 s2;
AllTheShapes.push_back( &s1 );
AllTheShapes.push_back( &s2 );
for_each( AllTheShapes.begin(), AllTheShapes.end(), ShowData);

cout << "\n" ;
std::list<shape*> AllTheShapesL;
AllTheShapesL.push_back( &s1 );
AllTheShapesL.push_back( &s2 );
for_each( AllTheShapesL.begin(), AllTheShapesL.end(), ShowData);
}

Output:

base shape
shape2

base shape
shape2

Regards,

Paul McKenzie
Paul McKenzie at 2007-11-9 1:07:18 >
# 11 Re: template iterator: class used without template parameters problem
hey treuss, dead frog,

I've tried the const_iterator with the template declaration within the class:

#ifndef UTILS_H
#define UTILS_H
#include <vector>
using namespace std;
#include "shape.h"

class utils {
public:
template <typename container>
static void showall(const container &c);
}
;
#endif

#include <vector>
#include <list>
#include "utils.h"
#include "shape.h"
using namespace std;

template <typename container>
void utils<container>::showall(const container &c) {
for (container::const_iterator it = c.begin(); it != c.end(); ++it) {
it->show();
}
};

and now I get brand new errors (a bit of a relief actually).Yepp, now your class is not template'ed any more, so utils<container> does not make sense. And you are still missing the typename (which you had in your original post...) before container::const_iterator. So with corrections: // utils.h
#ifndef UTILS_H
#define UTILS_H

class utils {
public:
template <typename container>
static void showall(const container &c);
};
#endif
// utils.cc
#include "utils.h"

template <typename container>
void utils::showall(const container &c) {
for (typename container::const_iterator it = c.begin(); it != c.end(); ++it) {
it->show();
}
};

BTW, referrint to your earlier post:
am I missing an includes statement?You have far too many include statements ;). Your template does not have to know about vector or list, neither does it have to know about iterators, const_iterators or the show method.
The compiler will only, when you instantiate the template (i.e. when you are calling utils::showall) evaluate that the parameter you are passing (vector/list) will have a subtype const_iterator, that the iterator can be dereferenced, and that the objects received by dereferencing the iterator have a method named show.
treuss at 2007-11-9 1:08:24 >