Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 模板声明不能出现在块范围中_C++_C++11 - Fatal编程技术网

C++ 模板声明不能出现在块范围中

C++ 模板声明不能出现在块范围中,c++,c++11,C++,C++11,这是给我的班级的,坦率地说,我以前从未用过模板。这是我的简单vector.h文件,但我保留了获取模板不能出现在块范围内的错误。我对这一点的理解是,这表明我正试图在函数中定义它。这是我的密码: #ifndef SIMPLEVECTOR_H #define SIMPLEVECTOR_H #include <iostream> #include <new> // Needed for bad-alloc exception #include <cstdlib> //

这是给我的班级的,坦率地说,我以前从未用过模板。这是我的简单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);

}


\ifndef SIMPLEVECTOR\u H
#定义SIMPLEVECTOR_H
#包括
#包含//错误的alloc异常所需
#包含退出功能所需的//项
使用名称空间std;
样板
类SimpleVector{
私人:
T*aptr;//指向分配的数组
int arraysize;//数组中的元素数
void memError();//处理内存分配错误
void subError();//处理的下标超出范围
公众:
SimpleVector()
{
aptr=0;arraysize=0;
}
SimpleVector(int-s);
SimpleVector(const SimpleVector&);
~SimpleVector();
int size()常量
{
返回阵列化;
}
T getElementAt(int-sub);
T&运算符[](常量int&);
};
#endif//SIMPLEVECTOR\u H
样板
SimpleVector::SimpleVector(int s)
{

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

尤其是在

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

template <class T>
SimpleVector<T>::SimpleVector(int s)
模板
SimpleVector::SimpleVector(int s)
最后一个循环的
的开始括号和结束括号不匹配


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

您能花点时间重新格式化显示的代码,使其具有清晰的逻辑缩进吗?照目前的情况,显示的代码由于其随意的缩进,大部分都是不可读和不可理解的。大多数编辑器都有一种简单的方法来逻辑地重新插入代码。使用它。也许您甚至会发现呃,你的代码中的输入错误,导致了这些编译错误!但是,在所有情况下,帮助你的人必须能够阅读显示的代码。在错误前几行有一个
{
应该是
}
。是的,就是这样,非常感谢!
template <class T>
SimpleVector<T>::SimpleVector(int s)