3

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...

iammilind
  • 68,093
  • 33
  • 169
  • 336
Roy Tommy
  • 51
  • 3
  • When posting questions about build errors, please include the *complete* error output, including any informational notes. Most likely the error message contains all information actually needed, but since we cant see it all we don't have all information. So please edit your question to include the complete output, in full and unedited, just copy-pasted. – Some programmer dude May 02 '16 at 08:39
  • While you're at it, please [drop that `using namespace std;`](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-in-c-considered-bad-practice) ;) – Quentin May 02 '16 at 08:55
  • slightly offtopic: Why is `vertex` a nested class of `quadrangle`? Would e.g. class `triangle` define different vertices? – VolkerK May 02 '16 at 08:56
  • A tip for debugging: simplify. Nested classes can be confusing, so if things aren't working, try moving the nested class to global scope. That wouldn't have fixed this problem, but it would have produced a better title for this question. That's not a comment on the use of nested classes; after seeing that moving it outside didn't affect the problem, you can put it back inside. – Pete Becker May 02 '16 at 12:14

2 Answers2

6

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).

Jarod42
  • 203,559
  • 14
  • 181
  • 302
3

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);    
}
marcinj
  • 48,511
  • 9
  • 79
  • 100
  • 1
    The thing in your second part is indeed a function pointer. Function types in a function parameters' declaration decay to pointers, much like arrays do. [Live check](http://coliru.stacked-crooked.com/a/40d2668b9f5c7bdc) – Quentin May 02 '16 at 08:53
  • @Quentin I didnt know that, thanx – marcinj May 02 '16 at 09:05