Please tell valarray how you can add an object to valarray . There is valarray

 valarray<Inspector> InspectorData; 

I'm trying to add this way:

 InspectorData.operator+= (Inspector(chNameDep, chTown, chRegion, chStreet, iBuilding, chNameInsp, chSurnameInsp, chPatronymicInsp, chPosition, chRank, iAge)); 

But I get an error.

 C2676 бинарный "+=": "Inspector" не определяет этот оператор или преобразование к типу приемлемо к встроенному оператору. 

Please tell me how to add an item.

Thanks in advance for your help.

    2 answers 2

    You can of course create a valarray<Inspector> object, but:

    • if the Inspector has no default arguments:

    You must immediately indicate these arguments and amounts:

      valarray<Inspector> inspactorData(Inspector(arg1, arg2,...), 10); 

    then you get inspactorData with 10 Inspector(arg1, arg2,...) objects Inspector(arg1, arg2,...) , which you can then replace with others.

    • If the Inspector still has a default constructor, then:

    you can create an object

      valarray<Inspector> inspactorData; 

    but then you have to set the size (how many objects are supposed to contain)

     inspactorData.resize(10); inspactorData[0] = Inspector(chNameDep, chTown, chRegion, chStreet, iBuilding, chNameInsp, chSurnameInsp, chPatronymicInsp, chPosition, chRank, iAge); 

    So you can fill in every element of the valarray object. All his operators lead to the call of the operators of the elements valarray . So your attempts to add using the + operator result in the fact that you summarize the objects contained in the sequence with another object, which is not what you expect, and secondly, your class may not have the definition of operator+

    But valarray optimized for numerical methods, for other purposes it is simply undesirable to use it, and sometimes even lead to a dead end. So specifically for your class, better use containers ("not quite a kiteyner": this gave valarray the Stroustrup class)

    • Thank you very much for the clarification. - Jack132

    For storage in the valarray class must conform to the Numric type specification. The class called Inspector not very similar to a number. Moreover, the + = operator in this case is used for elementwise addition of vectors, and not for addition. In short, use std::vector , then you can add an element like this:

     ::std::vector<Inspector> InspectorData{}; InspectorData.emplace_back(chNameDep, chTown, chRegion, chStreet, iBuilding, chNameInsp, chSurnameInsp, chPatronymicInsp, chPosition, chRank, iAge); 
    • not quite a valid statement - AR Hovsepyan