Hi again guys,
Im trying to declare SIZE as a constant but i keep getting an error saying C++ forbids declaration of SIZE with no type. Does anybody know how to fix this. it was also giving me an error when i was using the void main command so i switched to int main.

#include<iostream>
using std::cout;
using std::endl;

const SIZE= 101;//0 to 100 set containing 101 elements
class IntegerSet


    {
    		int a[SIZE];
    	public:
    		IntegerSet();
    		IntegerSet(int *, int);
    		IntegerSet unionOfSets(IntegerSet s2);
    		IntegerSet intersectionOfSets(IntegerSet s2);
    		int isEqualTo(IntegerSet s2);
    		void insertElement(int n);
    		void printSet();
};

IntegerSet::IntegerSet()//default constructor creates empty set


    {
    	int i;
    	for(i=0;i<SIZE;i++)
    		a[i]=0;
}

IntegerSet::IntegerSet(int *ptr, int n)//initialize object using another array of integers


    {
    	int i;
    	for(i=0;i<SIZE;i++)//Initialize the set first
    		a[i]=0;
    	for(i=0;i<n;i++)
    		a[(ptr[i])]=1;
}

IntegerSet IntegerSet::unionOfSets(IntegerSet s2)


    {
    	 IntegerSet s3;
    	 int i;
    	 for(i=0;i<SIZE;i++)
    		if(a[i]==1 || s2.a[i]==1)
    			s3.a[i]=1;
    	 return s3;
}

IntegerSet IntegerSet::intersectionOfSets(IntegerSet s2)


    {
    	 IntegerSet s3;
    	 int i;
    	 for(i=0;i<SIZE;i++)
    		if(a[i]==1 && s2.a[i]==1)
    			s3.a[i]=1;
    	 return s3;
}

int IntegerSet::isEqualTo(IntegerSet s2)


    {
    	int i;
    	int check=1;//equal
    	for(i=0;i<SIZE;i++)
    	 if(a[i]!=s2.a[i])


        	 {
        		check=0;//not equal
        		break;
        	 }
        	return check;
    }

    void IntegerSet::insertElement(int n)


        {
        	if (n<SIZE && n>=0) //check validity
        		a[n]=1;
    }

    void IntegerSet::printSet()


        {
        	int i, count=0;
        	for(i=0;i<SIZE;i++)
        		if(a[i]==1)


            		{
            			count++;
            			cout<<i<<" ";
            		}
            	if(count==0)
            		cout<<"---";
        }

        int main()
 {
            	int arr[4]={3,76,34,56};
            	IntegerSet s1, s2(arr, 4), s3, s4;
            	s4.insertElement(34);
            	s4.insertElement(56);
            	cout<<"\nDefault set=";
            	s1.printSet();
            	cout<<"\nUsing array as parameter, set s2= ";
            	s2.printSet();
            	cout<<endl;
            	cout<<"Inserting 50, 100,1,0,34, 56 to s1, set s1=";
            	s1.insertElement(50);
            	s1.insertElement(100);
            	s1.insertElement(1);
            	s1.insertElement(0);
            	s1.insertElement(34);
            	s1.insertElement(56);
            	s1.printSet();
            	cout<<endl;
            	cout<<"Union of s1 and s2, set s3=";
            	s3=s1.unionOfSets(s2);
            	s3.printSet();
            	cout<<endl;
            	cout<<"Intersection of s1 and s2, set s3=";
            	s3=s1.intersectionOfSets(s2);
            	s3.printSet();
            	cout<<endl;
            	cout<<"set s4=";
            	s4.printSet();
            	cout<<endl;
            	cout<<"s2==s3=";
            	if(s2.isEqualTo(s3)==1)
            		cout<<"True";
            	else
            		cout<<"False";
            	cout<<"\ns3==s4=";
            	if(s3.isEqualTo(s4)==1)
            		cout<<"True";
            	else
            		cout<<"False";
            		
            	system("pause");
            		return 0;	
        }

A constant is not a type. It just tells the program not to modify that type for example you could have an integer const or a string const or a char const or any other type. Looks like you want an integer constant. I would suggest the following

int const SIZE= 101;//0 to 100 set containing 101 elements

Just to note, this sort of thing is better done with a macro

#define SIZE 101

Chris

Some notes about your class design:
1. It's extremely ineffective way to pass huge size arguments by value. Use references:

...
IntegerSet unionOfSets(const IntegerSet& s2) const;
...

Don't forget to declare const where it's a semantically possible solution.
2. You write a code in C++. Use bool type:

bool isEqualTo(const IntegerSet& s2) const;

3. It seems IntegerSet(int *, int) constructor is wrong. At least it's a very dangerous function. What happens if an argument array contains integers >= SIZE?.. Or negative values?..
4. If your set contains integers in 0..SIZE-1 range, declare minimal size base element type for a set array (char, for example, or bool)...
5. Look at std::set class interface. Think about a proper set of useful operations...

SIZE might be a defined macro for something, so try to undefine it--

#include<iostream>
using std::cout;
using std::endl;

#ifdef SIZE
#undef SIZE
#endif

const SIZE= 101;//0 to 100 set containing 101 elements
// ...

EDIT: Scratch that. You forgot to declare a storage type for SIZE. It's const, but const what?

int?
unsigned int?
double?
etc?


I'd suggest unsigned int since your size will never be negative--

#include<iostream>
using std::cout;
using std::endl;

const unsigned int SIZE= 101;//0 to 100 set containing 101 elements
// ...

-- some implementations of C++ will allow you to declare an identifier without a storage type in which it will automatically make it an int if there's an assignment. That confused the hell out of me when I used a really old compiler and the Dev IDE when first learning C++.

It's wrong to make something an assumed type. Take better control of your program and be explicit ( yet concise ) about everything possible.

Just to note, this sort of thing is better done with a macro

#define SIZE 101

Chris

Disagree. This sort of thing is sometimes done with help of a macro. That doesn't make it better.

Macros are not type safe, cannot be checked by the compiler, disrespect scope, can be broken by other macros, ...... but, apart from trifling concerns like those, I suppose one can argue they are a better alternative.

In some compilers (like VSC++) int is implied when only the const keyword is used (like you did). It is always safest to issue a datatype, however, since many compilers require it.

In some compilers (like VSC++) int is implied when only the const keyword is used (like you did). It is always safest to issue a datatype, however, since many compilers require it.

More significantly, the C++ and latest C standards also require it.

The "implicit int" rule was supported by K&R C. I'd have to check the actual standard to be sure, but believe it was deprecated in the 1989 C standard. It is definitely disallowed in both the C++ and 1999 C standards.

The only modern compilers that support "implicit int" are those that are seeking to maintain backward compatibility to very old code (at least 10 years old). Fewer compilers are doing that as time marches on. Those compilers will work if the "datatype" is provided, as const is a more recent innovation than declarations. Accordingly, there is never any practical reason in new code (or code that is newly modified) to avoid the "datatype".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.