C++ object problem

Sharky Forums


Results 1 to 4 of 4

Thread: C++ object problem

  1. #1
    Reef Shark
    Join Date
    Feb 2001
    Posts
    261

    C++ object problem

    I was re-writing a program to be more object-oriented and organized, and i came upon an interesting program. I write another separate program to isolate what i was having a problem with, but that didnt help me fix it. Maybe you guys might have an idea... here is what im trying to do:

    PHP Code:
    #include<iostream>
    using namespace std;

    class 
    TEST
         
    {
         public:
         
    char A;
         
    int B;
         };

    void Init(TEST &obj);

    void Init(TEST &obj)
         {
         
    obj[1].'a';
         
    obj[1].1;
         }

    int main()
         {
         
    TEST obj[5];
         
    Init(obj);
         
    cout << obj[1].<< endl;
         return 
    0;
         } 
    I want to be able to declare an array of objects, like maybe 100 of them. I want to pass that entire array to an Init() function that will initialize SOME of those objects, then return them to main. Not sure why it wont work =( Thanks!

  2. #2
    Texan Dragon Moderator Galen of Edgewood's Avatar
    Join Date
    Sep 2000
    Location
    Ft Irwin, CA
    Posts
    5,602
    Silly yoshi. You left out a couple of major things. ---> [] These.

    I reformatted your code to the way I code (sorry, easier for me to read) and it compiles now.

    Code:
    #include <iostream>
    
    using namespace std;
    
    class TEST{
    
    public:
    	char A;
    	int B;
    };
    
    void Init(TEST obj[]);
    
    void main(){
    	TEST obj[5];
    	Init(obj);
    	cout << obj[1].A << endl;
    }
    
    void Init(TEST obj[]){
    	obj[1].A = 'a';
    	obj[1].B = 1;
    }
    Changes that I made:
    ***Init(TEST obj[])***

    To send an array into a function, you need to tell C++ that it's an array. You basically were confusing the compiler before that.
    Dragon of the OC Crusaders

    Break the rules and you're snack food for this dragon...

  3. #3
    Reef Shark
    Join Date
    Feb 2001
    Posts
    261
    wow, now i feel stupid, lol. I could have sworn that i tried that already... oh well. Thanks for the help!

  4. #4
    Texan Dragon Moderator Galen of Edgewood's Avatar
    Join Date
    Sep 2000
    Location
    Ft Irwin, CA
    Posts
    5,602
    Originally posted by yoshi273
    wow, now i feel stupid, lol. I could have sworn that i tried that already... oh well. Thanks for the help!
    LOL...

    Don't worry 'bout that. It's the silly little stuff that will kill you in no time flat.
    Dragon of the OC Crusaders

    Break the rules and you're snack food for this dragon...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •