-2

I am learning templates in C++, and a problem on my homework asks me the following:

//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 

What does this even mean? I've tried typing void enemyAdjust(RapidDog) and its variations, as well as others, but none of them seem to be right. What is it asking for? I understand the basics of template functions, and can write one, but I just don't understand the question or what I'm supposed to do.

Note: It's an online homework; it tells you whether you got it right or wrong and then asks you to try again to continue.

Alex G
  • 747
  • 4
  • 15
  • 27
  • I'm not familiar with the term 'prototyping', perhaps it means declaring (not defining): http://coliru.stacked-crooked.com/a/74391f228a7719cc – Marco A. Jun 04 '15 at 08:49
  • What do you mean "none seem to be right"? How are we to guess what the automatic verifier wants to see? There is nothing wrong with this template function definition. – StoryTeller - Unslander Monica Jun 04 '15 at 08:50
  • Probably you are asked to invoke the enemyAdjust function with a parameter of type RapidDog. E.g. like: `enemyAdjust(RapidDog());` or by doing it literally `enemyAdjust(RapidDog());` – W.F. Jun 04 '15 at 08:50
  • Or perhaps he just used a wrong terminology for "instantiate it explicitly" http://coliru.stacked-crooked.com/a/9c4e6e5749535290 – Marco A. Jun 04 '15 at 08:52
  • @MarcoA.: No, you're thinking of C. There's no notion of "prototype" in C++, and the term survives only in the name "prototype scope". – Kerrek SB Jun 04 '15 at 08:53
  • I already suspected the terminology was used incorrectly, you're confirming that one – Marco A. Jun 04 '15 at 08:55
  • Well, this sucks. Seems like the page/question/wording is at fault here, not the code. Thanks. – Alex G Jun 04 '15 at 08:57
  • Are you by any chance taking Ferguson's class? – Persomatey May 28 '17 at 07:50
  • I was, yes. No longer, but hope this helped. – Alex G May 30 '17 at 21:23

4 Answers4

3

Usually a prototype is a function's signature (its declaration). Here, however, you are probably asked to instantiate it explicitly.You can do it be typing:

template void enemyAdjust<RapidDog>(RapidDog objparm); // explicit instantiation
Adam Kosiorek
  • 1,438
  • 1
  • 13
  • 17
1

The question is asking: Write one line of code to prototype a template function using type: RapidDog. In this case, "T" is just the unknown type T that the professor must be referring to. So here the type is RapidDog, leading to:

void enemyAdjust(RapidDog  objparm);

Another template example would be this:

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

and just use the type that you wish. It's just plain substitution.

int func1(int a, int b);
double func1(double a, double b);
TheSynnott
  • 21
  • 6
0

It probably asks you to create a class called RapidDog. This class will need to be suitable for being replaced in the template instantiation in the enemyAdjust function.

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

should do the trick.

dau_sama
  • 4,247
  • 2
  • 23
  • 30
  • 1
    Nope. It only gives one single line to type in code. Usually these things are something like void enemyAdjust(); or something. I just can't figure out what this one's asking for. – Alex G Jun 04 '15 at 08:51
0

The object RapidDog should provide methods that enemyAdjust() template function uses. Did you check whether moveposition() and fireweapon() exist in RapidDog class? You should implement them, if they do not exist.

  • There is no further defined class. All I have as information is what I have posted in the code block. That is the question and its givens as presented in the homework material. – Alex G Jun 04 '15 at 08:55