Reputation: 6189
I'm a novice in c++
, i know there is a lot of similar questions but unfortunately didn't help me to solve this problem (which is a conceptual misunderstood i think)
So i have this constructor
Field::Field(const Position &posG, const Position &posC) {
//...
}
And i am creating a Field
as follows
// Create positions
Position posG, posC;
posG.x = 3;
posG.y = 3;
posC.x = 4;
posC.y = 4;
// Create pointers to positions
const Position *pPosC(&posC);
const Position *pPosG(&posG);
// Create field
Field field (pPosG, pPosC);
Where position is
struct Position {
int x;
int y;
};
Then i am getting this exception:
main.cpp:27:30: error: no matching function for call to ‘Field::Field(const Position*&, const Position*&)’
Field Field (pPosG, pPosC);
In file included from main.cpp:2:0:
Any help? Regards
Upvotes: 0
Views: 55
Reputation: 4121
you must define a constructor
, then use of const Position *pPosC(&posC);
expression.
you must define constructor-copy
like this :
struct Posicion {
public :
Position(const Position &pos) // constructor copy
{
// some code here
x = pos.x;
y = pos.y;
}
int x;
int y;
};
Upvotes: 0
Reputation: 49018
Field(const Position &posG, const Position &posC);
^^^^^ ^^^^^^
Those are references. So when you try to pass pointers
Field field (pPosG, pPosC);
^^^^^ ^^^^
pointer pointer
It can't compile.
Either make the constructor accept reference pointers (const Position *&posG)
or pass the value of the pointers (*pPosG
), or just pass the values directly (posG
).
Upvotes: 3
Reputation: 206707
When you have a constructor defined as
Field(const Position &posG, const Posicion &posC);
You can use objects of type Position
as arguments to it, not pointers to Position
.
You can use:
Field field(posG, posC);
or
Field field(*pPosG, *pPosC);
Upvotes: 3
Reputation: 96286
Your constructor expects references, not pointers.
As a sidenote, it's not clear why you use references or pointers.
Upvotes: 2