0

I have done this assignment as best as i understood it but i am pretty sure there are radical problems. I'll be Looking forward to your comments on how to make it better. The problem definition is as follows: Two classes. XBoxGame and WorldPeace.

Template class called Gift. Has one method called shake(). Inside shake(), we are going to call MakeNoise() on an instance of our template type T. ONLY XBoxGame has MakeNoise() as a method.

Now in main() as soon as you try to make a "Gift that holds an XBoxGame" (see what I did there?) you'll know if it works when you compile. Try to Gift<WorldPeace> and it won't compile because MakeNoise() is missing. What you turn in should just have the correctly templated XBoxGame class calling shake(), and a WorldPeace() that compiles, but only because it is not used in main().

End result "You can only put a class that makes noise in a gift". The Code i wrote:

#include <iostream>   
  class XBoxGame
  {
      void makeNoise(int,int)
    {
      std::cout << "Is this Working ? ";
    };
  };

class WorldPeace
{};


  template <typename T>
   class Gift
   {
     public:
       T shake()
     {
       void XboxGame::makeNoise();
     }
   };

The Main:

#include "Template.h"
using namespace std;
int main()
{
 Gift <XBoxGame> ;
 Gift <WorldPeace>;

return 0;
}
max66
  • 65,235
  • 10
  • 71
  • 111
ArashDe
  • 23
  • 1
  • 7

1 Answers1

0

I see some problems in your code.

In no particular order...

(1) in Gift::shake() you call XboxGame::makeNoise() but you defined the class with an upper B (XBoxGame).

(2) in Gift::shake() you write

void XBoxGame::makeNoise();

that is a prototipe for a method, not a call

(3) in Gift::shake(), if you remove void (see point 2), the following instruction

XBoxGame::makeNoise();

become the call to a makeNoise() static method in XBoxGame class; but you defined makeNoise() as a not static method.

You can define makeNoise() as static or you can call from an actual XBoxGame object; something like

  T shake()
   {
     XBoxGame xbg;

     xbg.makeNoise();
   }

or even

  T shake()
   { XBoxGame().makeNoise(); }

(4) you've (implicitly) defined makeNoise() as a private method of XBoxGame. If you want call it in a Gift object, you have to declare it as public (or define XBoxGame as a struct, so implicitly declaring makeNoise() as public

(5) you've defined makeNoise(), in XBoxGame, as a method that receive a couple of (unused) int values but you call it without arguments; you have to correct this point; a solution can be passing a couple of ints calling it; by example

  T shake()
   { XBoxGame().makeNoise(0, 0); }

(6) in main(), le following instructions

Gift <XBoxGame>;
Gift <WorldPeace>;

are declaring nothing; you should give a couples of names for a cuoples of variables

Gift<XBoxGame>    gx;
Gift<WorldPeace>  gv;

or transform this in the creation of a couple of temporary objects (avoiding a couples of "unused variable" warnings)

Gift<XBoxGame>();
Gift<WorldPeace>();

(7) if you want to see the "Is this Working ?" messages, you have to call the shake() methods

Gift<XBoxGame>().shake();
Gift<WorldPeace>().shake();

(8) your shake method is declared as returning T but return nothing; this is a problem that you can solve, by example, defining it as a void method

  void shake()
   { XBoxGame().makeNoise(0, 0); }

So, the following code compiles

#include <iostream>   

class XBoxGame
 {
   public:
      void makeNoise(int,int)
       { std::cout << "Is this Working ? " << std::endl; };
 };

class WorldPeace
 {};


template <typename T>
class Gift
 {
   public:
      void shake()
       { XBoxGame().makeNoise(0, 0); }
 };

int main()
 {
   Gift<XBoxGame>().shake();
   Gift<WorldPeace>().shake();
 }

Hope this helps.

-- EDIT --

(9) Not sure to understand but, if you want that only Gift<XBoxGame> compile (because only XBoxGame implement makeNoise(), I suppose you should write shake() using the T type; so something like

  void shake()
   { T().makeNoise(0, 0); }

if makeNoise() is an ordinary method in XBoxGame or

  void shake()
   { T::makeNoise(0, 0); }

if makeNoine() is a static method.

max66
  • 65,235
  • 10
  • 71
  • 111
  • Thank you so much max66. you explained everything perfectly, and i understood all my problems man. Thx – ArashDe Feb 14 '17 at 01:05