| | |
if statement involving array of a class (C2451)
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2008
Posts: 20
Reputation:
Solved Threads: 0
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?
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?
C++ Syntax (Toggle Plain Text)
friend void operator-(Collect& c,const Id& b);
C++ Syntax (Toggle Plain Text)
void operator-(Collect& c,const Id& b){ Id a; for(int i=0;i<c.total;i++;){ if(c.id[i]=b){ c.m[i]=a; c.total=c.total-1;} return;}
Last edited by blcase; Feb 9th, 2008 at 7:32 pm. Reason: c.m[0] should have been c.m[i]
Well, you've got a stray semicolon here:
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
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
Last edited by John A; Feb 9th, 2008 at 7:53 pm.
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
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?
totalwill 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).
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Compile time errors in C++ while generating random numbers
- Next Thread: execute operator() for elements of a vector
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






