Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

2
Help with == Overload
Post Flair (click to view more posts with a particular flair)
Post Body

I have some confusions about my program. Basically I've created Student to use elsewhere, but when I use it and check Valgrind, I get errors with my == overload. saying a conditional jump depends on a uninitialized value. Values are added correctly, as I can list them and delete them from elsewhere. I've deleted bits of code, like some of my getters and setters that I figured wouldn't be helpful. Now I'm trying to just figure out what the issue with this == operator is. Since it successfull prints with the << overload.

#include <iostream>
#include <fstream>
#include <string>
#include "Student.h"
using namespace std;
Student::Student() {
    studentID = 0;
}

Student::~Student() {

}

int Student :: getStudentID() {
    return studentID;
}

bool Student::operator==(const Student& rhs) const {
        if(this->studentID == rhs.studentID) {
            return true;
        }
    return false;
}
std::ostream& operator<< (std::ostream &out, const Student &student) {
    out << student.studentID <<" " << student.studentName;
    return out;
}
std::istream& operator>> (istream& is, Student& student) {
    is >> student.studentID >> student.studentName;
    return is;
}

My Student.h

#ifndef STUDENT_H

#define STUDENT_H

#include <string>

using namespace std;

class Student

{

public:

    Student();

    int getStudentID();

    void setStudentID(int);

    void setStudentName(string);

    string getStudentName();

    bool operator==(const Student& rhs) const;

    friend std::ostream& operator<< (std::ostream &out, const Student &student);

    friend istream& operator>> (istream& is, Student& student);

    int studentID;

    string studentName;

};

#endif

Valgrind Error

    ==46473== Conditional jump or move depends on uninitialised value(s)
    ==46473==    at 0x4016F2: Student::operator==(Student const&) const (Student.cpp:25)
    ==46473==    by 0x403671: UnsortedList<Student>::findIndex(Student) const (UnsortedList.cpp:79)
    ==46473==    by 0x403158: UnsortedList<Student>::putItem(Student) (UnsortedList.cpp:39)
    ==46473==    by 0x402003: testStudentList() (UnsortedListDriver.cpp:162)
    ==46473==    by 0x4017DA: main (UnsortedListDriver.cpp:24)
    ==46473==
    ==46473== Conditional jump or move depends on uninitialised value(s)
    ==46473==    at 0x4016F2: Student::operator==(Student const&) const (Student.cpp:25)
    ==46473==    by 0x403671: UnsortedList<Student>::findIndex(Student) const (UnsortedList.cpp:79)
    ==46473==    by 0x403237: UnsortedList<Student>::deleteItem(Student) (UnsortedList.cpp:50)
    ==46473==    by 0x402091: testStudentList() (UnsortedListDriver.cpp:169)
    ==46473==    by 0x4017DA: main (UnsortedListDriver.cpp:24)

UnsortedList findIndex -

template <class ItemType>
int UnsortedList<ItemType> :: findIndex(ItemType item) const{
    for (int i = length; i >= 0; i--) {
        if(info[i] == item) {
            return i;
        }
    }
    return -1;
}

UnsortedList putItem calls the findIndex and if it returns -1, it will throw an error saying there is a duplicate.

A test example would be the following

PutItem 5325 divergindog

PutItem 2353 Gabe

When the second putitem is called, it should compare the ids, 5325 and 2353 to see if they are the same.

Comments
[not loaded or deleted]

If you're trying out not equal make sure to return false.

[not loaded or deleted]

If you're trying out not equal make sure to return false.

Author
Account Strength
100%
Account Age
7 years
Verified Email
Yes
Verified Flair
No
Total Karma
10,390
Link Karma
4,704
Comment Karma
5,182
Profile updated: 1 day ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
4 years ago