简体   繁体   English

C++ 中的模板/参考

[英]Templates in C++ / reference

It's 3:30am and I'm working on a exercise about templates in C++.现在是凌晨 3:30,我正在做一个关于 C++ 模板的练习。 I don't get what I'm doing wrong in this case, could you please help me understand?我不明白在这种情况下我做错了什么,你能帮我理解吗? (I'm not very good at C++, started 2 weeks ago). (我不太擅长 C++,两周前开始学习)。

Here is the subject:这是主题:

[...], you must code the function template foreach . [...],您必须编写函数模板 foreach 。 This function allows to skim through an array by calling a function for each element of this array.此函数允许通过为该数组的每个元素调用一个函数来浏览数组。 The function accepts as argument the address of the beginning of the array, a reference on function and the size of the array.该函数接受数组开头的地址、函数的引用和数组的大小作为参数。 The reference on function corresponds to the following prototype: void func(const type& elem);对函数的引用对应于以下原型: void func(const type& elem); Moreover you must provide the function print that is passed to the function foreach and that displays each elements, one per line, whatever their type.此外,您必须提供传递给函数 foreach 并显示每个元素的函数 print,每行一个,无论它们的类型如何。

And here is my code:这是我的代码:

ex03.hpp ex03.hpp

#ifndef __EX03_H_INCLUDED__
# define __EX03_H_INCLUDED__

#include <iostream>
#include <iomanip>

template<typename type>
void foreach(type tab[0], (*)(print(const type& elem)), type size)
{
  int                   i;

  for (i = 0; i != size; i++)
    print(tab[i]);
}

template<typename type>
void print(const type& elem)
{
  std::cout << elem << std::endl;
}

#endif /* !__EX03_H__ */

and:和:

main.cpp主程序

#include "ex03.hpp"

int main(void)  
{
  int tab[] = { 11, 3, 89, 42 };
  foreach(tab, print<int>, 4);
  std::string tab2[] = { "j’", "aime", "les", "templates", "!" };
  foreach(tab2, print, 5);
  return 0;
}

I'm only allowed to turn in ex03.hpp, this is only a test main.我只允许上交ex03.hpp,这只是一个测试主。 I would like to understand what I'm doing wrong.我想了解我做错了什么。 Thanks!谢谢!

The problem is in foreach templated function:问题出在 foreach 模板化函数中:

  • first parameter can be a pointer to the array or it can just be static array array with undefined size第一个参数可以是指向数组的指针,也可以是大小未定义的静态数组数组
  • second parameter can be a function pointer or a reference to the function (which should accept constant element of same type as array elements by reference, as it will not be modified)第二个参数可以是函数指针或对函数的引用(它应该接受与数组元素相同类型的常量元素作为引用,因为它不会被修改)
  • third parameter should be different type as array size is not necessarilly same type as array element type第三个参数应该是不同的类型,因为数组大小不一定与数组元素类型相同

Fixed ex03.hpp looks like this:固定的 ex03.hpp 看起来像这样:

#ifndef __EX03_H_INCLUDED__
#define __EX03_H_INCLUDED__

#include <iostream>

// option 1
template<typename T1, typename T2>
void foreach(T1* arr, void (*fnc)(const T1&), T2 size) {
    for (T2 i = 0; i < size; i++) {
        (*fnc)(arr[i]);
    }
}

// option 2
template<typename T1, typename T2>
void foreach(T1 arr[], void (&fnc)(const T1&), T2 size) {
    for (T2 i = 0; i < size; i++) {
        (*fnc)(arr[i]);
    }
}

template<typename T1>
void print(const T1& element) {
    std::cout << element << std::endl;
}

#endif /* !__EX03_H__ */

And the main.cpp:和 main.cpp:

#include "ex03.hpp"

#include <string>

int main() {
    const size_t tab1Size = 4;
    int tab1[tab1Size] = { 11, 3, 89, 42 };

    foreach(tab1, print, tab1Size);

    const size_t tab2Size = 5;
    std::string tab2[tab2Size] = { "j’", "aime", "les", "templates", "!" };
    foreach(tab2, print, tab2Size);

    return 0;
}

But i agree as others said before, you should first learn the basics of c++, so it can be easier to understand more complicated stuff.但我同意之前其他人所说的,你应该先学习 C++ 的基础知识,这样才能更容易理解更复杂的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM