Click to See Complete Forum and Search --> : C++ object problem


yoshi273
06-13-2002, 02:40 AM
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:


#include<iostream>
using namespace std;

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

void Init(TEST &obj);

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

int main()
{
TEST obj[5];
Init(obj);
cout << obj[1].A << 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!

Galen of Edgewood
06-13-2002, 09:18 AM
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.


#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. :)

yoshi273
06-14-2002, 02:24 AM
wow, now i feel stupid, lol. I could have sworn that i tried that already... oh well. Thanks for the help!

Galen of Edgewood
06-14-2002, 10:25 AM
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. :)