By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,688 Members | 1,092 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,688 IT Pros & Developers. It's quick & easy.

Design and development help in C++ text game

nemisis
P: 64
Hi Everyone I have am doing an object oriented C++ program and I have no idea as to how start it.................


This program prints a motion verb ( fly, run, swim, crawl, walk, or roll ), waits for a second,
then prints the name of an entity and repeats the verb (for example: fly ..1s.. pigeon fly!.. ).
The player has ½ second to type ‘y’ for yes or ‘n’ for no. (case insensitive)
- If the answer is correct, the player scores, if incorrect, or if there was no answer within the ½ second, the
player looses a point. When correct, all the motions the entity can perform are printed.

- When wrong, Ka..BOOM! is printed instead.

The default number of questions per player is 20, but the number can be changed by passing a different
number on the command line when starting the program.


At the end of the run the player’s score (and the score of previous players if there were any) is printed, the
question: “Another player (Y/N): “ is asked. The game exits if ‘n’ is typed.

The output should look something like this:-

Player 1 starting.
You must answer with the letter ‘y’ for YES, or ‘n’ for NO.
Press [Enter] when ready to start:

1 – fly pigeon fly!.. y
- I walk - I fly
2 – swim Wheelbarrow swim.. n
- I roll
3 – crawl ball crawl!..
Ka...BOOM!
4 – fly plane fly!.. y
- I roll - I fly.
5 – run boat run!.. n
- I roll.
6 – walk engine walk!..
Ka..BOOM!
7 – roll stone roll!.. y
- I roll.
. . .
16- crawl time crawl!.. y
- I crawl - I fly.
17- swim goose swim!.. n
Ka..BOOM!
18- run lizard run!.. y
- I run - I crawl.
19- run nose run.. y
- I run
20- fly goose fly!.. y
- fly - I walk – I run – I swim.

Player 1 *** score: 16 ***

Well done! Start another player (Y or N)? y

Player 2 starting
You must answer with the letter ‘y’ for YES, or ‘n’ for NO.
Press [Enter] when ready to start:

1 – crawl giraffe crawl!.. y
Ka..BOOM!



I am not that good at coding so i tried and made an overview of what the main driver should look like


main{
start_function(a,b)
{
check here wherther the player is old or new------>Connector

where a = value of yes or no, taken by cin>>
if the vaue is yes....
}
you are back in main >>>here

now start the for...while/Do...while[(condition for 1/2 min)& this shld take care of the initial 30 seconds also]
{
call function2(loop for 20 questions)
else if right answer add a point
Also call a function to print what the object does (for ball, it will be I ROLL)

keep adding ( + or -)points in a variable

End for loop of questions
}

display final score
ask to replay ------------>connector
}


But as said above I have no idea on the coding part of the whole program and would like some help here please

Also apart from this can anyone suggest a good compiler, I use Visual Basic C++ if thats good
May 21 '07 #1
Share this Question
Share on Google+
45 Replies


AdrianH
Expert 100+
P: 1,251
Ok, looks like you have a general flow of your programme done. Now can you identify the objects you require?


Adrian
May 21 '07 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
Pay attention to what AdrianH says. Your outline is not object-oriented. It is object-based. For it to be object-oriented, you will a) need to identify your entities, b) identify their abilites, c) construct a class hierarchy, d) use virtual functions.

Object-based programming is perfectly OK but should not be called object-oriented just becuse it has a class or a struct in it.
May 21 '07 #3

nemisis
P: 64
ok so how do i go abt it, its too complicated for me to code it!!!!!
May 22 '07 #4

nemisis
P: 64
btw what makes my program object oriented then............... i havent written the game code still as i am poor at coding but i am just curious as to what would make the overview Object Oriented , i mean what changes???
May 22 '07 #5

AdrianH
Expert 100+
P: 1,251
ok so how do i go abt it, its too complicated for me to code it!!!!!
Relax. First you don't code, you design.

When you design, think about what objects you need, you look at the requirements and start picking out nouns (things). When you have your nouns, see if any are of a common group. I.e.

dog, cat, wolf, bear -- These are all types of animals
corola, versa, echo, matrix -- These are all types of cars

Then once you have your objects, start picking out the related verbs (actions) for a noun.

dog -- walks, eats, runs
cat -- walks, eats, runs
wolf -- walks, eats, runs
bear -- walks, eats, runs
thus all animals (at least in this set) walks, eats, runs so can be used as function names.

You can even have questions too for things that may be different between them:
Expand|Select|Wrap|Line Numbers
  1. .
  2.        can get      has how 
  3.       newspaper?   many legs?
  4. dog      yes          4
  5. cat       no          4
  6. wolf      no          4
  7. bear      no          4
Given your description, can you state some objects, group them and make up some verbs/questions for them?


Adrian
May 22 '07 #6

nemisis
P: 64
Given your description, can you state some objects, group them and make up some verbs/questions for them?


Adrian
ok lets c


object- verbs

pigeon- fly, walk
wheelbarrow- roll
ball- roll, fall
stone- fall, roll
lizard -crawl, eat
wheels- roll, turn
cat- jump, walk, eat, swim
plane- fly, roll
time- crawl, fly
river- run
crocodile- crawl, swim, roll
fish- swim

each object has different functions some are common, this should give the output as required if designed for random printing of verbs as described in my very first post, what say???
May 22 '07 #7

AdrianH
Expert 100+
P: 1,251
ok lets c


object- verbs

pigeon- fly, walk
wheelbarrow- roll
ball- roll, fall
stone- fall, roll
lizard -crawl, eat
wheels- roll, turn
cat- jump, walk, eat, swim
plane- fly, roll
time- crawl, fly
river- run
crocodile- crawl, swim, roll
fish- swim

each object has different functions some are common, this should give the output as required if designed for random printing of verbs as described in my very first post, what say???
That's good. You will also need to have it able to display what it can do as well.

Sorry, but I need to go and will not be back for pretty much the rest of the day. If Weaknessforcat is around, s/he may be able to help you as s/he is a quite competent C++ programmer with OOP experience.

I’ll look in on your progress later. Good luck.


Adrian
May 22 '07 #8

nemisis
P: 64
ok thanks for everything till now, but i still have to finish it in 2 days before 25th May, so will look forward for help and fast
May 22 '07 #9

AdrianH
Expert 100+
P: 1,251
ok thanks for everything till now, but i still have to finish it in 2 days before 25th May, so will look forward for help and fast
Ok, you know the class syntax right? So generate a class for each object, inheiriting from a single class. Use the verbs you stated (and I suggested) as function calls.

Let me know if you have any troubles.


Adrian
May 23 '07 #10

AdrianH
Expert 100+
P: 1,251
Ok, you know the class syntax right? So generate a class for each object, inheriting from a single class. Use the verbs you stated (and I suggested) as function calls.

Let me know if you have any troubles.


Adrian
Let me know how things are going. You understand inheritance, corret?


Adrian
May 23 '07 #11

nemisis
P: 64
srry i had 2 test today so couldnt do c++.................. anyways yes i know classes, Inheritance i dont know much but i can look it up in books i have and probably do it. I will start writing stuff tomorrow and post it so if i have problems u can help. Thanks dude.
May 23 '07 #12

AdrianH
Expert 100+
P: 1,251
srry i had 2 test today so couldnt do c++.................. anyways yes i know classes, Inheritance i dont know much but i can look it up in books i have and probably do it. I will start writing stuff tomorrow and post it so if i have problems u can help. Thanks dude.
No prob. Good luck on your tests.


Adrian
May 23 '07 #13

weaknessforcats
Expert Mod 5K+
P: 9,197
dog -- walks, eats, runs
cat -- walks, eats, runs
wolf -- walks, eats, runs
bear -- walks, eats, runs
To make this object-oriented you do not use dog, cat. wolf, or bear in your code.

Instead, you deveop a class Animal and derive the dog, cat, wolf and bear from Animal. This says that a dog IS-A a Animal. You then create dog, cat, wolf or bear objects and pass them to functions that have Animal* or Animal& arguments. That means the walks, eats, runs have to be Animal methods BUT if you are passing a dog object to the function and you ask for the runs method, you expect to execute dog::runs and not Animal::runs. So you make the Animal methods virtual.

The entire application would use Animal* or Animal& arguments. Nowhere would you see dog, cat, wolf bear except where the objects are created and even there this is often hidden a by factory class where you ask for a specific kind of animal and get a pointer to it:

Expand|Select|Wrap|Line Numbers
  1. Animal * dog = AnimalFactory.Create(I_WANT_A_DOG);
  2.  

Unless you code this way, your program is not object-oriented.
May 23 '07 #14

nemisis
P: 64
but i am not using only animals, hence a class of animals does no good, instead what if i make a class for objects which include fish, wheel, goose, stone, etc and another class which includes verbs like run, swim, roll, etc and then create another class functions which inherits both the classes objects and verbs.( i ll need help again in Inheritance)
May 24 '07 #15

nemisis
P: 64
Ok, you know the class syntax right? So generate a class for each object, inheiriting from a single class. Use the verbs you stated (and I suggested) as function calls.

Let me know if you have any troubles.

no wait Adrian said something else above................ i ll work on it
May 24 '07 #16

nemisis
P: 64
Expand|Select|Wrap|Line Numbers
  1. class object
  2. {
  3.  
  4. private:
  5.  
  6.   char *verb1 = fly, *verb2 = walk, *verb3 = fall, *verb4= roll, *verb5 = crawl,  *verb6 = eat, *verb7= turn, *verb8= jump, *verb9= swim, *verb10 = run; 
  7.  
  8. public:
  9.   void printdata();
  10.  
  11. };
  12.  
  13. void verb::printdata()
  14. {
  15.   cout<<       // how to randomly print the verbs??? //
  16.  
  17. }
  18.  
  19.  
  20.  
  21. class pigeon::private object
  22. {
  23. private:
  24.   char *object1= pigeon;
  25.  
  26. public:
  27.   void printdata();
  28.   void functions();
  29. };
  30.  
  31.  
  32. void pigeon::printdata()
  33. {
  34.   cout<< *object1;
  35. }
  36.  
  37. void pigeon::functions()
  38. {
  39.   verb::printdata();
  40.   cout<< "I" << *verb1 << ", I " << *verb2 << endl;
  41. }


So the printdata of pigeon class prints name and the functions of pigeon prints what the pigeon does( I fly, I walk) . This will be for all objects like time, river,cat,etc.

i wasnt sure how to create "words" without using arrary in char so used pointers(and i am pretty sure its somehow wrong)

anyways plz tell me what next
May 24 '07 #17

nemisis
P: 64
ok srry will keep in mind next time i post a code
May 24 '07 #18

AdrianH
Expert 100+
P: 1,251
Ok nemis, you got a reasonable start with a few errors such as using the scope ‘verb’. I’m sure you didn’t mean that intentionally. Also, you don’t need to declare all the verbs in the base class, though it could be done depending on the design.

I would suggest using a base type that would check and display the types of things it does. And in addition, have a pure virtual function that would return a list (perhaps a vector) of things it can do.

In each class that inherits from the base class, I would have a vector class that would be initialized with the appropriate things it can do and implement the pure virtual function, returning that vector. Like this:
Expand|Select|Wrap|Line Numbers
  1. vector<char*> canDo;
  2.  
  3. canDo.push_back(“walk”);
  4. canDo.push_back(“fly”);
  5.  
NOTE: I am using a char* but this is because I am only pushing things that are literals (preallocated and will never be deallocated) so I will not have to worry about deleting them afterwards or someone deleting them on me. I would normally use a string object under other circumstances.

In your base class you can then iterate through the things it can do (calling the pure virtual function to get that information) when listing the types it uses or check if it can be done.

Does all of this make sense to you?

If you have any difficulties, let me know.


Adrian
May 24 '07 #19

nemisis
P: 64
sorry i hardly got what u wrote, i have heard of vectors but dont know how and why is it used, also if i do create a base class then what would that class contain??
May 24 '07 #20

AdrianH
Expert 100+
P: 1,251
sorry i hardly got what u wrote, i have heard of vectors but dont know how and why is it used, also if i do create a base class then what would that class contain??
Well you've got one, printdata(), you would need another one (maybe iscapableof(string& verb)?

A vector is a dynamicly allocated array in which you don't have to deal with the dynamic allcation.

See here for more information on vectors.
See here for more info on iterators


Adrian
May 24 '07 #21

nemisis
P: 64
so from what i read abt vectors and what u say, i deduct that u want me to create dynamic vectors for the words and another function besides mine, ok how am i suppose to do that in classes?



ps- ur iscapableof() function is same as my functions() in class pigeon
May 24 '07 #22

AdrianH
Expert 100+
P: 1,251
so from what i read abt vectors and what u say, i deduct that u want me to create dynamic vectors for the words and another function besides mine, ok how am i suppose to do that in classes?
Well, you know how to do inheritance (BTW, use public, not private inheritance), so in each of your specializations, create a vector and initialize it with the correct values. Then return that value in a private function. That function is the one that you will call in the functions you described

ps- ur iscapableof() function is same as my functions() in class pigeon
Oh, didn’t see it since you didn’t declare them in the class. Don’t forget to do that.


Adrian
May 24 '07 #23

nemisis
P: 64
Expand|Select|Wrap|Line Numbers
  1. class object
  2. {
  3. private:
  4. vector<char*> canDo;
  5.  
  6. public:
  7. void printdata();
  8. };
  9.  
  10.  
  11. void verb::printdata()
  12. {
  13. cout<<   canDo.push_back(“walk”)    // how to randomly print the verbs??? //
  14. }
  15.  
  16.  
  17.  
  18. class pigeon::public object
  19. {
  20. private:
  21. vector<char*> object;
  22.  
  23. public:
  24. void printdata();
  25. void functions();
  26. };
  27.  
  28.  
  29. void pigeon::printdata()
  30. {
  31. cout<< object.push_back(“pigeon”);;
  32. }
  33.  
  34. void pigeon::functions()
  35. {
  36. verb::printdata();
  37.  
  38. cout<< "I" << canDo.push_back(“walk”) << ", I " <<  canDo.push_back(“fly”) << endl;
  39.  
  40. }
  41.  
line 13 , i ask that question as in the main program i need to print random verbs

also Adrian , anything that could shorten the time to write this whole game text program as i got 5-6 hrs left to go( i know there are no short cuts here but i do hv to finish it on time!!!! lol)

so moving on what next??
May 24 '07 #24

nemisis
P: 64
anybody here i need to get this done before i fall down cuz of drowsiness!!!
May 24 '07 #25

AdrianH
Expert 100+
P: 1,251
Expand|Select|Wrap|Line Numbers
  1. class object
  2. {
  3. private:
  4. vector<char*> canDo;
  5.  
  6. public:
  7. void printdata();
  8. };
  9.  
  10.  
  11. void verb::printdata()
  12. {
  13. cout<<   canDo.push_back(“walk”)    // how to randomly print the verbs??? //
  14. }
  15.  
  16.  
  17.  
  18. class pigeon::public object
  19. {
  20. private:
  21. vector<char*> object;
  22.  
  23. public:
  24. void printdata();
  25. void functions();
  26. };
  27.  
  28.  
  29. void pigeon::printdata()
  30. {
  31. cout<< object.push_back(“pigeon”);;
  32. }
  33.  
  34. void pigeon::functions()
  35. {
  36. verb::printdata();
  37.  
  38. cout<< "I" << canDo.push_back(“walk”) << ", I " <<  canDo.push_back(“fly”) << endl;
  39.  
  40. }
  41.  
line 13 , i ask that question as in the main program i need to print random verbs

also Adrian , anything that could shorten the time to write this whole game text program as i got 5-6 hrs left to go( i know there are no short cuts here but i do hv to finish it on time!!!! lol)

so moving on what next??
Sorry, I was out.

You initialise in the constructor not in a function.

To iterate, you should look at one of the last 2 links I sent you about iteration and accessing the element through the iterator.

I will try and look in on you in an hour or less. Good luck.


Adrian
May 25 '07 #26

nemisis
P: 64
here is something new


Expand|Select|Wrap|Line Numbers
  1. Motion.h
  2.  
  3. #ifndef MOTION_H
  4. #define MOTION_H
  5.  
  6. #include <iostream>
  7. #include <string>
  8.  
  9. using namespace std;
  10.  
  11. class Motion
  12. {
  13.     friend ostream &operator << (ostream &out, const Motion &m);
  14.  
  15.     public:
  16.         Motion();
  17.         virtual ~Motion(){};
  18.         virtual string *toString() const = 0;
  19.         virtual bool contains(const string *key) const = 0;
  20. };
  21.  
  22.  
  23. #endif
  24.  

Motion.cc

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4.  
  5. #include "Motion.h"
  6.  
  7. ostream &operator << (ostream &out, const Motion &m)
  8. {
  9.     out << "- I " << m.toString();
  10.     return out;
  11. }

FlyMotion.h

Expand|Select|Wrap|Line Numbers
  1. #ifndef FLYMOTION_H
  2. #define FLYMOTION_H
  3.  
  4. #include <iostream>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. #include "Motion.h"
  10.  
  11. class FlyMotion : public Motion
  12. {
  13.     public:
  14.         static FlyMotion* instance();
  15.         virtual string *toString() const;
  16.         virtual bool contains(const string *key) const;
  17.  
  18.     protected:
  19.         FlyMotion();
  20.         FlyMotion(const FlyMotion&);
  21.         ~FlyMotion(){};
  22.  
  23.     private:
  24.         static FlyMotion *inst;
  25. //        return &inst;
  26. };
  27.  
  28.  
  29. #endif
FlyMotion.cc

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4.  
  5. #include "FlyMotion.h"
  6.  
  7. using namespace std;
  8.  
  9. FlyMotion* FlyMotion::inst = 0;
  10.  
  11. FlyMotion::FlyMotion()
  12. {
  13.     inst = 0;
  14. }
  15.  
  16. FlyMotion* FlyMotion::instance()
  17. {
  18.     if(inst == 0)
  19.     {
  20.         inst = new FlyMotion;
  21.     }
  22.     return inst;
  23. }
  24.  
  25. string* FlyMotion::toString() const
  26. {
  27.     ostringstream outStr;
  28.  
  29.     outStr << Motion::toString() << "fly " << flush;
  30.     return outStr.str();
  31. }

After compiling FlyMotion.cc this is what i get

Flymotion.cc: In member function 'virtual std::string* FlyMotion::toString() const'
line30: cannot convert 'std::basic_string<char, std::char_traits<char> std::allocator<char> >' to 'std::string*' in return
May 26 '07 #27

AdrianH
Expert 100+
P: 1,251
string* FlyMotion::toString() const
{
ostringstream outStr;

outStr << Motion::toString() << "fly " << flush;
return outStr.str();
}[/code]


After compiling FlyMotion.cc this is what i get

Flymotion.cc: In member function 'virtual std::string* FlyMotion::toString() const'
line30: cannot convert 'std::basic_string<char, std::char_traits<char> std::allocator<char> >' to 'std::string*' in return
Glad to have you back.

Return a string not a string*. I also don't think that using an ostringstream does much for you. Probably using a string and the + operator would be sufficent. If you do continue using a ostringstream, flush is a function, put () after it, or you will have a pointer address passed to your string.

I've only look at the error at this point. I'll take a look at the rest of your code later today.


Adrian
May 26 '07 #28

nemisis
P: 64
Glad to have you back.

Return a string not a string*.

I was here all the time.
So ok already tried everything instead of string*, I put char, char* , string , everything that came to my mind but it gave more errors so i changed it back to string*.



I also don't think that using an ostringstream does much for you. Probably using a string and the + operator would be sufficent. If you do continue using a ostringstream, flush is a function, put () after it, or you will have a pointer address passed to your string.
i put () after the flush, but then i got a error saying cant find flush as a function, so maybe it requires a header file, but then again it works perfectly fine without the (). as u said about the outstream i changed it to string and got more errors so left it as it is, since that too isnt giving trouble at the moment.

Well then again thats the only error i have......................

Flymotion.cc: In member function 'virtual std::string* FlyMotion::toString() const'
line30: cannot convert 'std::basic_string<char, std::char_traits<char> std::allocator<char> >' to 'std::string*' in return
May 26 '07 #29

AdrianH
Expert 100+
P: 1,251
I was here all the time.
So ok already tried everything instead of string*, I put char, char* , string , everything that came to my mind but it gave more errors so i changed it back to string*.
Get rid of the *. Make it return a 'string' not a 'string*'.

i put () after the flush, but then i got a error saying cant find flush as a function, so maybe it requires a header file, but then again it works perfectly fine without the (). as u said about the outstream i changed it to string and got more errors so left it as it is, since that too isnt giving trouble at the moment.
You are right. They must have done something to look for the flush address. See here for more info.

I've got to go out now. TTYL.


Adrian

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.
May 26 '07 #30

nemisis
P: 64
ok i ll go through the stuff till u get back then i have to sleep. see ya l8r
May 26 '07 #31

nemisis
P: 64
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!!!

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*.
May 26 '07 #32

AdrianH
Expert 100+
P: 1,251
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.
Expand|Select|Wrap|Line Numbers
  1. // Takes a string by value, constructor is invoked in this case (copy 
  2. // constructor if passed a string).
  3. // 
  4. // Returns a string by value, copy constructor is invoked.
  5. string fn1(string param)
  6. {
  7.   return param;
  8. }
  9.  
  10. int main()
  11. {
  12.   string var1("hello");  // Constructor to init var1 to contain "hello".
  13.  
  14.   string var2 = "hello"; // Even though this looks like an assignment, it is 
  15.                          // still an initialisation, so the same constructor
  16.                          // as above is called.
  17.  
  18.   string var2(var1);     // var1 is a string, so the copy constructor is 
  19.                          // invoked.
  20.  
  21.   string returnValue 
  22.     = fn1(var1);  // fn1 is passed var1 which is a string.  param is the same 
  23.                   // type, thus to initialise param, the copy constructor is
  24.                   // invoked.  This is known as passing by value.
  25.                   //
  26.                   // fn1 returns a string, param is also a string, the return
  27.                   // value is initialised using the copy constructor.
  28.   return 0;
  29. }
  30.  
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.
Expand|Select|Wrap|Line Numbers
  1. void fn2(string& param1, string const & param2)
  2. {
  3.   param1="bye";  // Not invoking constructor, invoking the assignment operator.
  4.                  // If no assignment operator function defined, it will destroy
  5.                  // var1 and invoke the copy constructor on the memory space of
  6.                  // var1.
  7.  
  8.   param2="lala"; // Invalid operation, param2 is a constant, thus cannot be 
  9.                  // changed.  Comment out to get to compile.
  10. }
  11.  
  12. int main()
  13. {
  14.   string var1("hello");  // Constructor to init var1 to contain "hello".
  15.   string var2("bye-bye");
  16.   string& var3(var1);    // Init to reference var1 (also known as an alias)
  17.   string& var4 = var2;   // Init to reference var2
  18.   fn2(var1, var2);
  19.  
  20.   cout << "var1: " << var1 << endl; // outputs "var1: bye"
  21.   cout << "var2: " << var2 << endl; // outputs "var2: bye-bye"
  22.   cout << "var3: " << var3 << endl; // outputs "var3: bye"
  23.   cout << "var4: " << var4 << endl; // outputs "var4: bye-bye"
  24.  
  25.   return 0;
  26. }
  27.  
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.

Expand|Select|Wrap|Line Numbers
  1. void fn3(string* param1, string const * param2)
  2.   if (param1 == NULL) {
  3.     cout << "param1 is NULL" << endl;
  4.   }
  5.   else {
  6.     cout << "param1 points at string '" << *param1 
  7.          << "'.  Its length is " << param1->length()
  8.          << ".  Adding ' there' to it." << endl;
  9.     *param1 += " there";
  10.   }
  11.  
  12.   if (param2 == NULL) {
  13.     cout << "param2 is NULL" << endl;
  14.   }
  15.   else {
  16.     cout << "param2 points at string '" << *param2 
  17.          << "'.  Its length is " << (*param1).length() // also valid
  18.          << ".  Cannot add ' there' to it." << endl;
  19.     //*param2 += " there";  // INVALID since pointing at a const value.
  20.   }
  21. }
  22.  
  23. int main()
  24. {
  25.   string var1("hello");
  26.   string var2("bye-bye");
  27.   string* var3(&var1);   // Init to point at var1
  28.   string* var4 = &var2;  // Init to point at var2
  29.   fn3(var1, var2);
  30.  
  31.   cout << "var1: " << var1 << endl; // outputs "var1: bye"
  32.   cout << "var2: " << var2 << endl; // outputs "var2: bye-bye"
  33.   cout << "var3: " << var3 << endl; // outputs "var3: bye"
  34.   cout << "var4: " << var4 << endl; // outputs "var4: bye-bye"
  35.  
  36.   return 0;
  37. }
  38.  
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
May 27 '07 #33

nemisis
P: 64
ok I changed string* to string................. now i get these errors


flymotion.cc: In static member function 'static FlMotion* FlyMotion::instance()':
flymotion.cc:line 20:error: cannot allocate an object of type 'flymotion'
flymotion.cc:line 20:error: because the following virtual functions are abstract:
Motion.h:line 17:error: virtual bool Motion::contains(std::string) const
May 27 '07 #34

AdrianH
Expert 100+
P: 1,251
ok I changed string* to string................. now i get these errors


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

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


Adrian
May 27 '07 #35

nemisis
P: 64
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
May 27 '07 #36

Savage
Expert 100+
P: 1,764
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
May 27 '07 #37

nemisis
P: 64
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...............
May 27 '07 #38

nemisis
P: 64
You didn't declare and define bool FIMotion::contains(string) const function.

the bool FlyMotion::contains(string) is declared in Motion.h line 17 but i cant figure out what to define in it?? any suggestions??
May 27 '07 #39

nemisis
P: 64
Expand|Select|Wrap|Line Numbers
  1. #ifndef MOTION_H
  2. #define MOTION_H
  3.  
  4. #include <iostream>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. class Motion
  10. {
  11.     friend ostream &operator << (ostream &out, const Motion &m);
  12.  
  13.     public:
  14.         Motion();
  15.         virtual ~Motion(){};
  16.         virtual string toString() const = 0;
  17.         virtual bool contains(const string key) const = 0;
  18. };
  19.  
  20.  
  21. #endif


Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4.  
  5. #include "Motion.h"
  6.  
  7. ostream &operator << (ostream &out, const Motion &m)
  8. {
  9.     out << "- I " << m.toString();
  10.     return out;
  11. }

Expand|Select|Wrap|Line Numbers
  1. #ifndef FLYMOTION_H
  2. #define FLYMOTION_H
  3.  
  4. #include <iostream>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. #include "Motion.h"
  10.  
  11. class FlyMotion : public Motion
  12. {
  13.     public:
  14.         static FlyMotion* instance();
  15.         virtual string toString() const;
  16.         virtual bool contains(const string key) const;
  17.  
  18.     //protected:
  19.         FlyMotion();
  20.         FlyMotion(const FlyMotion&);
  21.         ~FlyMotion(){};
  22.  
  23.     private:
  24.         static FlyMotion *inst;
  25.      // return &inst;
  26. };
  27.  
  28.  
  29. #endif

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4.  
  5. #include "FlyMotion.h"
  6.  
  7. using namespace std;
  8.  
  9. FlyMotion* FlyMotion::inst = 0;
  10.  
  11. FlyMotion::FlyMotion()
  12. {
  13.     inst = 0;
  14. }
  15.  
  16. FlyMotion* FlyMotion::instance()
  17. {
  18.     if(inst == 0)
  19.     {
  20.         inst = new FlyMotion;
  21.     }
  22.     return inst;
  23. }
  24.  
  25.  
  26. string FlyMotion::toString() const 
  27. {
  28.   ostringstream outStr;
  29.  
  30.    outStr << Motion::toString() << "fly " << flush;
  31.  
  32. return outStr.str();
  33. }        



------ 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::FlyMotion(void)" (??0FlyMotion@@QAE@XZ)


FlyMotion.obj : error LNK2001: unresolved external symbol "public: virtual bool __thiscall FlyMotion::contains(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)const " (?contains@FlyMotion@@UBE_NV?$basic_string@DU?$cha r_traits@D@std@@V?$allocator@D@2@@std@@@Z)


FlyMotion.obj : error LNK2019: unresolved external symbol "public: virtual class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Motion::toString(void)const " (?toString@Motion@@UBE?AV?$basic_string@DU?$char_t raits@D@std@@V?$allocator@D@2@@std@@XZ) referenced in function "public: virtual class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall
FlyMotion::toString(void)const " (?toString@FlyMotion@@UBE?AV?$basic_string@DU?$cha r_traits@D@std@@V?$allocator@D@2@@std@@XZ)


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


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


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


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


========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
May 28 '07 #40

nemisis
P: 64
I solved all errors on my own,added new .cc file

Flypigeonfly.cc

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <set>
  3.  
  4. #include "Motion.h"
  5. #include "FlyMotion.h"/*
  6. #include "RunMotion.h"
  7. #include "SwimMotion.h"
  8. #include "CrawlMotion.h"
  9. #include "WalkMotion.h"
  10. #include "RollMotion.h"*/
  11.  
  12. const int size = 6;
  13.  
  14. int main()
  15. {
  16.     set<Motion> motions;
  17.  
  18.     FlyMotion fly = new FlyMotion();
  19.  
  20.     motions.insert(fly);
  21.  
  22.  
  23.     cout << "Press Enter to continue..." << flush;
  24.     cin.get();
  25.     return 0;
  26. }
and this is the error now

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

AdrianH
Expert 100+
P: 1,251
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
May 28 '07 #42

nemisis
P: 64
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.

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
May 28 '07 #43

AdrianH
Expert 100+
P: 1,251
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:

Expand|Select|Wrap|Line Numbers
  1. .       Noun
  2.          ^
  3.          |
  4.          +-------+-----+-----...
  5.          |       |     |      
  6.         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.):

Expand|Select|Wrap|Line Numbers
  1. if (Lizard::instance().contains(verb)) {
  2.   cout << Lizard::instance().toString() << endl;
  3. }
  4. else {
  5.   cout << "Ka-BOOM!" << endl;
  6. }
  7.  
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
May 28 '07 #44

nemisis
P: 64
Sorry to hear that. A solution I would have use would have had a hierarchy that looked something like this:

Expand|Select|Wrap|Line Numbers
  1. .       Noun
  2.          ^
  3.          |
  4.          +-------+-----+-----...
  5.          |       |     |      
  6.         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
May 28 '07 #45

AdrianH
Expert 100+
P: 1,251
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
You can PM them to me. Have you ever used an console app called uuencode? You can get a copy here for win32 (use uuencode/uudecode link). Zip the files up, uuencode them and paste them into the message body.

I make no warrenties of any binaries coming from bastet.com. If you do not trust bastet.com, search for uuencode on another website.


Adrian
May 28 '07 #46

Post your reply

Sign in to post your reply or Sign up for a free account.