简体   繁体   English

C ++推回向量 <struct> MyVector和错误C2664

[英]C++ Pushback vector<struct>MyVector And Error C2664

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. 我正在尝试制作一个包含2个字符串,一个int和一个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; 然后在Main.cpp中,我创建了一个像这样的向量: vector<OpiskelijanTiedot> Tiedot;

And in my functions.cpp I have this: 在我的functions.cpp中,我有这个:

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 说明项目文件行错误C2664
'void std::vector>::push_back(const OpiskelijanTiedot &)': cannot convert argument 1 from 'float' to 'OpiskelijanTiedot &&' 'void std :: vector> :: push_back(const OpiskelijanTiedot&)':无法将参数1从'float'转换为'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. 我猜测它不知道什么是字符串int等有问题,但是我无法弄清楚。 Thanks! 谢谢!

Tiedot is a vector of OpiskelijanTiedot, while temp.Arvosana is a float. Tiedot是OpiskelijanTiedot的向量,而temp.Arvosana是浮点数。

you should do 你应该做

Tiedot.push_back(temp)

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 OpiskelijanTiedot s. 您不能将stringintfloat类型的对象推OpiskelijanTiedotvector

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. 另一个错误是您的函数向输入vector添加了一个项目,但是更改仅影响vector的副本,因为参数是对象,而不是引用。 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM