0

I am having trouble using push_back for vectors in c++.

My vector is named data.

In my loop I want to add 50 to data[i].getQuantity then push_back to data

These are things that I have tried.

 data.push_back(data[i].getQuantity());

and

 float myFloat = data[i].getQuantity() + 50;
 data.push_back(data[i].getQuantity(myFloat));
 data.push_back(myFloat);

The error is saying

No function to call to push_back

Here is my code:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <iterator>
struct Input
{
 friend std::istream& operator >>(std::istream& inp, Input& item);
 friend std::ostream& operator <<(std::ostream& outp, Input const& item);
 std::string group;
 std::string total_pay;
 float quantity;
 // default constructor. sets up zero-elements
 Input() : group(), total_pay(), quantity()
 {
 }
 Input(std::string groupIn, std::string total_payIn, float quantityIn) :
 group(std::move(groupIn)),
 total_pay(total_payIn),
 quantity(quantityIn)
 {
 }
 // Accessors
 std::string const& getGroup() const { return group; }
 std::string getTotalPay() const { return total_pay; }
 float getQuantity() const { return quantity; }
};
// global free function for extracting an Input item from an input stream
std::istream& operator >>(std::istream& inp, Input& item)
{
 return (inp >> item.group >> item.total_pay >> item.quantity);
}
// global operator for inserting to a stream
std::ostream& operator <<(std::ostream& outp, Input const& item)
{
 outp
 << item.getGroup() << ", "
 << item.getTotalPay() << ", "
 << item.getQuantity();
 return outp;
}
struct ctype : std::ctype<char>
{
 static mask* make_table()
 {
 static std::vector<mask> table(classic_table(),
 classic_table() + table_size);
 table[','] |= space;
 return &table[0];
 }
 ctype() : std::ctype<char>(make_table()) { }
};
int main() {
 std::fstream infile("employee.dat");
 std::vector<Input> data;
 std::string line;
 try {
 while (std::getline(infile, line))
 {
 std::istringstream iss(line);
 Input inp;
 iss.imbue(std::locale(iss.getloc(), new ctype));
 while (iss >> inp) // calls our extraction operator >>
 data.push_back(inp);
 if (iss.fail() && !iss.eof())
 std::cerr << "Invalid input line: " << line << '\n';
 }
 // dump all of them to stdout. calls our insertion operator <<
 std::copy(data.begin(), data.end(),
 std::ostream_iterator<Input>(std::cout,"\n"));
 std::ofstream outp("output.dat");
 for(int i = 0; i < data[i].getQuantity(); i++)
 {
 float myFloat = data[i].getQuantity() + 50;
 data.push_back(myFloat);
 outp << data[i].getGroup() << ',' << data[i].getTotalPay() << ',' << data[i].getQuantity() + 50 << '\n';
 }
 } catch (std::exception& e) {
 std::cout << "There was an error: " << '\n';
 return 1;
 }
 return 0;
}
Tacet
1,4212 gold badges17 silver badges33 bronze badges
asked Oct 29, 2014 at 20:42
1
  • 3
    Looks like you want emplace_back() rather than push_back(). Commented Oct 29, 2014 at 20:46

1 Answer 1

4

Your vector is of type std::vector<Input>. That means you can only put objects of type Input into it. You can't push_back a float into such a vector.

If your intention is to create a new Input object and push that back into your vector, you could do something like

data.push_back(Input(data[i].getGroup(), data[i].getTotalPay(), data[i].getQuantity() + 50))

On the other hand, if you are simply trying to modify an element in data without adding a new element to data, you could just do

data[i].quantity += 50;

This works because you use a struct rather than a class. In a struct, variables default privacy level is public. If you wanted to use a class, or you just don't want to directly access the struct members, you would have to create a setter function for quantity.

answered Oct 29, 2014 at 20:45
Sign up to request clarification or add additional context in comments.

13 Comments

ohhhh ok god that makes sense. So because I am doing it as object I need to push back a object. Is it possible to add 50 as I want and create a object to push back then? What would be the logical way to handle this?
@NDiaz Well you haven't shown your Input class definition, so I don't really know what you can do with that. If you show that class definition, I could try to help.
@NDiaz Now that you have added your Input code, could you say what you are trying to accomplish?
@NDiaz Are you trying to push_back a new Input object with the same group and total_pay but different quantity, or are you just trying to change the quantity of an Input object that is already in the vector?
@iwolf TAing has allowed me to recognize mappings of (things novice programmers do) -> (things novice programmers are trying to do) that regular professional software developers might never think of.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.