Roy Tommy
Roy Tommy

Reputation: 61

Nested classes error in C++

I'm self-learning C++ as a beginner, and I faced some problems regarding Nested Classes. I was trying to define a class for a quadrangle given four vertices (define a point called vertices), which is represented by an object of a nested class for two-dimensional points. I only use one point to test my answer. My answer to the question is:

#include <iostream>
#include <assert.h>
using namespace std;

class quadrangle
{
public:
    class vertex
    {
    private:
    public:
        int x, y;
        friend class quadrangle;
        vertex();
        vertex(int a, int b);
        vertex(const vertex & old);
    };
    vertex p1;
    int a, b;
    friend class vertex;
    quadrangle();
    quadrangle(vertex(int a, int b)) : p1(a,b) {};
    quadrangle(const quadrangle & old);
    void draw();

};
quadrangle::vertex::vertex()
{
    x = 0; y = 0;
}
quadrangle::vertex::vertex(int a, int b)
{

    x = a; y = b;
}
void quadrangle::draw()
{
    cout << "p1: (" << p1.x << "," << p1.y << ") " << endl;
}
quadrangle::quadrangle()
{
    p1.x = 0; p1.y = 0;
}
int main()
{
    quadrangle q1(quadrangle::vertex(2,3));
    q1.draw();
}

Somehow I just got

error: no matching function for call to 'quadrangle::quadrangle(quadrangle::vertex)'

and have stuck for a whole afternoon. Could someone explain what's wrong in my code?? I know something's wrong with my constructor but I just couldn't fix it...

Upvotes: 3

Views: 954

Answers (2)

marcinj
marcinj

Reputation: 49976

Your error tell it all, you dont have constructor:

 quadrangle(const vertex & old);

and it is required to make this initialization:

 quadrangle q1(quadrangle::vertex(2,3));

And this is really strange:

quadrangle(vertex(int a, int b)) : p1(a,b) { }

it looks like its a constructor taking a function prototype (or a function type?) - but its not a function pointer I guess. p1(a,b) compiles only because you have such variables in your class.

[edit]

after comment from Quentin - above declaration is a function pointer

Function types in a function parameters' declaration decay to pointers

below example shows various ways you can write function pointer as parameter to function:

std::string bar(int a, int b) {
    std::cout << "bar";
    return "";
}
void foo1(std::string(int a, int b)) { } // Unnamed function pointer
void foo2(std::string(pf)(int a, int b)) { pf(0,0); } // Named function pointer
void foo3(std::string(*pf)(int a, int b)) { pf(0,0);} // Named function pointer

int main() {
    foo1(bar);
    foo2(bar);
    foo3(bar);    
}

Upvotes: 3

Jarod42
Jarod42

Reputation: 217085

Following is not what you expect:

quadrangle(vertex(int a, int b)) : p1(a,b) {};

it is a constructor which take a function returning vertex and taking 2 int. and then you initialize member vertex p1 with uninitilized member a and b.

What you want is simply:

quadrangle(const vertex& v) : p1(v) {}

(And remove members a, b).

Upvotes: 6

Related Questions