0

I am writting two classes called Point and Edge The first one works ok, but the second, which has as members objects of the first one, is giving me an error I can't figure out. I am newbie to working with classes. The code is:

#include <iostream>
#include <math.h>
using namespace std;
class Point2D{
    float x;
    float y;
 public:
     Point2D();
     Point2D(float);
     Point2D(float,float);
    float getx();
    float gety();
    void displayxy();};
class Edge{
    Point2D pA;
    Point2D pB;

public:
    int a=888;
    Edge();
    Point2D CoordinateA();
    float CoordinateAx();
    float CoordinateAy();
    Point2D CoordinateB();
    float CoordinateBx();
    float CoordinateBy();};

Point2D::Point2D(){
    cout <<"First constructor called"<<endl;
    x=0;
    y=0;}
Point2D::Point2D(float xx){
    cout <<"Second constructor called"<<endl;
    x=xx;
    y=0;}
Point2D::Point2D(float xx, float yy)
{   cout <<"Third constructor called"<<endl;
    x=xx;
    y=yy;}

float Point2D::getx(){return x; }
float Point2D::gety(){return y;}
void Point2D::displayxy(){cout<<"Coord x= "<<x<<" Coord y= "<<y<<endl;}

Edge::Edge(){Point2D pA(44,55); Point2D pB(66,77);}
float Edge::CoordinateAx(){return pA.getx();}
int main()
{   Edge side1();
    cout <<side1.CoordinateAx()<<endl;
    cout <<side1.CoordinateAy()<<endl;
    //Point2D side1cA=side1.pA;;
    return 1;
}

and the error:

C:\...\Poligoni\main.cpp|49|error: request for member 'CoordinateAx' in 'side1', which is of non-class type 'Edge()'|
C:\...\Poligoni\main.cpp|50|error: request for member 'CoordinateAy' in 'side1', which is of non-class type 'Edge()'|

which is referred to this two lines

cout <<side1.CoordinateAx()<<endl;
cout <<side1.CoordinateAy()<<endl;

Any idea?

some_math_guy
  • 333
  • 1
  • 8
  • 10
    `Edge side1();` is a function declaration – UnholySheep Dec 12 '19 at 12:22
  • 1
    https://stackoverflow.com/questions/14077608/what-is-the-purpose-of-the-most-vexing-parse – 463035818_is_not_an_ai Dec 12 '19 at 12:23
  • 2
    To solve your problem do `Edge side1;` – Some programmer dude Dec 12 '19 at 12:23
  • 3
    On a totally unrelated note, I think you should learn how to separate your code into "paragraphs" (using empty lines) more consistently. The class definition for `Point2D` and `Edge` flow together, making it hard to see when one ends and the other begins. Your use of trailing closing `}` doesn't make it better. While it takes time to come up with a coding style that you feel comfortable with, at least try to make it consistent. – Some programmer dude Dec 12 '19 at 12:25
  • Some beginner tips: Use for every class one header and source file. So you would have point2d.h, point2d.cpp, edge.h and edge.cpp. You don't need it right now, but you would seperate your code better – RoQuOTriX Dec 12 '19 at 12:25
  • @ Some programmer dude, if I use the constructor with one argument I do: Point(arg1), so when I am using an empty construtor I thought I could do Point() why is that wrong?.Thanks for the spacing advice, I just compacted it to posted it here. – some_math_guy Dec 12 '19 at 12:29
  • 1
    C++ chose to use the syntax `A foo();` for a declaration of a function with no arguments, rather than C's `A foo(void);` so you have to miss the parentheses off as well to disambiguate them. – Pete Kirkham Dec 12 '19 at 12:31
  • @ RoQuOTriX In fact, I do need to separate it like that, but can't find the way to link the files, that is how do I make them talk to each other?, what should I include on each file? – some_math_guy Dec 12 '19 at 12:31
  • `Edge point1();` declares a function that takes no argument and returns an `Edge` object by value. When you pass an argument to a constructor the compiler can (in *most* cases) disambiguate it to see that it's a construction of an object. There are cases when that's not possible, see [the most vexing parse](https://en.wikipedia.org/wiki/Most_vexing_parse). – Some programmer dude Dec 12 '19 at 12:31
  • It depends on how you are compiling. Your book/tutorial should cover this topic – RoQuOTriX Dec 12 '19 at 12:34
  • @RoQuOTriX I am using codeblocks, and I think I have to used some macro instructions I am not familiar with like ifndef – some_math_guy Dec 12 '19 at 12:40
  • @Botje Please note that the most vexing parse is only *one* of the problems of this code. Consider the default constructor of `Edge`: `Edge::Edge(){Point2D pA(44,55); Point2D pB(66,77);}` – Bob__ Dec 12 '19 at 15:42
  • @Bob__ what is wrong with it?, this leads to another error, if I put instead Edge: Edge::Edge(){ pA.x=44 ;pA.y=55 ;pB.x=66 ;pb.y=77; } I get another error, saying x and y are private, which I can't figure why, x is private in the Point Class, but pA and pB are members of the Edge class and I am working within its Constructor. – some_math_guy Dec 12 '19 at 15:57
  • Those are *local* variables, not the members `Edge::pA` or `Edge::pB`. Please read about [member initializer lists](https://en.cppreference.com/w/cpp/language/initializer_list) and [non-static member initialization](https://en.cppreference.com/w/cpp/language/data_members#Member_initialization). – Bob__ Dec 12 '19 at 16:05

0 Answers0