使用C ++

时间:2015-06-04 08:43:11

标签: c++

我正在学习C ++中的模板,我的作业出现问题如下:

//Use the template below to answer the questions and create C++ functions:
template<class  T>  void  enemyAdjust(T  objparm)
{
  objparm.moveposition();
  objparm.fireweapon();
} 
//Write one line of code to prototype a template function using type: RapidDog 

这甚至意味着什么?我尝试输入void enemyAdjust(RapidDog)及其变体,以及其他变体,但似乎没有一个是正确的。它要求什么?我理解模板功能的基础知识,可以写一个,但我不理解这个问题或我应该做什么。

注意:这是一个在线作业;它告诉你是对还是错,然后再请你再试一次。

4 个答案:

答案 0 :(得分:3)

通常原型是函数的签名(它的声明)。但是,在这里,您可能会被要求明确地实例化它。您可以输入:

template void enemyAdjust<RapidDog>(RapidDog objparm); // explicit instantiation

答案 1 :(得分:0)

它可能会要求您创建一个名为RapidDog的类。 这个类需要适合在enemyAdjust函数的模板实例化中替换。

struct RapidDog
{
  void moveposition() {};
  void fireweapon {};
};

应该这样做。

答案 2 :(得分:0)

对象RapidDog应该提供enemyAdjust()模板函数使用的方法。你检查了RapidDog类中是否存在moveposition()和fireweapon()?如果它们不存在,你应该实现它们。

答案 3 :(得分:0)

问题在于:使用类型:RapidDog编写一行代码来模拟模板函数。在这种情况下,&#34; T&#34;只是教授必须提到的未知类型T.所以这里的类型是RapidDog,导致:

public int Id { get; set; }
public string Name { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Country { get; set; }
public string ZipCode { get; set; }
public int KeyId { get; set; }
public AddressType Type { get; set; }

另一个模板示例如下:

void enemyAdjust(RapidDog  objparm);

并使用您想要的类型。它只是简单的替代。

template<class  T>   T  func1(T  a, T  b)