operator overloading slight problem

This is a discussion on operator overloading slight problem within the C++ Programming forums, part of the General Programming Boards category; got this assignment to write a class for sets and provide various member functions e.g add code, set union. each ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    4

    operator overloading slight problem

    got this assignment to write a class for sets and provide various member functions e.g add code, set union. each set has a certain number of codes. each code is a 5 charater c-string. i cant seem to get the set union (below) working.
    Code:
    class set
    {
    public:
    	set();
    	set operator+( const set& u ) const; //this operator is the set_union() function
    	
    private:
        char (*codes)[string_length];               //variable codes is a pointer to an array of characters "5"               
    	int num_codes;                              //the memory for the number of codes and the 5 character string 
    };//end
    
    set set::operator+( const set& u ) const
    {
        int i, j = 0;
        set union_set;
        union_set.num_codes = num_codes + u.num_codes;	// e.g num_codes = 2 and u.num_codes = 1, union_set.num_codes = 3
        union_set.codes = new char[union_set.num_codes][string_length]; //allocate memory needed
        for (i < 0; i < num_codes; i++)
        {
            for (j = 0; j < string_length; j++)	//codes from a will go into union_set.codes e.g union_set.codes[0][2] = codes[0][2]
    	    {
                union_set.codes[i][j] = codes[i][j];
            }
        }
        for (i = num_codes; i < union_set.num_codes; i++)	//codes from a will go into union_set.codes e.g union_set.codes[2][2] = u.codes[0][2]
        {
            for (j = 0; j < string_length; j++)
    	    {
                union_set.codes[i][j] = u.codes[i-num_codes][j];	//u.codes[i-num_codes] means u.codes starts at 0
            }
        }   
        return union_set;
    }
    
    void main()
    {
    set a, b, un;
    un = a + b;
    }
    the codes from a (codes) and b (u.codes) dont seem to get stored in union_set.
    anyhelp would be grateful as assignment is due in soon, thanks
    Last edited by reddzer; 02-23-2006 at 03:33 PM.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > for (i < 0; i < num_codes; i++)
    Look closely at your for-loop initial condition:
    Code:
        for (i < 0; i < num_codes; i++)
    > for (i < 0; i < union_set.num_codes; i++)

    And did you ever store anything in your set?

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    4
    yeah it was the for loop. stupid obvious error. probably should have gotten up and walked away from the computer. 4 hours straight of working on the rest of it has hindered my concentration.
    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Slight problem with socket reading!!!
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 02-15-2006, 08:55 AM
  2. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  3. Subclassing edit control - slight problem
    By Shag in forum Windows Programming
    Replies: 3
    Last Post: 11-02-2002, 11:33 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21