c++ - 模板程序中的“模板前预期的主要表达式/';'”错误

标签 c++ templates

#include <iostream>
#include <string>
#include "HashTable.h"

using namespace std;

struct MyStruct {
string str;
int num;
bool operator ==( const MyStruct & r ) { return str == r.str; }
};

const int SIZE1 = 97;

int hash1( const MyStruct & obj );

int main( )
{
HashTable<MyStruct> ht1( hash1, SIZE1 );

MyStruct myobj;

myobj.str = "elephant";
myobj.num = 25;
ht1.insert( myobj );


MyStruct myobj2;

myobj2.str = "elephant";
ht1.retrieve( myobj2 );
cout << "retrieved from ht1: " << myobj2.num << " for num." << endl;

return 0;
}

int hash1( const MyStruct & obj )
{
int sum = 0;
for ( int i = 0; i < 3 && i < int( obj.str.length( ) ); i++ )
    sum += obj.str[ i ];
return sum % SIZE1;
}

这是我的主要功能的代码,我遇到了一大堆错误,但找不到任何错误。

H:\CSC 375\Homework5\LinkedList.h|200|warning: no newline at end of file|

H:\CSC 375\Homework5\Array.h||In constructor `Array<DataType>::Array(int)':|
H:\CSC 375\Homework5\Array.h|46|error: expected primary-expression before "template"|
H:\CSC 375\Homework5\Array.h|46|error: expected `;' before "template"|

每次使用模板时重复

H:\CSC 375\Homework5\HashTable.h|6|error: expected primary-expression before "template"|
H:\CSC 375\Homework5\HashTable.h|6|error: expected `;' before "template"| 

每次使用模板时重复

H:\CSC 375\Homework5\HashTable.h|82|warning: no newline at end of file|


H:\CSC 375\Homework5\main.cpp|17|error: expected primary-expression before "int"|
H:\CSC 375\Homework5\main.cpp|17|error: expected `;' before "int"|
H:\CSC 375\Homework5\main.cpp|38|error: a function-definition is not allowed here before '{' token|
H:\CSC 375\Homework5\main.cpp|38|error: expected `,' or `;' before '{' token|
H:\CSC 375\Homework5\main.cpp|44|warning: no newline at end of file|
H:\CSC 375\Homework5\main.cpp|43|error: expected `}' at end of input|
||=== Build finished: 35 errors, 3 warnings (0 minutes, 0 seconds) ===|

编辑:链表、数组和哈希表都可以在没有 main.cpp 的情况下正常编译和构建,因此它必须包含在该文件中。

双重编辑:文件代码(我在使用模板时将接口(interface)和实现放在同一个文件中,链接 .h 和 .cpp 的问题太多)

// HashTable.h

#include "LinkedList.h"
#include "Array.h"

template <class DataType>
class HashTable
{
public:
    HashTable( int (*hashf)(const DataType &), int s );

    bool Insert( const DataType & newObject );
    bool retrieve( DataType & retrieved );
    bool Remove( DataType & removed );
    bool update( DataType & updateObject );
    void makeEmpty( );

private:
    Array< LinkedList<DataType> > table;
    int (*hashfunction)(const DataType &);
};

// HashTable.cpp
template <class DataType>
HashTable<DataType>::HashTable( int (*hashf)(const DataType &), int s )
    : table( s )
{
    hashfunc = hashf;
}

template <class DataType>
bool HashTable<DataType>::Insert( const DataType & newObject )
{
    int location = hashfunc( newObject );
    if ( location < 0 || location >= table.length( ) )
        return false;
    table[ location ].Insert( newObject );
    return true;
}

template <class DataType>
bool HashTable<DataType>::retrieve( DataType & retrieved )
{
    int location = hashfunc( retrieved );
    if ( location < 0 || location >= table.length( ) )
        return false;
    if ( !table[ location ].retrieve( retrieved ) )
        return false;
    return true;
}

template <class DataType>
bool HashTable<DataType>::Remove( DataType & removed )
{
    int location = hashfunc( removed );
    if ( location < 0 || location >= table.length( ) )
        return false;
    if ( !table[ location ].Remove( removed ) )
        return false;
    return true;
}

template <class DataType>
bool HashTable<DataType>::update( DataType & updateObject )
{
    int location = hashfunc( updateObject );
    if ( location < 0 || location >= table.length( ) )
        return false;
    if ( !table[location].find( updateObject ) )
        return false;
    table[location].Replace( updateObject );
    return true;
}

template <class DataType>
void HashTable<DataType>::makeEmpty( )
{
    for ( int i = 0; i < table.length( ); i++ )
        table[ i ].makeEmpty( );
}

.... //数组.h

#include <string>

using namespace std;

template <class DataType>
class Array
{
public:
    Array( int Size );
    Array( const Array<DataType> & ap );
    ~Array( );
    Array<DataType> & operator =( const Array<DataType> & right );

    inline DataType & operator [ ]( int index );
    void changeSize( int newSize );
    inline int length( ) const;
    string err( ) const;

private:
    DataType *elements;
    int capacity;
    DataType dud;
    int errorCode;
    inline void deepCopy( const Array<DataType> & original );
};

// Array.cpp


template <class DataType>
Array<DataType>::Array( int Size )
{
    if ( Size < 1 ) {
        capacity = 1;
        errorCode = 1;
    }
    else {
        capacity = Size;
        errorCode = 0;

    elements = new DataType [capacity];
}

template <class DataType>
Array<DataType>::Array( const Array<DataType> & ap )
{
    deepCopy( ap );
}

template <class DataType>
Array<DataType>::~Array( )
{
    delete [ ] elements;
}

template <class DataType>
Array<DataType> & Array<DataType>::operator =( const Array<DataType> & right )
{
    if ( this == &right )
        return *this;
    delete [ ] elements;
    deepCopy( right );

    return *this;
}

template <class DataType>
inline DataType & Array<DataType>::operator [ ]( int index )
{
#ifdef DEBUG_ARRAY
    if ( index < 0 || index >= capacity ) {
        errorCode |= 2;
        return dud;
        }
#endif
    return elements[ index ];
}


template <class DataType>
void Array<DataType>::changeSize( int newSize )
{
    if ( newSize < 1 )
    {
        errorCode |= 4;
        return;
    }

    DataType *newArray = new DataType [newSize];

    int limit = (newSize > capacity)? capacity : newSize;

    for ( int i = 0; i < limit; i++ )
        newArray[ i ] = elements[ i ];

    delete [ ] elements;

    elements = newArray;

    capacity = newSize;
}

template <class DataType>
inline int Array<DataType>::length( ) const
{
    return capacity;
}

template <class DataType>
string Array<DataType>::err( ) const
{

    if ( errorCode == 0 )
        return "No error.\n";

    string errorMessage = "";
    if ( errorCode & 1 ) {
        errorMessage += "Nonpositive size passed into constructor, so\n";
        errorMessage += "the capacity was set to 1 by default.\n";
    }
    if ( errorCode & 2 )
        errorMessage += "Index out of range.\n";
    if ( errorCode & 4 ) {
        errorMessage += "Nonpositive size passed into changeSize, so\n";
        errorMessage += "the size of the array was not changed.\n";
    }

    return errorMessage;
}

template <class DataType>
inline void Array<DataType>::deepCopy( const Array<DataType> & original )
{
    capacity = original.capacity;
    errorCode = original.errorCode;
    elements = new DataType [capacity];
    for ( int i = 0; i < capacity; i++ )
        elements[ i ] = original.elements[ i ];
}

大部分代码都是我们课本上提供的,我只是需要它来运行,请帮助!

最佳答案

template <class DataType>
Array<DataType>::Array( int Size )
{
    if ( Size < 1 ) {
        capacity = 1;
        errorCode = 1;
    }
    else {
        capacity = Size;
        errorCode = 0;
    }//This is missing
    elements = new DataType [capacity];
}

在 header 中使用命名空间也是一个坏主意。

关于c++ - 模板程序中的“模板前预期的主要表达式/';'”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16093255/

相关文章:

c++ - 基于 Python 的游戏——它们是如何制作的?

c++ - 在 C++ : Undefined reference to 上编译时出错

c++ - 函数重载 C++ 指针

c++ - 具有成员函数指针的模板的默认值

c++ - 新构造的对象作为默认模板函数参数

c++ - 打印结构的结构数组会导致意外的执行错误?

c++ - 将字符串转换为 const char *

c++ - 非实例化模板成员的编译时错误而不是链接时错误

c++ - 检查参数包是否包含类型

用于插入模板映射的 C++ 函数