本文介绍了在'<'令牌之前的预期构造函数,析构函数或类型转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C ++的新手,我似乎无法找出导致这些错误的原因。

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

以下是我的头文件:

#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

我的table.cpp如下:

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.

这是给我的大纲。我不允许修改table.hpp文件,除了私有字段。

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

任何帮助将不胜感激。

推荐答案

一个不是.cpp文件。将类模板的构造函数,方法等的定义直接放入类定义中,并删除.cpp文件。

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) {}

在尝试定义这个ctor之前,没有定义Table类模板,因此编译器对于应该在第一位的能力感到困惑。

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.

你可以解决你的braindead指令,防止正确地修复代码。首先,从不编译table.cpp,不让工具假设他们可以编译或处理它作为一个实现文件(许多正确的假设)。其次,在table.cpp的顶部包含你的头部(table.hpp?),因为你不太可能使用.cpp作为有效的实现文件捕获每一次出现的工具。

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.

这篇关于在'&lt;'令牌之前的预期构造函数,析构函数或类型转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 10:50