C++ Template class gives error

A

Ami

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
 
A

Andrey Tarasevich

Ami said:
...
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?
 
K

KK

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>
 
A

Ami

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

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
 
A

Ami

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.
 
A

Andrey Tarasevich

Ami said:
...
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;
 
A

Ami

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;

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.
 
E

Erik Wikström

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];
};
 

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
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top