so I'm working with C++ Primer and I'm trying to modify one of the book's examples to utilize a struct and a vector using said struct to store and then call on elements of said vector for print.
To do this, I useemplace_back() and pass it two integer arguments to satisfy the two integer declarations in the struct and then place that struct into the vector (I believe).
However, I keep getting "error C2661: 'matrix::matrix': no overloaded function takes 2 arguments" when I try to debug the program. I'm not too sure what's happening, and I can't seem to understand other explanations given to people with the same issue. The program runs mostly fine as it is written in the book (it compiles and doesn't die), but I'm trying to incorporate what I've learned in Accelerated C++ into Primer.
Help a beginner out? Here's what I've got:
#include <iostream>
#include <vector>
struct matrix //create struct
{
int value;
int count;
};
void printRepeats(std::vector<matrix>& e, std::vector<matrix>::size_type& r)
{
std::cout << e[r].value << e[r].count; // print elements of struct
}
int main()
{
std::vector<matrix> repeats;
int currVal = 0;
int val = 0;
if (std::cin >> currVal)
{
int cnt = 0;
while (std::cin >> val)
{
if (val == currVal)
{
++cnt;
}
else
{
repeats.emplace_back(currVal, cnt);
currVal = val;
cnt = 0;
}
}
}
for (std::vector<matrix>::size_type r(0); r != repeats.size(); r++)
{
printRepeats(repeats, r);
}
std::cin.get();
std::cin.get();
return 0;
}
emplace_backrequires your type to have ctor (more specifically a non default one taking those 2 arguments you passed to emplace_back) (P.S: as an aside, that//use emplace_back()comment doesnt serve a purpose and should be removed, imho)printRepeatsasvoid printRepeats(const matrix &element)(and make necessary adjustment)? Then replace the loop withfor (auto &element : repeats) { printRepeats(element); }push_backwith amatrixobject instead. In a case like this one, it should not make a big difference.