首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >模板声明不能出现在块范围内。

模板声明不能出现在块范围内。
EN

Stack Overflow用户
提问于 2020-02-27 15:20:11
回答 2查看 1.2K关注 0票数 0

所以这是我的课,坦率地说,我以前从未使用过模板。这里是我的简单vector.h文件,但是我保留了模板不能出现在块作用域中的获取错误。我对此的理解是,我试图在其中定义一个函数。这是我的代码:

代码语言:javascript
复制
#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);

}

这是我要犯的错误。

代码语言:javascript
复制
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

任何洞察力或帮助都将是惊人的!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-02-27 15:32:48

在您的

代码语言:javascript
复制
template <class T>
SimpleVector<T>::SimpleVector(int s)

最后,for循环的开始和结束大括号不匹配。

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

票数 1
EN

Stack Overflow用户

发布于 2020-02-27 15:29:56

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

特别是在

代码语言:javascript
复制
for(int i=0;i<arraysize;i++)
{
    aptr[i]=0;
{
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60436460

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档