i am trying to overload subtraction between two separate classes. Class Id is basically several strings such as the information of a voter database. Class Collect would be the collection of several class Id's in an array along with a total of Class Id's and the title of the collection (say "District 3")

My main statement would be along the lines of c1-id8;

I coded what i thought for sure was it. However apparently you cant if of type 'type' so i am stuck any help?

friend void operator-(Collect& c,const Id& b);
void operator-(Collect& c,const Id& b){
	 Id a;
	 for(int i=0;i<c.total;i++;){

What's the error message, and on which line does it occur?

1>c:\users\brandon\documents\visual studio 2008\projects\project two\project two\collect.cpp(29) : error C2451: conditional expression of type 'Id' is illegal


also i edited the code above accidentally wrote something wrong (changed c.m[0]=a to c.m=a)

Well, you've got a stray semicolon here:

for(int i=0;i<c.total;i++;){

The other thing is you can't just assign custom objects with the '=' operator. You either have to
a) overload the '=' operator so it copies all the object's data
b) use a pointer

i'm stuck could anyone help me start?

From what I understand, you're trying to overload the '-' operator so that you can remove any ID you want from a Collect object. There's several problems that you need to get through:

  • In your if() statement, you're trying to compare an ID from an array to another ID that you're trying to remove from the array. However, you can't just compare custom objects with '=='. It won't work; the compiler doesn't know what to compare. You have two options: either overload the '==' function for Id so that it compares individual data members to each other, OR compare using a unique identifier within ID. In other words, you'd have an id within the Id class.
  • Because arrays are big chunks of memory, you can't simply delete an element and expect its gap to be filled by the remaining members. Let's say you have an array of 50 IDs. Then let's say using this overloaded subtract function, you end up deleting ID #25. Do you see the problem? total will now be set at 49, but there's still technically 50 elements in your array. To fix this problem, you're either going to have to shift all the elements after the deleted element in the array over by 1 space, OR you'll have to use a type of storage method that doesn't have the limitations of an array (such as a linked list or a vector).
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.