Thread: An Array of Classes

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    6

    An Array of Classes

    I need to create an array of classes; something like; I have a class named calculate, and I need to create an array of the class like:

    claculate array[1000];

    is that possible? I tried this...and it compiles alright....but when I run the program...it gives a segmentation fault. Please suggest how I can do this!!


    Thanks

    Shrestha

    Here is a snippet of what I need to do:



    Code:
    calculate operator *(calculate big1, calculate big2)
    	{
    	calculate ans[1000];
    
    	int temp=0;
    	int temp1=0;
    	int temp2=0;
    	for (int i=1; i<big2.count+1;i++)
    	   {
    		for(int y=big1.count; y>0; y--)
    		{
    		   temp1=big1.array1[y]*big2.array1[i]+temp;
    		   temp=0;
    		   if (temp1>9)
    			{
    			   temp=temp1/10;
    			   temp1=temp1-temp*10;
    			}
    		   ans[i].array1[y]=temp1;
    		}
    	   ans[i].count=big1.count;
    	
    	   if (temp != 0)
    		{
    		  for (int j=ans[i].count+1; j>0; j--)
    			{
    			ans[i].array1[j]=ans[i].array1[j-1];
    			}
    		  ans[i].array1[1]=temp;
    
    		ans[i].count++;
    		}
    
    //This is the part where I'm not so sure
    
    	   for (int I=1; I<(big2.count-i+1); I++)
    		{
    		  ans[i].array1[ans[i].count+I]=0;
    		}
    	   ans[i].count=ans[i].count+(big2.count-i);
    
    
    ans[0]=ans[0]+ans[i];
    	   }
    
    
    return (ans[0]);
    	}
    Code tags added by Kermi3

  2. #2
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595

    Code\

    In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happy about helping you


    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [ code ] at the beginning of your code and [ /code ] at the end, only without the spaces. More information on code tags may be found at the link on my signature.
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    yes, you can make an array of user defined class objects.
    Code:
    class calculations
    {
       //whatever;
    };
    
    int main()
    {
      calculations ans[1000];
      //etc.
    }
    but there are several techincal hitches. First an array of any size at all, particularly if the objects are of any significant size, will eat up your stack space, so it may be best to use the free store instead. Second, the compiler will use the default constructor when creating each calculations declared in the array, so you have to have a default constructor available--either by using the default default constructor the compiler will provide if you don't declare any other constructors, or, preferably by declaring and implementing your own default constructor.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    It's hard to say, not knowing the details of the calculate class. But look at this:

    for (int i=1; i<big2.count+1;i++)

    If big2.count is how many items in an array, then you will surely overwrite memory by going to big2.count+1. Look at an array:

    int array[1000];

    The first index is '0', not '1'. And the last index is '999', not '1000'.

    The rule of thumb for all arrays is:

    "Don't access higher than count - 1."

    So change these to:

    for (int i = 0; i < (big2.count - 1); i++)

    for(int y = (big1.count - 1); y > 0; y--)


    My next advice is to build up the function slowly, adding more code little by little. Once you verify that the current snippet works, add more code. This will make debugging easier.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    6

    Wink An Array of Classes

    Thanks for the reply...I found out the error.....well...I just decreased the size of the array to 100 instead of 1000 and it worked!! Thanks for all the help though!!!!!


    PEACE!!!
    Manish

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    sounds like you were filling up the variable stack with the array of size 1000. If you want to use that size of an array, then you will need to reset the size of the variable stack, or more commonly, use the free store/heap/dynamic memory to store all the variables.

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    1
    You might also be interested in using the stl containers such as <vector>.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >You might also be interested in using the stl containers such as <vector>.
    Yes, if you have the option (ie. You're not doing homework), then STL containers are preferable to anything hand coded.

    -Prelude
    My best code is written with the delete key.

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708

    Yes, if you have the option (ie. You're not doing homework), then STL containers are preferable to anything hand coded.

    bah!
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  10. #10
    Registered User
    Join Date
    Feb 2002
    Posts
    589
    You also might want to look into the MS helper class CObjectArray on the mdsn helpfiles. It is wery nice and easy to work with.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading Array Objects for use with Classes
    By ibleedart in forum C++ Programming
    Replies: 2
    Last Post: 10-24-2007, 06:48 PM
  2. Modify an single passed array element
    By swgh in forum C Programming
    Replies: 3
    Last Post: 08-04-2007, 08:58 AM
  3. Char array and classes
    By Heineken in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2005, 12:32 PM
  4. two-dimensional dynamic array of pointers to classes
    By Timo002 in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 06:18 AM
  5. array of classes in C++
    By stanleyw in forum C++ Programming
    Replies: 2
    Last Post: 05-29-2002, 08:20 PM