Thread: overload struct in a constructor

  1. #1
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266

    overload struct in a constructor

    hey all, i tried this a couple of different ways, but can't make it work. is it possible to overload a private struct anywhere in the same class?

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    What do you mean by overload a private struct?

  3. #3
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    i was playing around trying to find out if an overloaded could be placed in a class to access a structure without passing it back from main in an array. (that didn't make any sense did it?). i'm working with the limited amount of knowledge i have right now. i'm sure there's a more praticle way of doing something to this effect, but i haven't learned it yet. did i use the right vocabulary, please corret me if i'm wrong.

    Code:
    #include <iostream.h>
    #define key 9
    #define name_max 20
    
    
    class c_stats
    {
    	protected:
    				struct plyr																//data struct
    				{	char name[name_max];
    					float fldng;
    					float b_aim;
    					int spd;
    					float thrwng;
    					float b_ave;
    					float p_ave;
    					float lck; 
    				};
    	public:
    				void mf_show (float x);
    				c_stats();
    };
    
    void c_stats::mf_show(float x)
    {
    	cout<<"a[0].b_aim: "<<x<<endl<<endl;
    }
    
    c_stats::c_stats()
    {
    ostream& operator<<(ostream& oput, plyr p[9])									//overload
    {
    	int index;
    	for(index=0;index<9;index++)
    	{
    		oput<<(p[index].name);
    		oput<<(p[index].fldng);
    		oput<<(p[index].b_aim);
    		oput<<(p[index].spd);
    		oput<<(p[index].thrwng);
    		oput<<(p[index].b_ave);
    		oput<<(p[index].p_ave);
    		oput<<(p[index].lck);
    	}
    	return(oput);
    }
    plyr a[9]={	{"t", .9, .2, 9, .9, .333, .100, .5},
    				{"r", .6, .05, 4, 1, .220, .500, .5},
    				{"a",1,1,1,1,1,1,1},
    				{"b",1,1,1,1,1,1,1},
    				{"c",1,1,1,1,1,1,1},
    				{"d",1,1,1,1,1,1,1},
    				{"e",1,1,1,1,1,1,1},
    				{"f",1,1,1,1,1,1,1},
    				{"g",1,1,1,1,1,1,1},
    			};
    
    main()
    {
    	
    	c_stats test;
    	test.mf_show();
    	return(0);
    }

  4. #4
    Registered User larry's Avatar
    Join Date
    Sep 2001
    Posts
    96
    If you don't want to pass a plyr type struct into the operator each time you call it, make an instance of plyr struct in the class:
    Code:
      class c_stats {
        private:
          struct plyr {
            // ...
          } m_player;
        public:
          friend ostream &operator<<(ostream&, c_stats&);
          // ...
      };
    ...and make an operator<< function a friend of c_stats class. This way you can define it explicitly and let it use private member m_player.

    Note: C doesn't allow nested functions, does it? You have the operator<< function in c_stats constructor?
    Please excuse my poor english...

  5. #5
    Unregistered
    Guest
    I have altered your code to something I think will work and be something like what you had intended.

    Code:
    --------------------------------------------------------------------------------
    #include <iostream.h>
    #define key 9
    #define name_max 20
    
    struct plyr	
    //data struct
    {	
      char name[name_max];
      float fldng;
      float b_aim;
      int spd;
      float thrwng;
      float b_ave;
      float p_ave;
      float lck; 
    };
    
    
    class c_stats
    {
      protected:
        plyr a[key];
      public:
        void mf_show (float x);
        c_stats();
        friend ostream & operator<<(ostream& oput, c_stats p);
    };
    
    void c_stats::mf_show(float x)
    {
    	cout<<"a[0].b_aim: "<<x<<endl<<endl;
    }
    
    c_stats::c_stats()
    {
        a[9]={	{"t", .9, .2, 9, .9, .333, .100, .5},
    	{"r", .6, .05, 4, 1, .220, .500, .5},
    	{"a",1,1,1,1,1,1,1},
    	{"b",1,1,1,1,1,1,1},
    	{"c",1,1,1,1,1,1,1},
    	{"d",1,1,1,1,1,1,1},
    	{"e",1,1,1,1,1,1,1},
    	{"f",1,1,1,1,1,1,1},
    	{"g",1,1,1,1,1,1,1}
                 }
    }
    
    ostream& operator<<(ostream& oput, c_stats p)		
    //display all members of the array a present in the c_stats p
    {
    	int index;
    	for(index=0; index<9; index++)
    	{
    		oput<<(p.a[index].name);
    		oput<<(p.a[index].fldng);
    		oput<<(p.a[index].b_aim);
    		oput<<(p.a[index].spd);
    		oput<<(p.a[index].thrwng);
    		oput<<(p.a[index].b_ave);
    		oput<<(p.a[index].p_ave);
    		oput<<(p.a[index].lck);
                                    cout << endl;
    	}
    	return(oput);
    }
    
    int main()
    {
    	
    	c_stats test;
                    cout << test;
    	test.mf_show(6.17);
    	return 0;
    }

  6. #6
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    wow, thanks for all your help i didn't know it could be that easy. i pluged in everything i was trying to get, but it's returning numbers that don't match, like the array is off or something. i changed the x= in the for loop and still got the exact same numbers. when i set the array sans loop, it repeats a single number (like you'd expect), but it's not the right bytes. any ideas?
    thanks again

    Code:
    #include <iostream.h>
    #define key 9
    #define name_max 20
    
    struct plyr	
    //data struct
    {	
      char name[name_max];
      float fldng;
      float b_aim;
      int spd;
      float thrwng;
      float b_ave;
      float p_ave;
      float lck; 
    };
    
    
    class c_stats
    {
      protected:
        plyr a[key];
      public:
        void mf_show ();
        c_stats();
        friend ostream & operator<<(ostream& oput, c_stats p);
    };
    
    void c_stats::mf_show()
    {
    	int x;
    	for(x=0;x<key;x++)
    	{
    	cout<<a[x].b_aim<<endl<<endl;
    	}
    };
    
    c_stats::c_stats()
    {
        plyr a[key]={
    			{"t", .9, .2, 9, .9, .333, .100, .5},
    			{"r", .6, .05, 4, 1, .220, .500, .5},
    			{"a",1,1,1,1,1,1,1},
    			{"b",1,1,1,1,1,1,1},
    			{"c",1,1,1,1,1,1,1},
    			{"d",1,1,1,1,1,1,1},
    			{"e",1,1,1,1,1,1,1},
    			{"f",1,1,1,1,1,1,1},
    			{"g",1,1,1,1,1,1,1}};
    };
    
    ostream& operator<<(ostream& oput, c_stats p)		
    //display all members of the array a present in the c_stats p
    {
    	int index;
    	for(index=0; index<9; index++)
    	{
    		oput<<(p.a[index].name);
    		oput<<(p.a[index].fldng);
    		oput<<(p.a[index].b_aim);
    		oput<<(p.a[index].spd);
    		oput<<(p.a[index].thrwng);
    		oput<<(p.a[index].b_ave);
    		oput<<(p.a[index].p_ave);
    		oput<<(p.a[index].lck);
                                    cout << endl;
    	}
    	return(oput);
    }
    
    main()
    {
    	
    	c_stats test;
        test.mf_show();
    
    return (0);
    }

  7. #7
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    hey, i'm still having problems with this. does someone mind taking a look?

  8. #8
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Your constructor is initialising a local array, not your protected plyr a[key]. Also you don't need semi-colons after your method definitions.

  9. #9
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    how would I intialize the protected array? i guess it may sound stupid, but actually what do i write in the constructor?

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    take a careful look at the constructor in unregistered's code versus yours. Then review the messages after to review a likely solution. Note the absence of the plyr term in unregistered whereas it is present in yours. The plyr term in yours means that the a[key] in the constructor is local to the constructor and is not the same a[key] declared as a protecterd variable in the class per se, even thought the two entities have the same name, the same description, etc. If you leave out the plyr term in the constructor then the a[key] is the protected member variable in the class and not a local plyr array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. Concatenating in linked list
    By drater in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 11:10 PM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. Help please im stuck
    By ItsMeHere in forum C Programming
    Replies: 7
    Last Post: 06-15-2006, 04:07 AM
  5. struct constructor woes
    By Mr_Jack in forum C++ Programming
    Replies: 2
    Last Post: 02-24-2004, 03:36 PM