declaring and using a class

ok. I’ve been writing nothing but C# for over a year and now .cpp is driving me bonkers.
I’m trying to declare a simple class called Point()
it has one short variable called X and another short variable called Y.
I actually had that working but when I created another class called Player that used Points it all collapsed.

I went through the tutorials Morse in the Arduino tutorials and am still having trouble with it.

so I first wrote the .h & .cpp files using the Arduino IDE and it kept creating a new directory and adding the .ino extensions to everything. I created a Point sub-directory in the Libraries directory and changed their extensions when I moved the files there. (can I use the IDE to edit my .h and .cpp files without their extensions and directories being auto-magically changed ?)

so, now I’m editing them with the MS NotePad (not the best) and I’m getting syntax errors which won’t compile.

have a look at my code:
this is the Point.h file

#ifndef Point_h
#define Point_h

#include "Arduino.h"

class Point
{
  public:
    Point(short x, short y);
    short X;
    short Y;
};

#endif

and its matching Point.cpp

#include "Arduino.h"
#include "Point.h"

Point::Point(short x, short y)
{
X = x;
Y = y;
}

I can use the Point class in my project by itself without a problem but then I add Points to a second class called Player and all hell breaks loose

#ifndef Player_h
#define Player_h

#include "Arduino.h"
#include "Point.h"

class Player
{
  public:
    Player();
    Point ptGame(0,0);
    Point ptGamePrevious(0,0);
    Point ptScreen(0,0);
    byte bytImage;
};

#endif

and its .cpp

#include "Arduino.h"
#include "Point.h"
#include "Player.h"

Player::Player()
{
  ptGame =Point(0,0);
  ptGamePrevious = Point(0,0);
  ptScreen = Point(0,0)
  bytImage = 0;
}

just declaring my intention to use the Player library with the line

#include <Player.h>

results in a slew of errors like :
C:…\Arduino\libraries\Player/Player.h:18:5: error: ‘Point::Point’ names the constructor, not the type

Point::Point ptScreen(0,0);

^~~~~

exit status 1
Error compiling for board Arduino Uno.

can you help me get started with Libraries in cpp?

Point ptGame(0,0);

This is not valid syntax. See the documentation for default member initialization.

Also get in the habit of using initializer lists in your constructor. If all your constructor is doing is just copying some parameters to member variables, there's no point in moving it to a separate file, just define it inline.

C++ is a statically typed language. You know the type of a variable, so there's no reason to include that type in the variable name as well.

struct Point {
  Point(short x, short y) : x(x), y(y) {}

  short x;
  short y;
};

struct Player {
  Point game{0, 0};
};

Pieter

thanks for your reply,

can you tell me what's wrong with

struct Point 
{
  Point(short x, short y) : X(x), Y(y) {}

  short X;
  short Y;
};

struct Player 
{
  Point game{0, 0};
  Point screen(0, 0);
};

Christ_Kennedy:
thanks for your reply,

can you tell me what's wrong with

  Point screen(0, 0);

Those parentheses should be replaced by curly braces, like in the line above it.

Note, for testing purposes, it's easier to put everything in the main .ino file. Once it's right, you can break it up into individual .h and .cpp files:

class Point {
  public:
    Point(short x, short y);
    short X;
    short Y;
};

Point::Point(short x, short y) : X(x), Y(y) {
}

class Player
{
  public:
    Player();
    Point ptGame;
    Point ptGamePrevious;
    Point ptScreen;
    byte bytImage;
};

Player::Player() : ptGame(0, 0), ptGamePrevious(0, 0), ptScreen(0, 0), bytImage() {
}

Player myPlayer;

void setup() {
}

void loop() {
}

awesome.
I didn't want to ask "could you write my code for me?" but this cut-n-paste sample will get me going!
thanks.