|
-
April 20th, 2009, 04:47 PM
#1
Structure Question.
Hello, im new with Object oriented programming.
I'm having a problem with a structure, what I'm trying to do is to make a function to create a sub structure named depending on what the input is, let me show what ive done.
Code:
items.h
struct Weapon
{
// Strings
string mName;
string mQuality;
// Integers
int mItemID;
int mGoldValue;
int mDamageLow;
int mDamageHigh;
};
items.cpp
void Create(string name, string qual, int ID, int value, int dmgLow, int dmgHigh)
{
Weapon name;
name.mName = name;
name.mQuality = qual;
name.mItemID = ID;
name.mGoldValue = value;
name.mDamageLow = dmgLow;
name.mDamageHigh = dmgHigh;
}
Now, I get an error that says: (declaration of 'Weapon name' shadows a parameter ).
Obviously I'm doing something wrong since im trying to make the parameter "name" be the name of the sub structure, for example: if user puts in "ShortSword" the name of it should be
"ShortSword.mName", and so on...
So is there any easier (working?) way to do this?
Thanks in advance.
-
April 20th, 2009, 05:11 PM
#2
Re: Structure Question.
In your main function, you have both a string and a 'Weapon' object with the same identifier, 'name'
Intel Core Duo Macbook w/ Mac OS 10.5.6
gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1
-
April 20th, 2009, 05:11 PM
#3
Re: Structure Question.
I believe that you need to use this syntax:
Code:
struct Weapon name;
instead of
EDIT and probably also change the name of your Weapon to something as per Etherous.
Duplicate variable names will cause quite a row with your compiler spitting all sorts of
related and seemingly non-related error messages.
Last edited by ahoodin; April 20th, 2009 at 05:16 PM.
ahoodin
To keep the plot moving, that's why.

-
April 20th, 2009, 05:15 PM
#4
Re: Structure Question.
Another point is that you can both create a constructor for your struct and change your syntax to support a more simple variable declaration:
Code:
typedef struct Weapon {
//...your stuff
} WEAPON, *PWEAPON;
then you can declare as such:
ahoodin
To keep the plot moving, that's why.

-
April 20th, 2009, 05:15 PM
#5
Re: Structure Question.
Might be more convenient in the long run to just do this:
Code:
typedef struct Weapon
{
...
} Weapon;
Edit: ahoodin was faster
Last edited by Etherous; April 20th, 2009 at 05:21 PM.
Intel Core Duo Macbook w/ Mac OS 10.5.6
gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1
-
April 20th, 2009, 05:19 PM
#6
Re: Structure Question.
I believe that I mentioned that, although if you are referring to the use of capital letters, that appears to be the standard in books regarding the usage of typedef.
ahoodin
To keep the plot moving, that's why.

-
April 20th, 2009, 05:20 PM
#7
Re: Structure Question.
 Originally Posted by ahoodin
I believe that I mentioned that, although if you are referring to the use of capital letters, that appears to be the standard in books regarding the usage of typedef.
Sorry. You must have posted yours before I'd posted mine saying the same thing
Intel Core Duo Macbook w/ Mac OS 10.5.6
gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1
-
April 20th, 2009, 05:24 PM
#8
Re: Structure Question.
The really odd thing about all this is that I believe we appear to be posting at precisely the same errr ....minute...lol I would not say second because it is not that precise.
I feel like Kreyton the robot from Red Dwarf, my favorite tv show
http://watchreddwarf.com/category/se...dwarf-season-1
ahoodin
To keep the plot moving, that's why.

-
April 20th, 2009, 05:35 PM
#9
Re: Structure Question.
All the stuff about needing "struct" in the type and the typedefs to get around it and whatnot.....doesn't apply to C++. Only C. And since this struct contains a std::string, there's no chance of using it in a C program anyway.
-
April 20th, 2009, 05:37 PM
#10
Re: Structure Question.
Thanks for all the answers!
I changed it some now, but ran into another problem.
Heres the new code:
items.h
Code:
#ifndef ITEMS_H
#define ITEMS_H
#include <string>
using namespace std;
struct Weapon
{
void Create(string name, string qual, int ID, int value, int dmgLow, int dmgHigh);
// Strings
string mName;
string mQuality;
// Integers
int mItemID;
int mGoldValue;
int mDamageLow;
int mDamageHigh;
};
#endif
items.cpp
Code:
#include "items.h"
#include <string>
using namespace std;
void Weapon::Create(string name, string qual, int ID, int value, int dmgLow, int dmgHigh)
{
mName = name;
mQuality = qual;
mItemID = ID;
mGoldValue = value;
mDamageLow = dmgLow;
mDamageHigh = dmgHigh;
}
game.cpp
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 << endl;
system("pause");
return 0;
}
But now i get these errors:
E:\Program\C++ Game Programming Files\Quillon\game.cpp In function `int main()':
22 E:\Program\C++ Game Programming Files\Quillon\game.cpp `ShortSword' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)
E:\Program\C++ Game Programming Files\Quillon\Makefile.win [Build Error] [game.o] Error 1
Any suggestions?
-
April 20th, 2009, 05:42 PM
#11
Re: Structure Question.
Well at this point it appears to be a problem with your scope. Your ShortSword variable only has local scope to Init(). It needs to be upgraded to either class scope, or declared in main.
ahoodin
To keep the plot moving, that's why.

-
April 20th, 2009, 05:42 PM
#12
Re: Structure Question.
Obviously I'm doing something wrong since im trying to make the parameter "name" be the name of the sub structure, for example: if user puts in "ShortSword" the name of it should be
"ShortSword.mName", and so on...
What you want isn't possible in C++. Understand, the identifiers in your code "Weapon", "name", "mQuality", or whatever, don't even exist when the program runs. They are just used to make it easy for us lowly humans to read and understand code.
If you want to identify something by a user defined name, you give it a string attribute to hold that name. Which you've already done. Or, depending on exactly what your needs are, you may to look at std::map.
-
April 20th, 2009, 05:44 PM
#13
Re: Structure Question.
I understood him quite well. As for being a lowly human, that is a career choice.
(so I assume you speak for yourself)
ahoodin
To keep the plot moving, that's why.

-
April 20th, 2009, 05:45 PM
#14
Re: Structure Question.
So i need to make it into a class or declare in main?
If i made, lets say 100 different Weapons, it would be a little too much code that goes into main, so isnt there any other way to make it into a function to make it look cleaner in main?
-
April 20th, 2009, 05:53 PM
#15
Re: Structure Question.
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.
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
|