Design and development help in C++ text game

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nemisis
    New Member
    • May 2007
    • 61

    #31
    ok i ll go through the stuff till u get back then i have to sleep. see ya l8r

    Comment

    • nemisis
      New Member
      • May 2007
      • 61

      #32
      Originally posted by AdrianH
      Get rid of the *. Make it return a 'string' not a 'string*'.

      doesnt work i have to change it in all files ie. motion.h, motion.cc and flymotion.h and after doing that too i am getting more errors which is way out of my understanding!! !

      Originally posted by AdrianH
      EDIT: P.S. If you want, you can take a look at a preliminary document I am writing regarding Parsing in C++ here. It is in review so that is why it is located in the Editors Corner.
      Again, i am not that familiar with parsing so i will have a look 2morrow as soon as i am out of bed since nothing is getting inside at this time of the night. Good night


      ps- if u want i can post the errors i get when i change to string from string* , i didnt as there are 4-5 of them and i got confused lookin at them hence put back string*.

      Comment

      • AdrianH
        Recognized Expert Top Contributor
        • Feb 2007
        • 1251

        #33
        Ok, when you state a type (whether it is a return type, parameter type or variable type) there are three ways of declaring it in C++, by value, by reference and by indirection (I'm not sure if indirection is the standard term though).

        By value means that you have an object. If you initialise an object by another object a constructor is invoked to initialise the object. If is initialised with another object of the same type the copy constructor is invoked to copy the object. The copy constructor is just a special constructor that takes a const type reference. More on that type later. E.g.
        [code=cpp]
        // Takes a string by value, constructor is invoked in this case (copy
        // constructor if passed a string).
        //
        // Returns a string by value, copy constructor is invoked.
        string fn1(string param)
        {
        return param;
        }

        int main()
        {
        string var1("hello"); // Constructor to init var1 to contain "hello".

        string var2 = "hello"; // Even though this looks like an assignment, it is
        // still an initialisation, so the same constructor
        // as above is called.

        string var2(var1); // var1 is a string, so the copy constructor is
        // invoked.

        string returnValue
        = fn1(var1); // fn1 is passed var1 which is a string. param is the same
        // type, thus to initialise param, the copy constructor is
        // invoked. This is known as passing by value.
        //
        // fn1 returns a string, param is also a string, the return
        // value is initialised using the copy constructor.
        return 0;
        }
        [/code]

        By reference means that you are borrowing the actual object. Thus, if you modify it (and it is not a const type reference), the original object will be modified. Behind the scenes, this may look like a const pointer (see by indirection). It is almost always initialised with an actual object.
        [code=cpp]
        void fn2(string& param1, string const & param2)
        {
        param1="bye"; // Not invoking constructor, invoking the assignment operator.
        // If no assignment operator function defined, it will destroy
        // var1 and invoke the copy constructor on the memory space of
        // var1.

        param2="lala"; // Invalid operation, param2 is a constant, thus cannot be
        // changed. Comment out to get to compile.
        }

        int main()
        {
        string var1("hello"); // Constructor to init var1 to contain "hello".
        string var2("bye-bye");
        string& var3(var1); // Init to reference var1 (also known as an alias)
        string& var4 = var2; // Init to reference var2
        fn2(var1, var2);

        cout << "var1: " << var1 << endl; // outputs "var1: bye"
        cout << "var2: " << var2 << endl; // outputs "var2: bye-bye"
        cout << "var3: " << var3 << endl; // outputs "var3: bye"
        cout << "var4: " << var4 << endl; // outputs "var4: bye-bye"

        return 0;
        }
        [/code]

        By indirection means that you indirectly point at the object you are referring to. To get access to the object, you either use the dereference operator (‘*’), or the member selection operator (‘->’) which is only valid for non-base types. The value of the pointer can be any value between 0 (usually called NULL) and the maximum addressable space value.

        [code=cpp]
        void fn3(string* param1, string const * param2)
        if (param1 == NULL) {
        cout << "param1 is NULL" << endl;
        }
        else {
        cout << "param1 points at string '" << *param1
        << "'. Its length is " << param1->length()
        << ". Adding ' there' to it." << endl;
        *param1 += " there";
        }

        if (param2 == NULL) {
        cout << "param2 is NULL" << endl;
        }
        else {
        cout << "param2 points at string '" << *param2
        << "'. Its length is " << (*param1).lengt h() // also valid
        << ". Cannot add ' there' to it." << endl;
        //*param2 += " there"; // INVALID since pointing at a const value.
        }
        }

        int main()
        {
        string var1("hello");
        string var2("bye-bye");
        string* var3(&var1); // Init to point at var1
        string* var4 = &var2; // Init to point at var2
        fn3(var1, var2);

        cout << "var1: " << var1 << endl; // outputs "var1: bye"
        cout << "var2: " << var2 << endl; // outputs "var2: bye-bye"
        cout << "var3: " << var3 << endl; // outputs "var3: bye"
        cout << "var4: " << var4 << endl; // outputs "var4: bye-bye"

        return 0;
        }
        [/code]

        NOTE: there are no constructors that will convert a string* to a string (although one could theoretically be made, it makes little sense and could result in inadvertent conversions) or a string to a string* (there is no automated way to make such a conversion).

        I hope that what I have written will help point you in understanding what you are doing wrong.


        Adrian

        Comment

        • nemisis
          New Member
          • May 2007
          • 61

          #34
          ok I changed string* to string......... ........ now i get these errors


          flymotion.cc: In static member function 'static FlMotion* FlyMotion::inst ance()':
          flymotion.cc:li ne 20:error: cannot allocate an object of type 'flymotion'
          flymotion.cc:li ne 20:error: because the following virtual functions are abstract:
          Motion.h:line 17:error: virtual bool Motion::contain s(std::string) const

          Comment

          • AdrianH
            Recognized Expert Top Contributor
            • Feb 2007
            • 1251

            #35
            Originally posted by nemisis
            ok I changed string* to string......... ........ now i get these errors


            flymotion.cc: In static member function 'static FlMotion* FlyMotion::inst ance()':
            flymotion.cc:li ne 20:error: cannot allocate an object of type 'flymotion'
            flymotion.cc:li ne 20:error: because the following virtual functions are abstract:
            Motion.h:line 17:error: virtual bool Motion::contain s(std::string) const
            You didn't declare and define bool FIMotion::conta ins(string) const function.

            Won't be in for the rest of the day.


            Adrian

            Comment

            • nemisis
              New Member
              • May 2007
              • 61

              #36
              Originally posted by AdrianH
              Won't be in for the rest of the day.


              Adrian
              hhmmmmmm just when i need u............. o well i aint sleeping for 48 hrs so whenever u come back i ll still be here

              Comment

              • Savage
                Recognized Expert Top Contributor
                • Feb 2007
                • 1759

                #37
                Originally posted by nemisis
                hhmmmmmm just when i need u............. o well i aint sleeping for 48 hrs so whenever u come back i ll still be here
                What a irony,eh?

                PS:Subscribing

                Savage

                Comment

                • nemisis
                  New Member
                  • May 2007
                  • 61

                  #38
                  Originally posted by Savage
                  What a irony,eh?

                  PS:Subscribing

                  Savage

                  better get to work then i was slackin cuz i thought adrian wasnt around, well next post in an hr or so............. ..

                  Comment

                  • nemisis
                    New Member
                    • May 2007
                    • 61

                    #39
                    Originally posted by AdrianH
                    You didn't declare and define bool FIMotion::conta ins(string) const function.

                    the bool FlyMotion::cont ains(string) is declared in Motion.h line 17 but i cant figure out what to define in it?? any suggestions??

                    Comment

                    • nemisis
                      New Member
                      • May 2007
                      • 61

                      #40
                      [CODE=cpp]#ifndef MOTION_H
                      #define MOTION_H

                      #include <iostream>
                      #include <string>

                      using namespace std;

                      class Motion
                      {
                      friend ostream &operator << (ostream &out, const Motion &m);

                      public:
                      Motion();
                      virtual ~Motion(){};
                      virtual string toString() const = 0;
                      virtual bool contains(const string key) const = 0;
                      };


                      #endif[/CODE]



                      [CODE=cpp]#include <iostream>
                      #include <sstream>
                      #include <string>

                      #include "Motion.h"

                      ostream &operator << (ostream &out, const Motion &m)
                      {
                      out << "- I " << m.toString();
                      return out;
                      }[/CODE]


                      [CODE=cpp]#ifndef FLYMOTION_H
                      #define FLYMOTION_H

                      #include <iostream>
                      #include <string>

                      using namespace std;

                      #include "Motion.h"

                      class FlyMotion : public Motion
                      {
                      public:
                      static FlyMotion* instance();
                      virtual string toString() const;
                      virtual bool contains(const string key) const;

                      //protected:
                      FlyMotion();
                      FlyMotion(const FlyMotion&);
                      ~FlyMotion(){};

                      private:
                      static FlyMotion *inst;
                      // return &inst;
                      };


                      #endif[/CODE]


                      [CODE=cpp]#include <iostream>
                      #include <sstream>
                      #include <string>

                      #include "FlyMotion. h"

                      using namespace std;

                      FlyMotion* FlyMotion::inst = 0;

                      FlyMotion::FlyM otion()
                      {
                      inst = 0;
                      }

                      FlyMotion* FlyMotion::inst ance()
                      {
                      if(inst == 0)
                      {
                      inst = new FlyMotion;
                      }
                      return inst;
                      }


                      string FlyMotion::toSt ring() const
                      {
                      ostringstream outStr;

                      outStr << Motion::toStrin g() << "fly " << flush;

                      return outStr.str();
                      } [/CODE]




                      ------ Build started: Project: Project1, Configuration: Debug Win32 ------
                      Linking...

                      FlyMotion.obj : error LNK2019: unresolved external symbol "public: __thiscall Motion::Motion( void)" (??0Motion@@QAE @XZ) referenced in function "public: __thiscall FlyMotion::FlyM otion(void)" (??0FlyMotion@@ QAE@XZ)


                      FlyMotion.obj : error LNK2001: unresolved external symbol "public: virtual bool __thiscall FlyMotion::cont ains(class std::basic_stri ng<char,struct std::char_trait s<char>,class std::allocator< char> >)const " (?contains@FlyM otion@@UBE_NV?$ basic_string@DU ?$char_traits@D @std@@V?$alloca tor@D@2@@std@@@ Z)


                      FlyMotion.obj : error LNK2019: unresolved external symbol "public: virtual class std::basic_stri ng<char,struct std::char_trait s<char>,class std::allocator< char> > __thiscall Motion::toStrin g(void)const " (?toString@Moti on@@UBE?AV?$bas ic_string@DU?$c har_traits@D@st d@@V?$allocator @D@2@@std@@XZ) referenced in function "public: virtual class std::basic_stri ng<char,struct std::char_trait s<char>,class std::allocator< char> > __thiscall
                      FlyMotion::toSt ring(void)const " (?toString@FlyM otion@@UBE?AV?$ basic_string@DU ?$char_traits@D @std@@V?$alloca tor@D@2@@std@@X Z)


                      LIBCMT.lib(crt0 .obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStar tup


                      C:\Documents and Settings\Admin\ Desktop\c++\Pro ject1\Debug\Pro ject1.exe : fatal error LNK1120: 4 unresolved externals


                      Build log was saved at "file://c:\Documents and Settings\Admin\ Desktop\c++\Pro ject1\Project1\ Debug\BuildLog. htm"


                      Project1 - 5 error(s), 0 warning(s)


                      ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

                      Comment

                      • nemisis
                        New Member
                        • May 2007
                        • 61

                        #41
                        I solved all errors on my own,added new .cc file

                        Flypigeonfly.cc

                        [CODE=cpp]#include <iostream>
                        #include <set>

                        #include "Motion.h"
                        #include "FlyMotion. h"/*
                        #include "RunMotion. h"
                        #include "SwimMotion .h"
                        #include "CrawlMotio n.h"
                        #include "WalkMotion .h"
                        #include "RollMotion .h"*/

                        const int size = 6;

                        int main()
                        {
                        set<Motion> motions;

                        FlyMotion fly = new FlyMotion();

                        motions.insert( fly);


                        cout << "Press Enter to continue..." << flush;
                        cin.get();
                        return 0;
                        }[/CODE]

                        and this is the error now

                        FlyPigeonFly.cc
                        ..\..\FlyPigeon Fly.cc(18) : error C2440: 'initializing' : cannot convert from 'FlyMotion *' to 'FlyMotion'
                        No constructor could take the source type, or constructor overload resolution was ambiguous

                        Comment

                        • AdrianH
                          Recognized Expert Top Contributor
                          • Feb 2007
                          • 1251

                          #42
                          Read post 33 again. It relates to all class types, not just strings. I only used strings as it was a well defined example.

                          Can you tell me why you would have an instance() function? I'm not saying it is good or bad, I'm just asking your reasoning.


                          Adrian

                          Comment

                          • nemisis
                            New Member
                            • May 2007
                            • 61

                            #43
                            Originally posted by AdrianH
                            Read post 33 again. It relates to all class types, not just strings. I only used strings as it was a well defined example.

                            ok thanks got it.

                            Originally posted by AdrianH
                            Can you tell me why you would have an instance() function? I'm not saying it is good or bad, I'm just asking your reasoning.


                            Adrian
                            Thats for the main when i call randomly to print the motion, I really dont know exactly how but its given as a hint to me.

                            Morever I already gave up the program without a proper compilation, so I am done with this........... ..... now wait for results........ ... Thank You.

                            Well seeing everyone around in forums

                            Comment

                            • AdrianH
                              Recognized Expert Top Contributor
                              • Feb 2007
                              • 1251

                              #44
                              Originally posted by nemisis
                              Thats for the main when i call randomly to print the motion, I really dont know exactly how but its given as a hint to me.

                              Morever I already gave up the program without a proper compilation, so I am done with this........... ..... now wait for results........ ... Thank You.

                              Well seeing everyone around in forums
                              Sorry to hear that. A solution I would have use would have had a hierarchy that looked something like this:

                              Code:
                              .       Noun
                                       ^
                                       |
                                       +-------+-----+-----...
                                       |       |     |      
                                      Lizard  Time  Stone
                              The verbs would have been queried inside of the object (Using your function names. This is only an example. It would be implemented in the actual programme slightly differently as you would have to have this for every type if you did it this way.):

                              [code=cpp]
                              if (Lizard::instan ce().contains(v erb)) {
                              cout << Lizard::instanc e().toString() << endl;
                              }
                              else {
                              cout << "Ka-BOOM!" << endl;
                              }
                              [/code]
                              See how you delegate the problem to a more trivial one. The main code knows nothing about how the information is processed, it just asks using the given interface.


                              Adrian

                              Comment

                              • nemisis
                                New Member
                                • May 2007
                                • 61

                                #45
                                Originally posted by AdrianH
                                Sorry to hear that. A solution I would have use would have had a hierarchy that looked something like this:

                                Code:
                                .       Noun
                                         ^
                                         |
                                         +-------+-----+-----...
                                         |       |     |      
                                        Lizard  Time  Stone
                                The verbs would have been queried inside of the object (Using your function names. This is only an example. It would be implemented in the actual programme slightly differently as you would have to have this for every type if you did it this way.):


                                Adrian
                                Thats exactly how i did it, but i messed up the code very badly, if u want i can post or pm them, whatever u like , but there are around 10- 12 files anyways i cant go through all that, hv had enuf for 2day i ll pursue it 2morrow if i feel like it

                                Comment

                                Working...