Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with classes and structs... 2

Status
Not open for further replies.

jd1pek

Technical User
Apr 16, 2004
5
US
I need help on how to have access to my variables when inside of the ex_class.add() function.

struct ex_struct
{
char name[21];
int num;
....
};

class ex_class
{
public:
ex_class();
int display_all();
int add(const ex_struct &);

private:
ex_struct new_struct[101];
int keep_count;

};

ex_class::ex_class()
{
keep_count=0;
...
}

#### below is where i am not sure on what to do??
int ex_class::add(const ex_struct &)
{
// How would i have access to something declared in main

}

int main()
{
ex_class my_fav[101];
ex_class my_nightmare[101];
....
//eventually calling
my_nightmare.add(?????);
}

Any help would be greatly appreciated, think i should have taken better notes in class.

Thanks in advance.
jd1pek
 
These declarations:

Code:
  ex_class my_fav[101];
  ex_class my_nightmare[101];

are declaring arrays of classes, so "my_nightmare" is the array and "my_nightmare[0]" is one element of the array, in this case a class instance.

Your function definition also need to give a name to the paramater it's passing in. It should be declared like this:

Code:
int add(const ex_struct &my_ex_struct);

Dennis
 
ok, it is now making it to the following function:
int ex_class::add(const ex_struct &my_ex_struct)
{
...
}

but how would i add something to the array something like this:

// This would be inside of the add member function
strcpy(my_fav[keep_count].name,my_ex_struct.name);
my_fav[keep_count].num=my_ex_struct.num;
++keep_count;

 
This code compiles and runs by itself. Your add() code was basically correct, but you must use the name of the struct array as you called it inside the class declaration (new_struct instead of my_fav). If you want to make sure that my_fav is the instance of ex_class that gets "Rodney" added, you must call add with my_fav, like I did below.
Code:
#include <cstring>

struct ex_struct
{
  char name[21];
  int num;
};

class ex_class
{
  public:
    ex_class();
    int add(const ex_struct &);
  
  private:
    ex_struct new_struct[101];
    int keep_count;

};

ex_class::ex_class()
{
  keep_count=0;
}

int ex_class::add(const ex_struct &my_ex_struct)
{
  if (keep_count >= 101)
    return -1;

  strcpy(new_struct[keep_count].name,my_ex_struct.name);
  new_struct[keep_count].num=my_ex_struct.num;
  ++keep_count;

  return keep_count;
}

int main()
{
  ex_class my_fav;
  ex_class my_nightmare;

  ex_struct first_struct;
  strcpy(first_struct.name, "Rodney Buford");
  first_struct.num = 32;

  //call using my_fav
  my_fav.add(first_struct);
}
 
That looks like what i am trying to do but isn't new_struct private so i won't have access to it? I am going to try it.
 
I am still getting a segmentation fault when it trys to do to:
Code:
strcpy(new_struct[keep_count].name,my_ex_struct.name);
 
Ok, i messed up in my actual code i wasn't initializing the counter variable so it was garbage and wouldn't work. Thank you soooo much for all of your input you have solved my problem!

THANK YOU!
 
jd1pek said:
That looks like what i am trying to do but isn't new_struct private so i won't have access to it? I am going to try it.
Just to answer your question, new_struct is private, which is why you only have access to it inside member functions of the class. Since add() is a member function of the class, it has all the access it wants. That is the point of making member variables private and creating public member functions to do the work.


If you had left new_struct public, then in main() you might have accidentally manually added a new value to the array without incrementing keep_count. But because it is private, as long as your public add() function does the right thing, then anybody who uses your class can't mess it up.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top