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

懵懂的女人 提交于 2019-12-11 03:28:17

问题


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


回答1:


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.




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/4916506/expected-constructor-destructor-or-type-conversion-before-token

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!