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.
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.
If you're trying out not equal make sure to return false.
Subreddit
Post Details
- Posted
- 4 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/cpp_questio...
If you're trying out not equal make sure to return false.