开发者

Expected constructor, destructor, or type conversion before ‘<’ token

I'm new to C++, and I just can't seem to figure out what's causing these errors.

The following is my header file:

#ifndef TABLE
#define TABLE

#include <iostream>
#include <cstdlib>
#include <vector>

typedef struct {
    double successful , unsuccessful[2] ;
} Perform ;

using namespace std ;

template <class DATA>
class Table {

private :
    vector<DATA>* slot;
    vector<bool>* passBits;
    vector<bool>* full;
    int tableSize;

public :
    explicit Table ( unsigned size = 5 ) ;
    ~Table( ) ; //destructor
    void empty ( ) ;
    bool insert ( DATA & data ) ;
    bool insertD ( DATA & data ) ;
    bool fetch ( DATA & data ) const ;
    void print ( ostream & ) const ;
    Perform perform ( ) const ;
} ;

template <class DATA>
ostream & operator << ( ostream & out , const Table<DATA> &开发者_StackOverflow社区; table )
{
    table.print( out ) ;   return out ;
}

#include "table.cpp"

#endif

My table.cpp is as follows:

template <class DATA>
Table<DATA> :: Table ( unsigned size ) // Error
{

}

template <class DATA>
Table<DATA> :: ~Table( ) // Error
{

}

template <class DATA>
void Table<DATA> :: empty ( ) // Error
{

}

template <class DATA>
bool Table<DATA> :: insertD ( DATA & data ) // Error
{

}

#include "MyData.hpp"

The first two lines marked // Error have the error. The last two have an "expected initializer before ‘<’ token" error.

This is the outline that was given to me. I am not allowed to modify the table.hpp file except for the private fields.

Any help would be appreciated.


You're compiling a .cpp file that isn't. Put the definitions of your class template's constructors, methods, etc. directly into the class definition, and delete your .cpp file.

For example, compare with this code which is what the compiler sees and shows your first error:

template<class DATA>
Table<DATA>::Table(unsigned size) {}

Notice this code does not define the Table class template before trying to define this ctor, so the compiler is confused about what able is supposed to be in the first place.


You can work around your braindead instructions which prevent fixing the code correctly. First, never compile table.cpp and don't let tools assume they can compile or process it as an implementation file (which many rightly assume). Secondly, include your header (table.hpp?) at the top of table.cpp, since it is unlikely you will catch every occurrence of tools using .cpp as a valid implementation file.


This problem can be solved by putting the definition of the template into the head file. C++ does not support seperating the declaration and definition of a template in different files. See this Storing C++ template function definitions in a .CPP file

However some compilers such as IBM xlc and HP acc can do this.


I can't see anything wrong with the code you supplied (except for the unsual include of a .cpp file but that's legal).

Therefor I think you're trying to compile table.cpp. If so ,then that is an mistake on your part since it is compile through the include statement in your header-file.

You only need to compile the file in which you use the template.

EDIT: when used right with #include "table.h" in a test.cpp it compiled fine. When I tried to compile table.cpp (on MSVS 2010) In got 'missing ; before < '

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜