Errors

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Latina

    #1

    Errors

    Hi I am doing a program oveloraded operator.
    I am having a few errors on it.

    Error1: request for member `insertElement' in `S1set', which is of
    non-
    class type `IntegerSet[26]'

    Error2: no matching function for call to `IntegerSet::op erator+
    (IntegerSet[26])'

    Here is my program:


    class IntegerSet
    {
    private:
    bool set[26];
    int element;

    public:
    //Operator methods.
    IntegerSet operator + (const IntegerSet &)const;//method union

    //Methods
    IntegerSet(); //default constructor
    IntegerSet(int x[], int k); //overload constructor

    bool isValid(int)con st;
    void insertElement(i nt);
    void deleteElement(i nt);
    void setString();
    void inputSet();
    };


    IntegerSet set();

    IntegerSet::Int egerSet()
    {
    for(element=0; element>=25; element++)
    set[element]= false;
    }

    IntegerSet::Int egerSet(int x[], int k)
    {
    for(element=0; element>=25; element++)
    set[element]= false;
    for(int j=0; j<k; j++)
    {
    element=x[j];
    set[element]= true;
    }
    }

    bool IntegerSet::isV alid(int i)const
    {
    return set[i];
    }

    //insert element to a set
    void IntegerSet::ins ertElement(int element)
    {
    set[element]=true;


    }

    //delete element of a set
    void IntegerSet::del eteElement(int element)
    {
    set[element]=false;


    }

    //overloaded operator + to compute the union of two sets
    IntegerSet IntegerSet::ope rator+(const IntegerSet &right)const
    {
    IntegerSet j;

    for(int element=0; element<=25; element++)
    {
    if(isValid(elem ent) || right.isValid(e lement))
    j.insertElement (element);
    }
    return j;
    }


    int main()
    {
    IntegerSet run;
    IntegerSet S1set[26];
    IntegerSet S2set[26];
    IntegerSet S3set[26];
    IntegerSet Sset[26];

    for(int i=2; i<=20; i+2)
    S1set.insertEle ment(); <--Error 1

    for(int k=6; k<=21; k+3)
    S2set.insertEle ment(); <--Error 1

    for(int j=3; j<=18; j+6)
    S3set.insertEle ment(); <--Error 1

    for(int z=0; z<=25; z++)
    Sset.insertElem ent(); <--Error 1

    run.inputSet();
    int choice;

    cout<<"\n WELCOME to the INTEGER SET PROGRAM\n";
    cout<<"\n\nSele ct one of these choices\n";
    cout<<" 0. Create set \n";
    cout<<" 1. Find Union \n";
    cin>>choice;

    if(choice==0)
    {
    int temp, ele;
    int newSet[26];

    cout<<"Enter how many elements you want in the set: "<<endl;
    cin>>ele;

    for(int i=0; i<ele; i++)
    {
    cout<<i+1;
    cin>>temp;
    newSet[i]=temp;
    }
    }
    else if(choice==1)
    {
    char a, b, c, d, e,;
    int option;

    cout<<"Select one of this choices";
    cout<<"a. To find the union of the set you enter and the set
    'S'";
    cout<<"b. To find the union of the set you enter and the set
    'S1'";
    cout<<"c. To find the union of the set you enter and the set
    'S2'";
    cout<<"d. To find the union of the set you enter and the set
    'S3'";
    cin>>option;
    if(option=='a'| |option=='A')
    {
    run.operator+(S set); <--Error 2
    }
    else if(option=='b'| |option=='B')
    {
    run.operator+(S 1set); <--Error 2
    }
    else if(option=='c'| |option=='C')
    {
    run.operator+(S 2set); <--Error 2
    }
    else if(option=='d'| |option=='D')
    {
    run.operator+(S 3set); <--Error 2
    }
    }

    return 0;
    }


    I hope some one can help me.

  • Tadeusz B. Kopec

    #2
    Re: Errors

    On Tue, 13 Nov 2007 09:35:27 +0000, James Kanze wrote:
    Tadeusz B. Kopec wrote:
    >On Sun, 11 Nov 2007 21:46:46 -0800, Latina wrote:
    >
    Hi I am doing a program oveloraded operator. I am having a few errors
    on it.
    class IntegerSet
    {
    private:
    bool set[26];
    [snip]
    >Second - it's wasting space. Use std::vector<boo lor if you want a
    >fixed size - std::bitset.
    >
    As has often been pointed out, std::vector<boo lis broken, and should
    be avoided. And it's likely to take up just as much, or more space than
    the original code. (On my machine, I get sizeof(bool) == 1,
    sizeof(std::vec tor<bool>) == 40. And that doesn't count the memory
    dynamically allocated by std::vector.)
    OK. I thought that vector<boolnot being a container isn't a problem for
    the use that is made in this class, but anyway it sacrifices speed for
    (probable) space gain and it's a premature optimisation (if it is an
    optimisation).

    [snip]
    IntegerSet::Int egerSet()
    {
    for(element=0; element>=25; element++)
    set[element]= false;
    }
    >The only thing this function does is assigning 0 to element.
    >
    It initializes the set to empty. Seems like a reasonable thing to do
    for a default constructor. (Of course, using std::fill would be more
    idiomatic. But his code seems quite reasonable.)
    It would do this, if the loop condition was 'element <= 25'. As it is,
    only the assignment to element will be executed. And of course using a
    member as a loop controlling variable is a bad idea.
    --
    Tadeusz B. Kopec (tkopec@NOSPAMP LEASElife.pl)
    BOFH excuse #25:

    Decreasing electron flux

    Comment

    • James Kanze

      #3
      Re: Errors

      On Nov 13, 9:19 pm, "Tadeusz B. Kopec" <tko...@NOSPAMP LEASElife.pl>
      wrote:
      On Tue, 13 Nov 2007 09:35:27 +0000, James Kanze wrote:
      Tadeusz B. Kopec wrote:
      On Sun, 11 Nov 2007 21:46:46 -0800, Latina wrote:
      Hi I am doing a program oveloraded operator. I am having a few errors
      on it.
      class IntegerSet
      {
      private:
      bool set[26];
      [snip]
      Second - it's wasting space. Use std::vector<boo lor if you want a
      fixed size - std::bitset.
      As has often been pointed out, std::vector<boo lis broken, and should
      be avoided. And it's likely to take up just as much, or more space than
      the original code. (On my machine, I get sizeof(bool) == 1,
      sizeof(std::vec tor<bool>) == 40. And that doesn't count the memory
      dynamically allocated by std::vector.)
      OK. I thought that vector<boolnot being a container isn't a problem for
      the use that is made in this class, but anyway it sacrifices speed for
      (probable) space gain and it's a premature optimisation (if it is an
      optimisation).
      I don't think it would be an actual problem, but as a matter of
      principle, I think it better to avoid vector<bool>, because it
      looks like a container, even if it isn't one. Learning all its
      quirks, to be sure of what you are doing, is probably more work
      than just using the C style array (in this case, at least).
      [snip]
      IntegerSet::Int egerSet()
      {
      for(element=0; element>=25; element++)
      set[element]= false;
      }
      The only thing this function does is assigning 0 to element.
      It initializes the set to empty. Seems like a reasonable thing to do
      for a default constructor. (Of course, using std::fill would be more
      idiomatic. But his code seems quite reasonable.)
      It would do this, if the loop condition was 'element <= 25'.
      Oops. But that's obviously a typo.
      As it is, only the assignment to element will be executed. And
      of course using a member as a loop controlling variable is a
      bad idea.
      The overall structure of the code wasn't particularly good,
      agreed. As I said, I'd use std::fill here. But the principle
      of a loop isn't necessarily wrong, even if he miswrote it, and
      didn't do it very cleanly.

      --
      James Kanze (GABI Software) email:james.kan ze@gmail.com
      Conseils en informatique orientée objet/
      Beratung in objektorientier ter Datenverarbeitu ng
      9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

      Comment

      Working...