|
-
April 20th, 2009, 05:59 PM
#16
Re: Structure Question.
 Originally Posted by Lindley
It sounds to me like you want a std::map< std::string, Weapon >. That's the closest you can get to what you seem to be trying to do.
Alternatively, you could look into inheritance, for instance defining ShortSword as a subclass of Weapon. That wouldn't be worthwhile unless you had a number of different ShortSword objects you expected to be manipulating with various properties.
From your questions, I'm wondering whether you really understand what object scope is all about.
No Im not sure, I've just started to learn C++ and its not in the book I have (Game Institute, Game Programming 1).
But could you give me a link to an explanation / example?
-
April 20th, 2009, 06:11 PM
#17
Re: Structure Question.
Hm, now I just changed some code and it works.
Earlier:
Code:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include "items.h"
using namespace std;
int Init()
{
Weapon ShortSword;
ShortSword.Create("Short Sword", "Common", 1, 10, 1, 4);
}
int main()
{
srand(time(0));
Init();
cout << ShortSword.mName;
system("pause");
return 0;
}
Changed To:
Code:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include "items.h"
using namespace std;
int main()
{
srand(time(0));
Weapon ShortSword;
ShortSword.Create("Short Sword", "Common", 1, 10, 1, 4);
cout << ShortSword.mName;
system("pause");
return 0;
}
So basically all I did was to create the structure in main, instead of in a function with the creation that is called in main.
Can someone explain why it works to create it in main, but not in a function which is called in main?
Thanks.
-
April 20th, 2009, 06:28 PM
#18
Re: Structure Question.
 Originally Posted by ahoodin
I understood him quite well.
Apparently not, since it took 12 posts for someone to address what he was actually wanting to do.
As for being a lowly human, that is a career choice.
(so I assume you speak for yourself)
Unless you happen to be fluent in machine code, you're a lowly human in this context.
Can someone explain why it works to create it in main, but not in a function which is called in main?
http://en.wikipedia.org/wiki/Scope_(programming)
http://en.wikipedia.org/wiki/Variabl...ope_and_extent
If the book you're learning from has gotten you into functions and structures without explaining something as basic as scope, I'd really suggest looking for another book.
Last edited by Speedo; April 20th, 2009 at 06:32 PM.
-
April 20th, 2009, 06:40 PM
#19
Re: Structure Question.
I do understand scopes, but what im asking is: do you have to create structures in main like this:
Code:
int main()
{
Weapon sword;
Weapon dagger;
Weapon axe;
Weapon bow;
}
And is it impossible to create them in void functions which is called in main when to create them?
Code:
void Call();
int main()
{
Call();
}
void Call()
{
Weapon sword;
Weapon dagger;
Weapon axe;
Weapon bow;
}
Its hard to explain it when everyone is thinking so complex.
-
April 20th, 2009, 06:55 PM
#20
Re: Structure Question.
Code:
void Call( )
{
Weapon sword;
Weapon dagger;
Weapon axe;
Weapon bow;
} // <-- sword, dagger, axe and bow go out of scope at this point, and no longer exist
There are various methods to have a function create objects that are useable after that function ends. Namely, have the function take some STL container by reference and insert the objects into that container.
Simple example using a map:
Code:
typedef std::map<std::string, Weapon> WeaponMap;
void Create(WeaponMap& wm)
{
wm["sword"] = Weapon(/*params*/);
wm["dagger"] = Weapon(/*params*/);
// etc
}
int main( )
{
WeaponMap weps;
Create(weps);
cout << weps["sword"].mName;
return 0;
}
Last edited by Speedo; April 20th, 2009 at 07:00 PM.
-
April 20th, 2009, 07:11 PM
#21
Re: Structure Question.
I think I understand how it works now, thanks mate.
-
April 20th, 2009, 09:04 PM
#22
Re: Structure Question.
 Originally Posted by Speedo
Unless you happen to be fluent in machine code, you're a lowly human in this context.
I'm afraid I don't care for the context. Speak for yourself mate.
 Originally Posted by Speedo
Apparently not, since it took 12 posts for someone to address what he was actually wanting to do.
Well again I am afraid this is a matter of opinion.
ahoodin
To keep the plot moving, that's why.

Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|