What's wrong?

Started by
17 comments, last by Dalphin 9 years, 3 months ago

I have to do a programming excercise for university.

I've done most of it, but I get some error and I can't figure out what's

wrong and how to solve it.

I hope you can understand it, because as I said it's for university and

that's why I have to do it in my native language.


#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>

using namespace std;

class Rechthoek{
protected:
    int hoogte;
    int breedte;
public:
    Rechthoek(int hoogte, int breedte)
        :hoogte(hoogte), breedte(breedte){
        }

    void print()
    {
        for(int i = 0; i < hoogte; i++){
            for(int j = 0; j < breedte; j++)
                cout << "* ";
            cout << endl;
        }
        cout << endl;
    }
};

class FlexRechthoek : virtual public Rechthoek{
public:
    FlexRechthoek(int hoogte, int breedte)
        :Rechthoek(hoogte, breedte){
        }

    void hoger()
    {
        hoogte++;
    }

    void breder()
    {
        breedte++;
    }
};

class Venster : virtual public Rechthoek{
public:
    Venster(int h, int b)
        :Rechthoek(hoogte, breedte){
        }

    void print()
    {
        for(int i = 0; i < breedte; i++) //bovenstuk
            cout << "* ";
        cout << endl;

        for(int j = 0; j < hoogte - 2; j++){   //middenstuk
            cout << "* ";
            for(int i = 0; i < breedte -2; i++)
                cout << "  ";
            cout << "* " << endl;
        }


        for(int i = 0; i < breedte; i++) //eindstuk
            cout << "* ";
        cout << endl;
    }
};

class VensterMetTitel : public Venster{
protected:
    string titel;
public:
    VensterMetTitel(int h, int b, string titel)
        :Venster(hoogte, breedte), titel(titel){
        }

    void print()
    {
         for(int i = 0; i < breedte; i++) //bovenstuk
            cout << "* ";
        cout << endl;

        for(int j = 0; j < hoogte - 2; j++){   //middenstuk
            cout << "* ";
            for(int i = 0; i < breedte -2; i++)
                cout << "  ";
            cout << "* " << endl;
        }


        for(int i = 0; i < breedte; i++) //eindstuk
            cout << "* ";
        cout << endl;
    }
};

int main()
{
    Venster v1(5,9, "TEST");

    v1.print();
}

I get the following error message:

||=== Build: Debug in Opdrachten oefenen (compiler: GNU GCC Compiler) ===|
D:\gw\Opdrachten oefenen\main.cpp||In constructor 'VensterMetTitel::VensterMetTitel(int, int, std::string)':|
D:\gw\Opdrachten oefenen\main.cpp|76|error: no matching function for call to 'Rechthoek::Rechthoek()'|
D:\gw\Opdrachten oefenen\main.cpp|76|note: candidates are:|
D:\gw\Opdrachten oefenen\main.cpp|13|note: Rechthoek::Rechthoek(int, int)|
D:\gw\Opdrachten oefenen\main.cpp|13|note: candidate expects 2 arguments, 0 provided|
D:\gw\Opdrachten oefenen\main.cpp|8|note: constexpr Rechthoek::Rechthoek(const Rechthoek&)|
D:\gw\Opdrachten oefenen\main.cpp|8|note: candidate expects 1 argument, 0 provided|
D:\gw\Opdrachten oefenen\main.cpp|8|note: constexpr Rechthoek::Rechthoek(Rechthoek&&)|
D:\gw\Opdrachten oefenen\main.cpp|8|note: candidate expects 1 argument, 0 provided|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
Thanks

Advertisement


Venster(int h, int b) :Rechthoek(hoogte, breedte){

Shouldn't it be "Rechthoek(h,b)" ?

The same with...


VensterMetTitel(int h, int b, string titel) :Venster(hoogte, breedte), titel(titel){

Try with:

VensterMetTitel(int h, int b, string titel) :Venster(h, b), titel(titel){

Oh excuse me, first I had this, but this happened because I modified things,

while trying to fix it, but I still get the same error.

So maybe something else?

Look closely at your error message. I've bolded the critical parts.

D:\gw\Opdrachten oefenen\main.cpp|76|error: no matching function for call to 'Rechthoek::Rechthoek()'|
D:\gw\Opdrachten oefenen\main.cpp|76|note: candidates are:|
D:\gw\Opdrachten oefenen\main.cpp|13|note: Rechthoek::Rechthoek(int, int)|
D:\gw\Opdrachten oefenen\main.cpp|13|note: candidate expects 2 arguments, 0 provided|
D:\gw\Opdrachten oefenen\main.cpp|8|note: constexpr Rechthoek::Rechthoek(const Rechthoek&)|
D:\gw\Opdrachten oefenen\main.cpp|8|note: candidate expects 1 argument, 0 provided|
D:\gw\Opdrachten oefenen\main.cpp|8|note: constexpr Rechthoek::Rechthoek(Rechthoek&&)|
D:\gw\Opdrachten oefenen\main.cpp|8|note: candidate expects 1 argument, 0 provided|
On line 76 (although you say you have changed your code since then) you are calling a constructor with no parameters.
The compiler can find some potential matches and is offering them as a helpful hint. The first candidate has two parameters. It can also find an auto-generated copy constructor and an auto-generated move constructor.
You either need to provide a constructor with no parameters, also called a 'default constructor', or you need to change line 76 to have the necessary parameters.

Yes I already saw that. but I don't understand it completely, because me 76 line is:


        :Venster(hoogte, breedte), titel(titel){}

So what did I do wrong, because I have two parameters?

When you write "class FlexRechthoek : virtual public Rechthoek" you're using virtual inheritance (I just learned about it, I didn't know you could use the "virtual" keyword like that). Are you sure that's something you want?

I removed those "virtual" (that one on another below) and the code compiles. I've found that virtual inheritance is used to solve the multiple inheritance "diamond" problem, and you're not doing multiple inheritance here.

EDIT: Sorry, you are using multiple inheritance, so that's probably what you want. Look here: http://stackoverflow.com/questions/3524019/default-constructor-and-virtual-inheritance

When using virtual inheritance without a default constructor for the base class you need to make the call explicit. Something like this:


 VensterMetTitel(int h, int b, string titel)
        :Rochthoek(h,b), Venster(h, b), titel(titel)

Anyway, it looks like a really complex inheritance for some code that handles 2 ints and prints out some "*", do you really need something that complex?

EDIT2: Haha, sorry again, you're NOT doing multiple inheritance, VensterMetTitel inherits from Venster and Venster inherits from Rochthoek, FlexRochthoek is never used. So, check if you really need those "virtuals", and if you do, add the explicit call to the Rochthoek constructor.

Well for this excercise i will need virtual inheritance, because this is only the first part. If need to make a class that inherits from flexRechthoek and vensterMetTitel.

So any solutions?

Oops I was on my brother PC and I didn't saw that he was still logged in, excuse me.

So:

Well for this excercise i will need virtual inheritance, because this is only the first part. If need to make a class that inherits from flexRechthoek and vensterMetTitel.

So any solutions?


So any solutions?

Read again my answer, I gave you a solution to do virtual inheritance without default constructors and the link where I found it. Or search for "virtual inheritance with default constructor" in google.

I was able to make it work here:

http://codepad.org/O9Bci0Mf

But I'm not sure if instantiating Venster rather than VensterMetTitel was actually the idea which then makes my code defeat the purpose.
I haven't been able to make Diego's solution work though.

This topic is closed to new replies.

Advertisement