0

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;
}
4
  • 2
    AFAIK emplace_back requires 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) Commented Jul 15, 2018 at 1:28
  • 1
    As a side note, why are you not declaring printRepeats as void printRepeats(const matrix &element) (and make necessary adjustment)? Then replace the loop with for (auto &element : repeats) { printRepeats(element); } Commented Jul 15, 2018 at 2:23
  • 1
    Either add a constructor as mentioned above or use push_back with a matrix object instead. In a case like this one, it should not make a big difference. Commented Jul 15, 2018 at 2:34
  • 1
    I'm not quite sure why people don't think this is a decent question, because, to me, it's just fine - there's even a minimal reproducible example. That big wodge of text at the top wasn't helping, OP please take note. Commented Jul 15, 2018 at 6:21

1 Answer 1

4

@Borgleader is right, of course. But then the Borg always are.

So to get it to compile, all you have to do is change this:

struct matrix
{
    int value;
    int count;
};

To this:

struct matrix
{
    matrix (int value, int count) : value (value), count (count) {}  // constructor
    int value;
    int count;
};

And off you go.

See it over at Wandbox: https://wandbox.org/permlink/zzRRzdQjjG1vm4tM

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.