Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.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++_Operator Overloading_Subscript - Fatal编程技术网

C++ C++;运算符[]的重载不使用模板类

C++ C++;运算符[]的重载不使用模板类,c++,operator-overloading,subscript,C++,Operator Overloading,Subscript,我有一个模板类概要文件,它由一个std::vector和一些我在该向量上执行的函数组成。我试图使用Profile::operator[]直接访问向量,但在编译时出错 侧面图 template <class T> class Profile { public: vector<T> mElements; template <class T> T& Profile<T>::operator[] (const int in

我有一个模板类概要文件,它由一个std::vector和一些我在该向量上执行的函数组成。我试图使用Profile::operator[]直接访问向量,但在编译时出错

侧面图

template <class T>
class Profile
{
public:
    vector<T> mElements;

    template <class T>
    T& Profile<T>::operator[] (const int index)
    {
        return mElements[index];
    }

    template <class T>
    unsigned int Profile<T>::size ()
    {
        return mElements.size();
    }

    ...
}
模板
班级简介
{
公众:
矢量融合;
模板
T&Profile::运算符[](常量int索引)
{
返回熔化[指数];
}
模板
无符号整数配置文件::大小()
{
返回mElements.size();
}
...
}
当我尝试在代码的另一部分中使用运算符时:

Profile<float> oldProfile;
vector<float> shiftedVector (oldProfile.size(), 0.0);
int shift = 3;

for (i=0 ; i<oldProfile.size() ; ++i)
{
    shift++;

    if (shift > oldProfile.size())
    {
        shift = 0;
    }

    shiftedVector[shift] = oldProfile[i];
}
Profile-oldProfile;
向量移位向量(oldProfile.size(),0.0);
int-shift=3;
对于(i=0;i oldProfile.size())
{
移位=0;
}
移位向量[shift]=oldProfile[i];
}
编译时,会出现以下错误:

  • 无法将分配中的“配置文件”转换为“浮动”

我读到的所有东西都说这应该行得通。唯一的区别是我使用的是一个模板类。我是否遗漏了一些明显的东西?

我想你的意思是:

template <class T>
class Profile
{
public:
    vector<T> mElements;

    T& operator[](const int index) { return mElements[index]; }

    unsigned int size() { return mElements.size(); }

    // ...
};
模板
班级简介
{
公众:
矢量融合;
T&运算符[](常量int索引){返回元素[index];}
无符号int size(){return mElements.size();}
// ...
};

我想你的意思很简单:

template <class T>
class Profile
{
public:
    vector<T> mElements;

    T& operator[](const int index) { return mElements[index]; }

    unsigned int size() { return mElements.size(); }

    // ...
};
模板
班级简介
{
公众:
矢量融合;
T&运算符[](常量int索引){返回元素[index];}
无符号int size(){return mElements.size();}
// ...
};

据我所知,您可以用一行简单的代码替换所有代码,从而解决整个问题:

template<class... Args> using Profile = std::vector<Args...>;
这种方法的优点是:

  • 容器无关算法
  • 您不会纠缠容器的接口

据我所知,您可以通过用一行简单的代码替换所有代码来解决整个问题:

template<class... Args> using Profile = std::vector<Args...>;
这种方法的优点是:

  • 容器无关算法
  • 您不会纠缠容器的接口

如上所述,我决定将shift()函数作为Profile类的一部分。这消除了重载运算符[]的需要,并更好地封装了Profile类的详细信息


谢谢你的帮助。您的回答能够激励我为我的类找到更好的方向。

正如我前面提到的,我决定将shift()函数作为Profile类的一部分。这消除了重载运算符[]的需要,并更好地封装了Profile类的详细信息


谢谢你的帮助。您的回答能够激励我为我的课程找到更好的方向。

您的成员函数和运算符不需要是模板。我可以问一下
概要文件
课程的目的是什么吗?它可能会帮助我给你一个更好的答案。它被用来比较已知数组和新数组。这些值是新数组被操纵以试图找到与已知数组最匹配的值。但是,在我思考的过程中,我意识到最好是将shift()函数合并到Profile类中。这解决了我的问题,消除了对运算符[]函数的需要。您的成员函数和运算符不需要是模板。请问
配置文件
类的用途是什么?它可能会帮助我给你一个更好的答案。它被用来比较已知数组和新数组。这些值是新数组被操纵以试图找到与已知数组最匹配的值。但是,在我思考的过程中,我意识到最好是将shift()函数合并到Profile类中。这就解决了我的问题,不再需要运算符[]函数。