0

First of all I'm pretty new to vectors themself, so bear with me please.

I'm trying to make a vector that contains 2strings, int and a float.

This is my struct:

struct OpiskelijanTiedot {

   string etunimi;
   string sukunimi;
   int HarjMaara;
   float Arvosana;

   OpiskelijanTiedot() : etunimi(), sukunimi(), HarjMaara(), Arvosana() {}
   OpiskelijanTiedot(string const& e, string const& s, int const& h, float const& a) :
         etunimi(e), sukunimi(s), HarjMaara(h), Arvosana(a) {}
   bool operator<(OpiskelijanTiedot const& rhs) const
   {
      return sukunimi < rhs.sukunimi;
   }
};

Then in Main.cpp I made a vector like this: vector<OpiskelijanTiedot> Tiedot;

And in my functions.cpp I have this:

void oppilas(vector<OpiskelijanTiedot>Tiedot, int laskuri) {
   OpiskelijanTiedot temp;
   cout << "Etunimi: ";
   cin.ignore();
   cin >> temp.etunimi;
   Tiedot.push_back(temp.etunimi);
   cout << "Sukunimi: ";
   cin >> temp.sukunimi;
   Tiedot.push_back(temp.sukunimi);
   cout << "Tehtyjen harjoitustehtavien maara: ";
   cin >> temp.HarjMaara;
   Tiedot.push_back(temp.HarjMaara);
   cout << "Harjoitustyon arvosana: ";
   cin >> temp.Arvosana;
   Tiedot.push_back(temp.Arvosana);
}

So I'm getting this error:

Description Project File Line Error C2664
'void std::vector>::push_back(const OpiskelijanTiedot &)': cannot convert argument 1 from 'float' to 'OpiskelijanTiedot &&'

I've been searching about this problem for a long time now and can't get rid of this, so any help or pointing me to right direction is greatly appreciated. I'm guessing it somehow has a problem with me not telling what's string int etc, but I can't figure it out. Thanks!

R Sahu
  • 204,454
  • 14
  • 159
  • 270
weku
  • 3
  • 3

2 Answers2

1

Tiedot is a vector of OpiskelijanTiedot, while temp.Arvosana is a float.

you should do

Tiedot.push_back(temp)
crazyxiazi
  • 68
  • 8
  • Oh god, that was easier than I thought, I tried literally everything else. Thank you, thank you so much! – weku Nov 28 '15 at 07:38
1

Your function has syntax errors, which were pointed out by the compiler. You cannot push objects of type string, int, and float to a vector of OpiskelijanTiedots.

The other error is that your function is adding an item to the input vector but the change only affects the copy of the vector since the argument is object, not a reference. If you want the change of adding an item to the vector to be reflected in the calling function, you'll have to change the argument type to be a reference.

void oppilas(vector<OpiskelijanTiedot>& Tiedot, int laskuri) {
                                 //  ^^^ Change the argument type to a reference
   OpiskelijanTiedot temp;
   cout << "Etunimi: ";
   cin.ignore();
   cin >> temp.etunimi;
   cout << "Sukunimi: ";
   cin >> temp.sukunimi;
   cout << "Tehtyjen harjoitustehtavien maara: ";
   Tiedot.push_back(temp.HarjMaara);
   cout << "Harjoitustyon arvosana: ";
   cin >> temp.Arvosana;

   // Add the object to the vector.
   Tiedot.push_back(temp);
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270