-2

last night my question on vector pairing was answered :)

However, I have a new problem. In my case, I have a toString() method declared in my class as follows.

string toString() {
    stringstream info;
    info << "Name   : " << getName() << "\nSubject: " << getSubject() << "\nResult : " << getGrade() << endl << endl;
    return info.str();
}

Next, I have a vector and printing code as follow:

vector<pair<Student*, string>> Students;
//...
Students.push_back(make_pair(Abbie, Abbie->getGrade(80)));
//...
for (int i = 0; i < Students.size(); i++)
{
    cout << Students[i]->toString() << endl;
}

Apparently the grade is not being captured, and is not being printed.

Am I not supposed to cout this way if I have a toString()? If it is not, may I know how am I suppose to do it?

Anyone can help? :)

Edit: I have found my source of error - I assigned values using '==' instead of '='. But nevertheless, thank you all for trying to help

Timothy Wong
  • 689
  • 3
  • 9
  • 28
  • 2
    Is `getGrade()` returning what it's support to? Is the data stored in `*Abbie` correct? There's more relevant code that we're not seeing. – Captain Obvlious Jan 12 '16 at 05:07
  • yes it is. I am suspecting that this is not the way I am supposed to cout. That's why I am posting the question... – Timothy Wong Jan 12 '16 at 05:16
  • 4
    Well you are missing the "first" when you try to cout, to start pointing out something. `cout << Students[i].first->toString() << endl;` I would be surprised if you tell me that this code compiles. – Nacho Jan 12 '16 at 05:17
  • I did try 'cout << Students[i].first->toString() << Students[i].second->toString();' But I received a pointer type error. I am guessing that I only have two elements in first, and one conflicting element in second. – Timothy Wong Jan 12 '16 at 05:29
  • What do you actually want to do? print the Student::toString() method or the grade? As Ignacio mentioned to need to reference one of the items in the pair using .first or .second. – Dylan James McGannon Jan 12 '16 at 05:29
  • I am trying to print Students[i] with toString() @Dylan James – Timothy Wong Jan 12 '16 at 05:30
  • 3
    The first element is a pointer, the second element isn't so try 'cout << Students[i].first->toString() << Students[i].second << endl;' – Dylan James McGannon Jan 12 '16 at 05:30
  • When you say "the grade is not being captured, and is not being printed", are you implying that the name and subject *are* being printed? **Try something simpler.** Try printing the information from one Student. Only when that works perfectly should you try to print the information from a pair, and only when that works perfectly should you print the information from a vector of pairs. – Beta Jan 12 '16 at 05:31
  • Anyway, I tried Dylan's method 'cout << Students[i].first->toString() << Students[i].second << endl;' Name and subject (elements of the student object) are printed, but not the grade. – Timothy Wong Jan 12 '16 at 05:36
  • The problem here is that we are proposing solutions to something that is badly designed to begin with. If the `cout << Students[i].second` doesn't print anything is because there is nothing there. Please review how are you initializing that variable and edit your question to show us. I would bet the problem is there. – Nacho Jan 12 '16 at 12:58

2 Answers2

1

I guess

Abbie->getGrade(80)

is just converting int to string, not assigning any data to the object.If so, at here:

 Students.push_back(make_pair(Abbie,Abbie->getGrade(80)));

your "Grade" is stored at pair.second only, but not in Abbie itself, so printing class information does not print the value of

Abbie->getGrade(80)
ggrr
  • 7,737
  • 5
  • 31
  • 53
0

If the toString() method is defined as you mentioned then the Student class must have the members "name, subject and grade" so the class should look something like:

class Student
{
public:
  Student(std::string name_, std::string subject_, std::string grade_) // Let's just use string everywhere to keep the example simple
    : name(name_)
    , subject(subject_)
    , grade(grade_)
  {}

  std::string getName() { return name; }
  std::string getSubject() { return subject; }
  std::string getGrade() { return grade; }

  std::string toString();

private:
  std::string name;
  std::string subject; 
  std::string grade;
}

Now when you create "Abbie" you should do so with all the information needed:

Student * Abbie = new Student("Abbie", "Math", "80");

The rest of you code should work now, but I would like to point out a couple of things:

You really don't need a vector of pairs if the Student class has a method that returns all of its members:

vector<Student*> Students;

Students.push_back(Abbie);
for (int i = 0; i < Students.size(); i++)
{
    cout << Students[i]->toString() << endl; // Now you can use this without the "first" I mentioned in a comment up there
}

One more thing, the toString() method belongs to the Student class, so it has access to his own members, so no need to call the getters to obtain the values:

std::string toString() 
{
  stringstream info;
  info << "Name   : " << name << "\nSubject: " << subject << "\nResult : " << grade << endl << endl;
  return info.str();
}

NOTE: However this should work and print everything correct using the toString(), the title of your question was "Printing a pair from a vector". If this is really what you need explained you should do it as Dylan James McGannon told you in a comment.

Nacho
  • 1,104
  • 1
  • 13
  • 30