首页 > 解决方案 > 模板函数调用

问题描述

#include <iostream>
#include <memory>
#include <initializer_list>
#include <cassert>
template <typename T>
class Vector
{
    // Your implementation of the Vector class starts here

    private:
//attributes
    int length;
    T* data;

    public:
//constructors
    Vector()
    :length(0),
    data(nullptr) 
    {
        std::cout<<"New empty object created"<<std::endl;
    }

    Vector(int length)
    :length(length), 
    data(new T[length])
    {
        std::cout<< "Length of object is = " <<length <<std::endl;
    }

    //use a copy of another Vector
    Vector(const Vector& other)
    : Vector(other.length)
    {
        for(int i=0;i<length;i++)
            data[i]=other.data[i];

    }

    // using initializer list
    Vector(std::initializer_list<T> list)
    : Vector((int)list.size())
    {
        std::uninitialized_copy(list.begin(), list.end(), data);
        std::cout<< "The elements in the object are: ";
        for (auto it = std::begin(list); it!=std::end(list); ++it)
        std::cout << ' ' << *it;
        std::cout<<std::endl;
    }

//operators

    //copy
    Vector<T>& operator=(const Vector<T>& other)
    {
        if(this!= &other)
        {
            std::cout<<"Copied constructor"<<std::endl;
            delete[] data;
            length = other.length;
            std::cout<<"New length ="<<length<<std::endl;
            data = new T[length];
            std::cout<<"The elements in the object are: ";
            for(int i=0;i<length;i++)
            {
                data[i]=other.data[i];
                std::cout<<data[i]<<" ";
            }
        }
        std::cout<<std::endl;
        //std::cout << "copy operator" << std::endl;
        return *this;
    }
    //move
    Vector<T>& operator=(Vector<T>&& other)
    {
        if(this!= &other)
        {
            delete[] data;
            length = other.length;
            data = new T[length];
            for(int i=0;i<length;i++){
                data[i]=other.data[i];}
            other.length = 0;
            delete[] other.data;
            other.data = nullptr;
        }
        //std::cout << "Move operator" << std::endl;
        return *this;
    }
    ///////////////////////////////////////////////
    //add
    Vector<T> operator+(const Vector<T>& other) const
    {
        assert(length == other.length);
        Vector<T> newer(length);
        std::cout<<"The addition gives: ";
        for(auto i=0;i<length;i++)
        {
            newer.data[i] = data[i]+other.data[i];
            std::cout<<newer.data[i]<<" ";
        }
        std::cout<<std::endl;
        return newer;
    }
    //minus
    Vector<T> operator-(const Vector<T>& other) const
    {
        assert(length == other.length);
        Vector<T> newer(length);
        for(auto i=0;i<length;i++)
            newer.data[i] = data[i]-other.data[i];
        return newer;
    }
    // Multiply by a scalar
    Vector<T> operator*(T scalar)
    {
        Vector<T> newer(length);
        std::cout<<"Multiplication of a new vector by scalar provides: ";
        for (auto i=0; i<length; i++)
        {
            newer.data[i] = data[i] * scalar;
            std::cout<<newer.data[i]<<" ";
        }
        std::cout<<std::endl;
        return newer;
    }
    //////////////////////////////////////////
    // Add to existing Vector
    Vector<T>& operator+=(const Vector<T>& other)
    {
        for (auto i=0; i<length; i++)
            data[i] += other.data[i];
        return *this;
    }
    // Multiply existing Vector by a scalar
    Vector<T>& operator*=(T scalar)
    {
        std::cout<<"Multiplication of an existing vector by scalar provides: ";
        for (auto i=0; i<length; i++)
        {
            data[i] *= scalar;
            std::cout<<data[i]<<" ";
        }
        std::cout<<std::endl;
        return *this;
    }
    double Valueform(int i)
    {
        return data[i];
    }
    int Lengthfinder()
    {
        return length;
    }
    ///////////////////////////////////////////
//destructor
    ~Vector()
    {
        delete[] data;
        data = nullptr;
        length = 0;
    }
};

template<typename T>
T dot(const Vector<T>& lhs, const Vector<T>& rhs)
{
    // Your implementation of the dot function starts here
    T result = 0;
    for (auto i=0; i<lhs.Lengthfinder(); i++)
    {
        result = lhs.Valueform(i)*rhs.Valueform(i);
        //result + = multiply(lhs.data[i],rhs.data[i]);
       // result += lhs.data[i]*rhs.data[i];
    }
    return result;
}
//failsafe for value multiplied by a vector
template <typename T, typename I>
Vector<T> operator*(I i,Vector<T> other)
{
    std::cout<<"Multiplication was done by a vector and failsafe activated"<<std::endl;
    return(other* T(i)) ;
}


int main()
{
    Vector<double> a(5);
    Vector<double> b({ 1 , 2 , 3 , 4 });
    Vector<double> c({ 2 , 3 , 4 });
    Vector<double> d({ 5 , 2 , 1 });   

   // std::cout<< "a=c" <<std::endl;
    a = c;      
  //  std::cout<< "e = c+d" <<std::endl;
    Vector<double> e;
    e = c+d;
  //  std::cout<< "f = c*5" <<std::endl;
    Vector<double> f;
    f = c*5;
 //   std::cout<< "g = 5*d" <<std::endl;
    Vector<double> g;
    g = 5*d;
    Vector<double> Dott;
    Dott = dot(c,d);
    return 0;
}

代码不允许我调用函数 Valueform 和 Lengthfinder,任何人都可以解决我可以获得特定数据值和长度的工作,因为类变量是私有的?这些函数主要围绕模板工作,但我只想调用 2 个函数来获取某些属性,它们给了我不应该存在的错误,尽管我不确定为什么。

标签: c++c++11

解决方案


您的dot函数采用常量向量:

template<typename T>
T dot(const Vector<T>& lhs, const Vector<T>& rhs)
{ //  ^^^^^-----------------^^^^^----- these are const
    // Your implementation of the dot function starts here
}

但是,您的成员函数未标记为 const。您应该将它们声明为 const 以使这些函数可用:

double Valueform(int i) const
{
    return data[i];
}
int Lengthfinder() const
{
    return length;
}

推荐阅读