Thread: C++ Class to add two boolean arrays or 1 boolean array with a int

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    2

    C++ Class to add two boolean arrays or 1 boolean array with a int

    Hi folks,

    I am trying to write a C++ class where i can add or subtract 2 seperate boolean arrays. If you look at my code, where I overload the first + operator, I get an error here because while performing the OR operation, I am trying to map a BOOL value into the class SETS value. So the compiler is asking for a SETS(BOOL) method. I'm pretty lost here and would sincerely appreciate any advise.

    #include <iostream.h>

    class sets
    {
    private:
    bool *set;
    int element;
    public:
    sets();
    sets(bool []);
    sets operator + (sets v)const;
    sets operator * (sets v)const;
    sets operator + (int f)const;
    friend istream& operator >> (istream& istr,sets& v);
    friend ostream& operator << (ostream& ostr,sets& v);
    };

    sets :: sets()
    {
    *set=0;
    }

    sets :: sets(bool x[])
    {
    set=x;
    element=(sizeof(x)/sizeof(bool));
    }
    sets sets :: operator + (sets v)const
    {
    for (int i=0,j=0;i<element,j<v.element;i++,j++)
    return sets(*(set+i) || *(v.set+j));
    }
    sets sets :: operator * (sets v)const
    {
    for (int i=0,j=0;i<element,j<v.element;i++,j++)
    return sets(*(set+i) && *(v.set+j));
    }
    sets sets :: operator + (int f)const
    {
    if (f<=element && f > 0)
    *(set+(f-1))=1;
    for (int i=0;i<element;i++)
    return sets(*(set+i));
    }
    ostream& operator << (ostream& ostr,sets& v)
    {
    for (int i=0;i<v.element;i++)
    {ostr << "{" << *(v.set+i) << "}";}
    return ostr;
    }

    int main()
    {
    sets s3[5];
    int e1=4;

    s3 = s3 + 5;
    cout << s3;
    }

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    I don't know if this is what you're looking for, but I've spotted a problem. The following code of yours is not doing what you think it is:
    *(set+i) || *(v.set+j)
    If you have a pointer to an object and you want to dereference it, you have to do things a bit differently.
    Either
    (*object).method();
    Or
    object->method();
    NOT
    *(object.method());
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Mmm...
    Code:
    sets :: sets(bool x[])
    {
    set=x; //probably a bad idea,
    // it would be better to allocate set and copy each element of x
    element=(sizeof(x)/sizeof(bool)); //don't think this will work,
    //instead make this constructor take the size of x as a parameter
    }
    That's at least one thing you should look at...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Adding Boolean Values????

    I' don't understand exactly what you're trying to do (and I'm week on the STL ) but you can't add or subtract boolean values. A boolean variable is either TRUE or FALSE. (Or, either ZERO or NON-ZERO.)

    Welcome, khush. Please the post regarding the use of Code Tags. And while you're at it, take a look at the Forum Guidelines.

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    2
    Thats what i figured...i'm probably on the right track. Right, this is what i'm trying to do and i hope u could provide some advise.

    1. Create a class that accepts boolean arrays such as
    a[5]={1,0,1,0,1} and b[5]={1,1,1,1,1}

    2. Overload the + operator to allow an addition operation between
    a[5] and b[5]. The addition operation is really an OR operation
    where 1 + 0 = 1

    I'd really appreciate any advise where to start from.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Replies: 26
    Last Post: 11-30-2007, 03:51 AM
  3. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  4. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 07:45 AM
  5. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 07:34 AM