模板声明不能出现在块作用域内

时间:2020-02-27 15:20:11

标签: c++ c++11

所以这是给我上课的,老实说,我以前从未使用过模板化。这是我的简单的vector.h文件,但我得到的错误是模板无法出现在块范围内。我对此的理解表明我正在尝试在函数中对其进行定义。这是我的代码:

#ifndef SIMPLEVECTOR_H
#define SIMPLEVECTOR_H
#include <iostream>
#include <new> // Needed for bad-alloc exception
#include <cstdlib> // Needed for the exit function
using namespace std;

template <class T>
class SimpleVector {
        private:

        T *aptr; // To point to the allocated array
        int arraysize; // Number of elements in the array
        void memError(); // Handles memory allocation errors
        void subError(); // Handles subscripts out of range
public:

        SimpleVector()
                {
                aptr = 0; arraysize = 0;
                }

        SimpleVector(int s);

        SimpleVector(const SimpleVector &);

        ~SimpleVector();

        int size() const
                {
                return arraysize;
                }

        T getElementAt(int sub);

        T &operator[](const int &);


};

#endif //SIMPLEVECTOR_H

template <class T>
SimpleVector<T>::SimpleVector(int s)
{
        if(s<1)
                {
                arraysize=1;
                }

        else
                {
                arraysize=s;
                }

        try
                {
                aptr = new T [arraysize];
                }

        catch (bad_alloc)
                {
                memError();
                }

        for(int i=0;i<arraysize;i++)
                {
                aptr[i]=0;
                {
} 

template <class T>
void SimpleVector<T>::memError()
{

        cout<<"Error: cannot allocate memory."<<endl;
        exit(EXIT_FAILURE);

}

template <class T>
T SimpleVector<T>::getElementAt(int sub)
{
        return aptr[sub];
}

template <class T>
SimpleVector<T>::~SimpleVector()
{
        if(arraysize>0)
                {
                aptr.clear();
                aptr=aptr[0];
                }
}

template <class T>
void SimpleVector<T>::subError()
{

        cout<<"Subscripts out of range."<<endl;
        exit(EXIT_FAILURE);

}


然后是我遇到的错误。

In file included from main.cpp:4:0:
simplevector.h: In constructor ‘SimpleVector<T>::SimpleVector(int)’:
simplevector.h:87:1: error: a template declaration cannot appear at block scope
 template <class T>
 ^
simplevector.h:99:1: error: expected ‘;’ before ‘template’
 template <class T>
 ^
simplevector.h:109:1: error: a template declaration cannot appear at block scope
 template <class T>
 ^
simplevector.h:122:1: error: expected ‘;’ before ‘template’
 template <class T>
 ^
main.cpp:9:1: error: a function-definition is not allowed here before ‘{’ token
 {
 ^
main.cpp:47:1: error: expected ‘}’ at end of input
 }
 ^
main.cpp:47:1: error: expected ‘}’ at end of input
make: *** [main.o] Error 1

任何见识或帮助将是惊人的!

2 个答案:

答案 0 :(得分:1)

在您的

template <class T>
SimpleVector<T>::SimpleVector(int s)

最后一个for循环的开括号和闭括号不匹配。

在析构函数中,应将向量清除为aptr->clear();,因为aptr是指针变量。

答案 1 :(得分:0)

您可能错过了括号{ }或括号太多。检查关闭的}和打开的{代码块,并确保始终有一对,尤其是在错误的代码行之前查找。

特别是

for(int i=0;i<arraysize;i++)
{
    aptr[i]=0;
{