【问题标题】:Class template with array or vector arguments具有数组或向量参数的类模板
【发布时间】:2020-05-19 18:22:23
【问题描述】:

我需要编写一个类模板定义,其中包含两个模板参数(type、functor)和两个模板参数(array/std::vector、int),它们可以执行以下代码:

    const char* message= "Message";

    const transform<char, firstFunctor> first(message, lengthOfMessage);

    transform_view<int, secondFunctor> second(intArray, intSize);

    transform<double, thirdFunctor> third(doubleArray, doubleSize);

数组/向量的类型必须与第一个模板参数的类型匹配。

我尝试了一些这样的变体:

   template <typename A, typename B>
   class transform
   {
   public:
    transform<A, B>(A[], B) {...};
   }

但我无法让构造函数的第一个参数匹配所有三种类型。

感谢任何建议,谢谢!

【问题讨论】:

    标签: c++ arrays templates vector class-template


    【解决方案1】:

    你写错了构造函数的定义。

    transform&lt;A, B&gt;(A[], B) {...};你会传递一个向量,那你为什么把A[]写成参数类型呢?

    你需要类似下面的东西

    #include <iostream>
    
    template <typename T>
    struct functor{
        void operator()(const T array [], size_t sze) {
            for (int i{}; i < sze; ++i) {
                std::cout << array[i] << " ";
            }
            std::cout << "\n";
        }
    };
    
    template<typename T, typename Function>
    class transform {
        const T* array;
        size_t sze;
        Function functor{};
    public:
        transform(const T array [], size_t sze):array{array}, sze{sze}{
            functor(array, sze);
    
        }
    };
    
    template< typename T, typename E>
    using transform_view =  transform<T, E>;
    
    
    int main()
    {
        using firstFunctor = functor<char>;
        using secondFunctor = functor<int>;
        using thirdFunctor = functor<double>;
        const char *message = "Message";
        size_t lengthOfMessage = 7;
    
        int intArray[] = {1, 3};
        size_t intSize = 2;
    
        double doubleArray[] = {1.4, 3.2};
        size_t doubleSize = 2;
    
        //The given three lines
        const transform<char, firstFunctor> first(message, lengthOfMessage);
    
        transform_view<int, secondFunctor> second(intArray, intSize);
    
        transform<double, thirdFunctor> third(doubleArray, doubleSize);
    }
    The output
    M e s s a g e
    1 3
    1.4 3.2
    

    【讨论】:

    • 谢谢,但是transform的参数应该是两个。这段代码:“ transform_view second(intArray, intSize); ”不能根据赋值改变。
    • 谢谢!它适用于数组。现在我只需要让它与向量一起工作(第二个构造函数)。
    【解决方案2】:

    据我了解,问题是第一个参数是模板中的数组。 试试下面的构造函数:

    transform(A* arr, B el){
        //now here is a tricky part, because there is a very big difference
        //between a char*, and an int/double/unsigned/float/... *.
    
    }
    

    如果您在类中有一个类型为 A 的数组,并且想将其更改为传递的数组:

    private:
        A* my_array;
    

    你可以这样尝试:

    if(dynamic_cast<char*>(arr)) //if arr is of type char* {
    
        if (this->my_array != nullptr) delete my_array; //not needed if in a basic constructor...
        size_t len = strlen(arr);
        my_array = new char [len + 1];
        strcpy(this->my_array, arr); //(destination, source)
        my_array[len] = '\0';
    } 
    else //if it is a numeric array
    {
    this->my_array = arr;
    //redirecting the pointers in enough
    }
    

    哦,如果你在 Visual Studio 上,如果你写 strcpy 就可以了

    '#pragma warning (disable: 4996)'在文件顶部。

    否则它会将其标记为不安全并建议 strncpy、strcpy_s、...

    【讨论】:

    • 谢谢。当我运行代码时,我仍然得到“没有构造函数实例与参数列表匹配”所以我需要作为参数传递的数组仍然不被接受。
    • 这很有趣。消息中的某处应该有类似于“参数类型是:...”的文本,并且它应该显示类中的参数类型和传递给构造函数的对象类型。如果您可以在此处复制并粘贴它们,它可能会有所帮助。 (甚至可能是整个消息)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-25
    • 1970-01-01
    • 1970-01-01
    • 2018-12-16
    • 2010-12-17
    • 2014-09-16
    • 1970-01-01
    相关资源
    最近更新 更多