2 Replies - 459 Views - Last Post: 14 April 2021 - 02:35 PM Rate Topic: ***-- 2 Votes

#1 alexisamedee   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-April 21

keep getting these errors "error: ‘size’ cannot be used as a funct

Posted 14 April 2021 - 12:22 PM

Im guessing its something simple that i forgot to include or something i included thats not required.

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <sstream>
#include <iomanip>

using namespace std;

string stos(const int set[])
{
    stringstream sout;
    sout << "{" << set[1];
    for (int i = 2; i <= set[0]; i++)
    {
        sout << "," << set[i];
    }
    sout << "}";
    return sout.str();
}

bool contains(const int set[], int elt)
{
    for (int i = 1; i <= set[0]; i++)
    {
        if (elt==set[i])
            return true;
            
    }
    return false;
}

void trimSet(int*& set)
{
    int* tmp = new int[set[0]+1];
    tmp[0] = set[0];
    for (int i = 1; i <= set[0]; i++)
    {
        tmp[i] = set[i];
    }
    delete[] set;
    set = tmp;
}

int* setAnd(const int set1[], const int set2[])
{
    int size;
    if (set1[0] < set2[0])
        size = set1[0];
    else
        size = set2[0];
    int* ans = new int[size + 1];
    int j = 1;
    for (int i = 1; i <= set1[0]; i++)
    {
        if (contains(set2, set1[i]))
        {
            ans[j] = set1[i];
            j++;
        }
    }
    ans[0]= j-1;
    trimSet(ans);
    return ans;
}

int* setOr(const int set1[], const int set2[])
{
    int size = set1[0] + set2[0];
    int* ans = new int[size + 1];
    int j = 1;
    for (int i = 1; i <= set1[0]; i++)
    {
        ans[j] = set1[i];
        j++;
    }
    for (int i = 1; i <= set2[0]; i++)
    {
        if (!contains(set1, set2[i]))
        {
            ans [j] = set2[i];
                j++;
                
        }
    }
    ans[0] = j - 1;
    trimSet(ans);
    return ans;
}
int* setDiff(const int set1[], const int set2[])
{
    int size = set1[0] + set2[0];
    int* ans = new int[size + 1];
    int j = 1;
    for (int i = 1; i <= set1[0]; i++)
    {
        if (!contains(set1, set2[i]))
        {
            {
            ans [j] = set2[i];
                j++;
                
        }
        }
    }
    ans[0] = j - 1;
    trimSet(ans);
    return ans;
}

int size(const int set[])
{
    return set[0];
}

int main()
{
    int size;
    cout << "Enter size and elements of Set A->";
    cin >> size;
    int* setA = new int[size + 1];
    setA[0] = size;
    for (int i= 1; i <= size; i++)
    {
        cin >> setA[i];
    }
    cout << "Enter size and elements of Set B ->";
    cin >> size;
    int* setB = new int[size + 1];
    setB[0] = size;
    for (int i= 1; i <= size; i++)
    {
        cin >> setB[i];
    }
    cout << "Enter size and elements of Set C ->";
    cin >> size;
    int* setC = new int[size + 1];
    setC[0] = size;
    for (int i= 1; i <= size; i++)
    {
        cin >> setC[i];
    }
    cout << "A =" << stos(setA) << endl;
    cout << "B =" << stos(setB) << endl;
    cout << "C =" << stos(setC) << endl;
    int* setAuB = setOr(setA, setB);
    cout << endl << "A u B =" << stos(setAuB) << endl;
    int* setAnB = setAnd(setA, setB);
    cout << endl << "A n B =" << stos(setAnB) << endl;
    int* setAdiffB = setDiff(setA, setB);
    int* setBdiffA = setDiff(setB, setA);
    cout << "A - B =" << stos(setAdiffB) << endl;
    int* setABuBA = setOr(setAdiffB, setBdiffA);
    cout << "(A - B)/> u (B - A) =" << stos(setABuBA) << endl;
    delete[] setAdiffB, setBdiffA, setABuBA;
    cout << endl << "|A u B| =" << size(setAuB) << endl;
    delete[] setAuB;
    cout << "|A| + |B| - |A n B| =" << size(setA) + size(setB) - size(setAnB) << endl;
    int* setBuC = setOr(setB, setC);
    int* setAnC = setAnd(setA, setC);
    cout << endl << "A n (B u C) =" << stos(setAnd(setA, setBuC)) << endl;
    delete[] setBuC;
    cout << "(A n B)/> u (A n C) =" << stos(setOr(setAnB, setAnC)) << endl;
    int* setAnBnC = setAnd(setAnB, setAnC);
    delete [] setAnB, setAnC;
    cout << endl << "A n B n C =" << stos(setAnBnC) << endl;
    cout << "Please enter an integer->";
    int testInt;
    cin >> testInt;
    cout << "Is" << testInt << "in A n B n C ?";
    if (contains(setAnBnC, testInt))
    {
        delete[] setAnBnC;
        cout << "TRUE" << endl;
    }
    else
    {
        delete[] setAnBnC;
        cout << "FALSE" << endl;
    }
}



:code:

i keep getting these errors (all part of the main function)
main.cpp: In function ‘int main()’:
main.cpp:163:47: error: ‘size’ cannot be used as a function
cout << endl << "|A u B| =" << size(setAuB) << endl;
^
main.cpp:165:49: error: ‘size’ cannot be used as a function
cout << "|A| + |B| - |A n B| =" << size(setA) + size(setB) - size(setAnB) << endl;
^
main.cpp:165:62: error: ‘size’ cannot be used as a function
cout << "|A| + |B| - |A n B| =" << size(setA) + size(setB) - size(setAnB) << endl;
^
main.cpp:165:77: error: ‘size’ cannot be used as a function
cout << "|A| + |B| - |A n B| =" << size(setA) + size(setB) - size(setAnB) << endl;
^

This post has been edited by modi123_1: 14 April 2021 - 01:14 PM
Reason for edit:: In the future please use the [code] tags in the editor.


Is This A Good Question/Topic? 0
  • +

Replies To: keep getting these errors "error: ‘size’ cannot be used as a funct

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 16201
  • View blog
  • Posts: 64,509
  • Joined: 12-June 08

Re: keep getting these errors "error: ‘size’ cannot be used as a funct

Posted 14 April 2021 - 01:16 PM

Perhaps give your function, or variable, a different name than 'size'.
Was This Post Helpful? 3
  • +
  • -

#3 jimblumberg   User is offline

  • member icon

Reputation: 5906
  • View blog
  • Posts: 17,911
  • Joined: 25-December 09

Re: keep getting these errors "error: ‘size’ cannot be used as a funct

Posted 14 April 2021 - 02:35 PM

Plus anther reason not to use "using namespace std;".

There is a standard function named size()
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1