[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

C++ Template class gives error

Ami

11/25/2008 7:38:00 PM

Hi All,
I have declared and defined a template based class in header
file but when I try to compile it, it throws follwoign errors:

error C2143: syntax error : missing ';' before '<'
error C2501: 'CSVReader<T>::vector' : missing storage-class or type
specifiers
error C2238: unexpected token(s) preceding ';'
error C2146: syntax error : missing ';' before identifier 'ifs'
error C2501: 'CSVReader<T>::ifstream' : missing storage-class or type
specifiers
error C2501: 'CSVReader<T>::ifs' : missing storage-class or type
specifiers
error C2065: 'T' : undeclared identifier
........
........

The class declaration/definition is as follows:

//templateclass.h
typedef struct _mystruct
{
char myparam[256];
}mystruct;


template <class T>
class TempClass{
private:
vector<T> *mystruct;
long MAX_COL;
ifstream ifs;
public:
TempClass(char* inchar);
~TempClass();
};

CSVReader<T>::CSVReader(char *csv)
{
//
}

template <class T> CSVReader<T>::~CSVReader()
{
//
}


Any help is highly appreciated.
Regards
7 Answers

Andrey Tarasevich

11/25/2008 7:42:00 PM

0

Ami wrote:
> ...
> CSVReader<T>::CSVReader(char *csv)
> {
> //
> }
>
> template <class T> CSVReader<T>::~CSVReader()
> {
> //
> }
> ...

The code makes no sense. What's 'CSVReader' and what on Earth is it
doing in this code?

--
Best regards,
Andrey Tarasevich

KK

11/25/2008 7:43:00 PM

0

On Nov 25, 11:38 am, Ami <ver_amit...@yahoo.com> wrote:
> Hi All,
>        I have declared and defined a template based class in header
> file but when I try to compile it, it throws follwoign errors:
>
> error C2143: syntax error : missing ';' before '<'
>  error C2501: 'CSVReader<T>::vector' : missing storage-class or type
> specifiers
>  error C2238: unexpected token(s) preceding ';'
> error C2146: syntax error : missing ';' before identifier 'ifs'
> error C2501: 'CSVReader<T>::ifstream' : missing storage-class or type
> specifiers
> error C2501: 'CSVReader<T>::ifs' : missing storage-class or type
> specifiers
> error C2065: 'T' : undeclared identifier
> .......
> .......
>
> The class declaration/definition is as follows:
>
> //templateclass.h
> typedef struct _mystruct
> {
>         char myparam[256];
>
> }mystruct;
>
> template <class T>
> class TempClass{
> private:
>         vector<T> *mystruct;
>         long MAX_COL;
>         ifstream ifs;
> public:
>         TempClass(char* inchar);
>         ~TempClass();
>
> };
>
> CSVReader<T>::CSVReader(char *csv)
> {
>         //
>
> }
>
> template <class T> CSVReader<T>::~CSVReader()
> {
> //
>
> }
>
> Any help is highly appreciated.
> Regards

Its not really the template problem, you are missing the headers
#include <vector>
#include <fstream>

Ami

11/25/2008 7:45:00 PM

0

On Nov 26, 12:42 am, Andrey Tarasevich <andreytarasev...@hotmail.com>
wrote:
> Ami wrote:
> > ...
> > CSVReader<T>::CSVReader(char *csv)
> > {
> >    //
> > }
>
> > template <class T> CSVReader<T>::~CSVReader()
> > {
> > //
> > }
> > ...
>
> The code makes no sense. What's 'CSVReader' and what on Earth is it
> doing in this code?
>
> --
> Best regards,
> Andrey Tarasevich

Ahh I am sorry my bad. Please read the class defintion as follows:
//templateclass.h
typedef struct _mystruct
{
char myparam[256];
}mystruct;


template <class T>
class TempClass{
private:
vector<T> *mystruct; //The error
long MAX_COL;
ifstream ifs;
public:
TempClass(char* inchar);
~TempClass();
};


TempClass<T>::TempClass(char *inchar)
{
//


}


TempClass<class T> TempClass<T>::~TempClass()
{
//


}
Thanks

Ami

11/25/2008 7:49:00 PM

0

On Nov 26, 12:43 am, KK <pedag...@gmail.com> wrote:
> On Nov 25, 11:38 am, Ami <ver_amit...@yahoo.com> wrote:
>
>
>
>
>
> > Hi All,
> >        I have declared and defined a template based class in header
> > file but when I try to compile it, it throws follwoign errors:
>
> > error C2143: syntax error : missing ';' before '<'
> >  error C2501: 'CSVReader<T>::vector' : missing storage-class or type
> > specifiers
> >  error C2238: unexpected token(s) preceding ';'
> > error C2146: syntax error : missing ';' before identifier 'ifs'
> > error C2501: 'CSVReader<T>::ifstream' : missing storage-class or type
> > specifiers
> > error C2501: 'CSVReader<T>::ifs' : missing storage-class or type
> > specifiers
> > error C2065: 'T' : undeclared identifier
> > .......
> > .......
>
> > The class declaration/definition is as follows:
>
> > //templateclass.h
> > typedef struct _mystruct
> > {
> >         char myparam[256];
>
> > }mystruct;
>
> > template <class T>
> > class TempClass{
> > private:
> >         vector<T> *mystruct;
> >         long MAX_COL;
> >         ifstream ifs;
> > public:
> >         TempClass(char* inchar);
> >         ~TempClass();
>
> > };
>
> > CSVReader<T>::CSVReader(char *csv)
> > {
> >         //
>
> > }
>
> > template <class T> CSVReader<T>::~CSVReader()
> > {
> > //
>
> > }
>
> > Any help is highly appreciated.
> > Regards
>
> Its not really the template problem, you are missing the headers
> #include <vector>
> #include <fstream>- Hide quoted text -
>
> - Show quoted text -

Thanks for reply KK. I have included both the files already. Actually
this header file is little long and contains some more declarations so
to remove the unnecessary part, i removed the #include<*h> part too.
Any other guess why I am getting this error with header files too.
Thanks.

Andrey Tarasevich

11/25/2008 7:56:00 PM

0

Ami wrote:
> ...
> TempClass<T>::TempClass(char *inchar)
> {
>
> }
>
>
> TempClass<class T> TempClass<T>::~TempClass()
> {
> //
>
>
> }
> ...

Now it's even worse, in a sense.

Your destructor definition was originally OK, except for the wrong class
name

template <class T> CSVReader<T>::~CSVReader()
{
//
}

Now you corrected the class name, but for some reason replaced the
'template' keyword. Why did you replace the 'template' keyword? Change
it back, and your destructor definition will be fine.

Now, the constructor. Can you just take a close look at your constructor
definition and see how it is quite different from your destructor
definition? (No, not just the '~' in the name). Once you notice that
major difference, I'm sure you'll figure out how to fix it.

P.S. I assume you included all the necessary headers, but unless you
used 'using', it should be 'std::vector' and 'std::ifstream;

--
Best regards,
Andrey Tarasevich

Ami

11/25/2008 8:03:00 PM

0

On Nov 26, 12:55 am, Andrey Tarasevich <andreytarasev...@hotmail.com>
wrote:
> Ami wrote:
> > ...
> > TempClass<T>::TempClass(char *inchar)
> > {
>
> > }
>
> > TempClass<class T> TempClass<T>::~TempClass()
> > {
> > //
>
> > }
> > ...
>
> Now it's even worse, in a sense.
>
> Your destructor definition was originally OK, except for the wrong class
> name
>
>    template <class T> CSVReader<T>::~CSVReader()
>    {
>      //
>    }
>
> Now you corrected the class name, but for some reason replaced the
> 'template' keyword. Why did you replace the 'template' keyword? Change
> it back, and your destructor definition will be fine.
>
> Now, the constructor. Can you just take a close look at your constructor
> definition and see how it is quite different from your destructor
> definition? (No, not just the '~' in the name). Once you notice that
> major difference, I'm sure you'll figure out how to fix it.
>
> P.S. I assume you included all the necessary headers, but unless you
> used 'using', it should be 'std::vector' and 'std::ifstream;
>
> --
> Best regards,
> Andrey Tarasevich

Many thanks Andrey. Yes, it was my mistake that i just inlcuded the
proper header files but didnt used the declaration with std:: .
Sorry for being careless. once again thanks for help.

Erik Wikström

11/25/2008 8:38:00 PM

0

On 2008-11-25 20:38, Ami wrote:
> Hi All,
> I have declared and defined a template based class in header
> file but when I try to compile it, it throws follwoign errors:
>
> error C2143: syntax error : missing ';' before '<'
> error C2501: 'CSVReader<T>::vector' : missing storage-class or type
> specifiers
> error C2238: unexpected token(s) preceding ';'
> error C2146: syntax error : missing ';' before identifier 'ifs'
> error C2501: 'CSVReader<T>::ifstream' : missing storage-class or type
> specifiers
> error C2501: 'CSVReader<T>::ifs' : missing storage-class or type
> specifiers
> error C2065: 'T' : undeclared identifier
> .......
> .......
>
> The class declaration/definition is as follows:
>
> //templateclass.h
> typedef struct _mystruct
> {
> char myparam[256];
> }mystruct;

I'll just point out that in C++ you do not need the typedef, instead you
can just write

struct mystruct
{
char myparam[256];
};

--
Erik Wikström