-1

This is a similar question to others, BUT I have checked what this is being flagged as a duplicate as, and the general responses aren't helping me because I either do not understand them or they are irrelevant to my issue. I need help and/or a solution specifically to this problem. Please and thank you. I am building a "programming language" (but it isn't really, just don't know how to describe it), and in my ContainerList class I keep getting this error saying that there is no matching call for function. Why am I getting this error?

I've tried manipulating where and when the constructor is used or defined or if it even exists. But there seems to be no luck. I looked into the MemoryContainer class and it doesn't seem like there is anything to cause the error.

Here is my code for the ContainerList class:

#include "MemoryContainer.cpp"

struct ContainerListItem{
    int index = 0;
    MemoryContainer item;
    ContainerListItem* nextItem;
    ContainerListItem(byte none) {}
};

class ContainerList{
    public:
    int getSize() {return size;}
    void addContainer(MemoryContainer item){
        ContainerListItem indexItem = ContainerListItem(1);
        indexItem.item = item;
        indexItem.index = size - 1;
        indexItem.nextItem = &firstItem;
        firstItem = indexItem;
        size++;
    }
    MemoryContainer getContainer(int index){
        ContainerListItem currentItem = ContainerListItem(1);
        currentItem = firstItem;

        while(currentItem.index != index)
            currentItem = *currentItem.nextItem;

        return currentItem.item;
    }
    private:
    int size = 1;
    ContainerListItem firstItem = ContainerListItem(1);
};

full error for the defined constructor:

In file included from Main.cpp:1:
ContainerList.cpp: In constructor ‘ContainerListItem::ContainerListItem(uint8_t)’:
ContainerList.cpp:7:34: error: no matching function for call to ‘MemoryContainer::MemoryContainer()’
     ContainerListItem(byte none) {}
                                  ^
In file included from ContainerList.cpp:1,
                 from Main.cpp:1:
MemoryContainer.cpp:24:5: note: candidate: ‘MemoryContainer::MemoryContainer(uint64_t, std::__cxx11::string)’
     MemoryContainer(lint ContainerSize, string name){
     ^~~~~~~~~~~~~~~
MemoryContainer.cpp:24:5: note:   candidate expects 2 arguments, 0 provided
MemoryContainer.cpp:17:7: note: candidate: ‘MemoryContainer::MemoryContainer(const MemoryContainer&)’
 class MemoryContainer{
       ^~~~~~~~~~~~~~~
MemoryContainer.cpp:17:7: note:   candidate expects 1 argument, 0 provided
MemoryContainer.cpp:17:7: note: candidate: ‘MemoryContainer::MemoryContainer(MemoryContainer&&)’
MemoryContainer.cpp:17:7: note:   candidate expects 1 argument, 0 provided
smac89
  • 39,374
  • 15
  • 132
  • 179
  • So, which exact line in the code you've shown defines the undefined constructor that's referenced by the error message? Of course, none of them. So why do you think it's important to show something that has nothing to do with the error message? Where is this constructor defined? How are you linking with that translation unit? – Sam Varshavchik Aug 26 '19 at 22:47
  • Then where is the problem? Do you want me to post the entire code? Last I checked the error message references the lines of code that I included, so what do you mean by the first part? The constructor and I get another error whenever I define the constructor in the struct (as stated before in my post). I'm linking by pushing the item to a memory slot and then having the head point to it (with this happening for each item), this is how I normally do linked-lists, and I normally do not get an error. But just to double check, I am going to set a dummy program with the same setup toTest itToBeSure – pgrafhamster Aug 26 '19 at 23:27
  • I will also comb through the solutions on that other question's post to see if I miss anything. In the mean time, if someone has a solution could someone post it to this thread? – pgrafhamster Aug 26 '19 at 23:30
  • The problem is that the referenced constructor is not defined. How exactly do you expect anyone to tell you where you can find something that is not defined anywhere? You have to define it yourself, and include it in your compiled code. – Sam Varshavchik Aug 26 '19 at 23:33
  • I know that, the thing is I did define it, but it gives me another error. Let me edit my post to show you the error that comes out of it. – pgrafhamster Aug 26 '19 at 23:35
  • 1
    If you have a compilation error, the right thing to do is to figure out why you're getting the error. Removing the code and hoping that the error will go away will never work. And now, by adding an error message from compiling some other code other than the one that's shown in the question, the end result makes no sense at all, until someone reads these comments. You need to scrap the whole thing, and post the original question you should've posted about the original error, and make sure it meets all requirements for a [mre], as explained in stackoverflow.com's [help]. – Sam Varshavchik Aug 26 '19 at 23:48
  • 1
    And before you post a cleaned-up version of the question, you must take a [tour] of stackoverflow.com, visit the [help], and read the instructions for [ask] questions here. Otherwise whatever you post again is likely to also be unanswerable for similar reasons, and closed as off-topic. – Sam Varshavchik Aug 26 '19 at 23:50
  • 1
    Unrelated: `#include "MemoryContainer.cpp"` is almost always a mistake. Once you get past the current crop of bugs you'll probably start getting multiple definition errors as MemoryContainer.cpp is compiled on it's own and because it was included in ContainerList.cpp. Include header files. Compile cpp files. – user4581301 Aug 26 '19 at 23:54
  • @user4581301 oh yeah, I know. I’m going to change that later, I just had it this way because I was debugging a different error earlier, and forgot to change it back again. – pgrafhamster Aug 26 '19 at 23:55
  • Except that your post was, indeed, a duplicate of a canonical question about linking failures due to undefined symbols. The shown code was linked without defining the constructor. That's it. And you later admitted that you removed the constructor. Hence the link failure. It was the right duplicate. – Sam Varshavchik Aug 26 '19 at 23:56
  • Sam's explained how you can get onto a path where we can help you. Right now we can't. The information we need is not here. Recommendation: don't do something you know is wrong to solve a problem. Usually it just makes things wrong-er. Instead of one bug, you now have at least two, and bugs have an annoying habit of ganging up on you by hiding or distorting the effects of each other. Sometimes you even successfully solve bug one and discard the solution because it didn't also solve the new bugs. – user4581301 Aug 26 '19 at 23:58
  • Please do one more thing and fix the question's title, which is now referencing a completely different problem. Although I can edit it myself, this is your question. And try to reformat the real compilation errors, that were appended by your edit, so that they're legible. Your real compilation error is actually not very complicated, but, as of now, this question still needs a little bit more work before it can be rightfully answered. Maybe you can even figure out what the problem is, simply by ***reading*** what the first error message says. It's in plain English. – Sam Varshavchik Aug 27 '19 at 00:06
  • 1
    Putting together a simple example and filling in the blanks, I wind up with https://ideone.com/TbT1RZ . This does not reproduce your problem. What you want to do is manufacture, with the smallest possible amount of code, a program that would run if not for the undefined reference. The beauty of making one of these is usually the program is so small that you can see the error yourself. – user4581301 Aug 27 '19 at 00:13
  • 1
    As a new programmer the best thing you can do is write small amounts of code, a function at the most, at a time and compile and test it before adding anything else. Always build on solid foundation. One of the biggest differences between a new and experienced programmer is how much code can be comfortably be written before testing it. Make a class. compile and test. Add a constructor to the class. Compile and test. Add a utility function. Compile and test. Rinse, repeat. The experienced programmer may write the whole class in one shot because, well, experience tells them they can. – user4581301 Aug 27 '19 at 00:26

1 Answers1

1

Your ContainerListItem class has a member that's another class:

MemoryContainer item;

However the constructor does not initialize it at all:

ContainerListItem(byte none) {}

The constructor is mostly empty.

All members of a class must be initialized by its constructor. If the constructor does not explicitly initialize a class member, it must have a default constructor that initializes it. A default constructor is a constructor that takes no parameters.

You did not show the declaration of your MemoryContainer class, but it must have a constructor that takes at least one parameter (actually there might be a few other reasons too, but this is the most likely one).

Which means that ContainerListItem must either explicitly initialize the class member, by constructing it, or you must add a default constructor to MemoryContainer.

This is exactly what your compiler is telling you, if you carefully read its error message:

error: no matching function for call to ‘MemoryContainer::MemoryContainer()’

The compiler is telling you that this class's default constructor does not exist. Because ContainerListItem does not explicitly initialize this class member, its default constructor must be called. But it's not declared.

You will find more information about the different ways of initializing class members in your C++ book. But if, for example, MemoryContainer's only constructor takes an int parameter, and you wish to initialize it, in this case, by calling its constructor with 0, your ContainerListItem constructor would read (using modern C++ uniform initialization syntax):

ContainerListItem(byte none) : item{0} {}

You need to figure out whether you want to implement a default constructor for MemoryContainer, or how to initialize it here. Nobody else can figure it out for you, this is a decision that only you can make.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • Thank you for elaborating. I wasn't able to connect the dots until you made this explanation. – pgrafhamster Aug 27 '19 at 00:24
  • Sidenote: You cannot initialize a member in the body of the constructor. All members have been initialized before the body of the constructor has been entered. That groovy `: item{0}` thing in the constructor is a [Member Initializer List](https://en.cppreference.com/w/cpp/language/initializer_list), C++'s way to initialize members. You'll also find this handy for initializing base classes when using inheritance. – user4581301 Aug 27 '19 at 00:27