Problem with function returning a structure

This is a discussion on Problem with function returning a structure within the C++ Programming forums, part of the General Programming Boards category; I'm trying to write a simple text based RPG. This is the pertinent code: Code: //========================================= //PitII- Text based RPG ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    1

    Problem with function returning a structure

    I'm trying to write a simple text based RPG. This is the pertinent code:
    Code:
    //=========================================
    //PitII- Text based RPG
    //01-20-07 to __-__-__
    //=========================================
    //INCLUDES
    #include <iostream>
    #include <string>
    #include <vector>
    #include <ctime>
    
    //USING
    using namespace std;
    
    //FUNCTIONS
    void clear();
    string begin_game();
    int new_room();
    struct weapon create_new_weapon(int char_lvl, int mon_lvl);
    
    //THE SAD, LONELY, choice
    char choice;
    
    int main()
    {
    	//Classes for use throughout the ENTIRE GAME
    	struct weapon
    	{
    		string name;
    		int min;
    		int max;
    		int bonus;
    	};
    	struct armor
    	{
    		string name;
    		int def;
    		int block;
    	};
    	class Monster
    	{
    	public:
    		string name;
    		int lvl;
    		int hp;
    		int hp_max;
    		int mind;
    		int maxd;
    		int armor;
    		int block;
    	};
    	class Hero
    	{
    	public:
    		string name;
    		int hp;
    		int hp_max;
    		string weapon_name;
    		int mind;
    		int maxd;
    		string armor_name;
    		int armor;
    		int block;
    		int lvl;
    		int exp;
    		int exp_need;
    		void show_stats()
    		{
    			cout<<name<<":\n"
    				<<"\tHealth:  "<<hp<<"/"<<hp_max<<"\n"
    				<<"\tLevel:   "<<lvl<<"\n"
    				<<"\tExp/Need:"<<exp<<"/"<<exp_need<<"\n"
    				<<weapon_name<<":\n"
    				<<"\tDamage:  "<<mind<<"-"<<maxd<<"\n"
    				<<armor_name<<":\n"
    				<<"\tArmor:   "<<armor<<"\n"
    				<<"\tBlock%:  "<<block<<"%\n";
    			cout<<"Press any key to continue.\n>:";
    			cin>>choice;
    		};
    	};
    
    
    	//Begin Game:
    	Hero player;
    	player.hp = 50;
    	player.hp_max = 50;
    	player.weapon_name = "Rusty Dagger";
    	player.mind = 1;
    	player.maxd = 3;
    	player.armor_name = "Cloth Armor";
    	player.armor = 15;
    	player.block = 5;
    	player.exp = 0;
    	player.exp_need = 100;
    	player.lvl = 1;
    	player.name = begin_game();
    	cout<<player.name<<" has entered the pit.\n>:";
    	clear();
    
    	//Display stats, setup and display items
    	player.show_stats();
    
    	//Begin of Game Loop
    	srand(time(NULL));
    	do
    	{
    		clear();
    		create_new_weapon(2, 7);
    		cin>>choice;
    	}while(player.hp >= 1);
    	return 0;
    }
    
    struct weapon create_new_weapon(int char_lvl, int mon_lvl)
    {
    	cout<<"A blacksmith forged you a weapon!"<<"\n"
    		<<"Sword, level "<<char_lvl + mon_lvl<<"\n";
    	weapon new_weapon;
    	new_weapon.name = "Sword";
    	new_weapon.min = 52;
    	new_weapon.max = 67;
    	new_weapon.bonus = 5;
    	return new_weapon;
    }
    What it is supposed to do is create a new weapon based on character and monster level, but it returns a miriad of errors-
    C2027: use of undefined type 'weapon'
    C2079: 'create_new_weapon' uses undefined struct 'weapon'
    C2079: 'new_weapon' uses undefined struct 'weapon'
    C2228: left of '.name' must have class/struct/union (for all of the things)
    What's wrong?

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    You don't declare structs or classes inside main(). You do it outside main(). Likewise, you don't declare that sad, lonely char outside main--you do it inside main().

    Next time when you have errors, make sure you do this:
    Code:
    weapon new_weapon;  //ERROR***ERROR****ERROR
    Last edited by 7stud; 01-21-2007 at 11:32 AM.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    struct weapon should be defined before you firstly use with name
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem getting structure to work in my function
    By Tom Bombadil in forum C Programming
    Replies: 18
    Last Post: 05-28-2009, 09:53 AM
  2. Replies: 5
    Last Post: 02-14-2006, 08:04 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. problem returning struct member from function
    By yukster in forum C Programming
    Replies: 6
    Last Post: 08-24-2003, 01:21 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21