Compile Error Depends on Constructor Used

Hi All.

After many years a C programmer, I’m finally tackling the intricacies of C++ classes and constructors. This code illustrates the problem I hit:

class myClass {
  private:
    uint16_t y;

  public:
    void printValues();    
    myClass();
    myClass(uint16_t);   
};

myClass::myClass() : y(2) {}
myClass::myClass(uint16_t y) : y(y) {}

void myClass::printValues() {
  Serial.print("y = ");Serial.println(y);
}

myClass classInstance1();
myClass classInstance2(44);

void setup() {
  Serial.begin(115200);
  delay(1000);
  classInstance1.printValues();
}

void loop() {}

The compile-time error message is:

Arduino: 1.8.2 (Windows 10), TD: 1.36, Board: "Arduino/Genuino Uno"

C:\Users\tr001221\Documents\Portable Arduino 1.8.2\arduino-1.8.2\portable\sketchbook\yyy\yyy.ino: In function 'void setup()':

yyy:24: error: request for member 'printValues' in 'classInstance1', which is of non-class type 'myClass()'

   classInstance1.printValues();

                  ^

exit status 1
request for member 'printValues' in 'classInstance1', which is of non-class type 'myClass()'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

However, if instead I try to call the ‘printValues()’ method on the other class instance:

class myClass {
  private:
    uint16_t y;

  public:
    void printValues();    
    myClass();
    myClass(uint16_t);   
};

myClass::myClass() : y(2) {}
myClass::myClass(uint16_t y) : y(y) {}

void myClass::printValues() {
  Serial.print("y = ");Serial.println(y);
}

myClass classInstance1();
myClass classInstance2(44);

void setup() {
  Serial.begin(115200);
  delay(1000);
  classInstance2.printValues();
}

void loop() {}

It compiles without error.

Would appreciate help with this from an experienced C++ coder.

Thanks.

UPDATE:
Looks like my first instantiation was incorrect. Compiles fine if I do it this way:

myClass classInstance1;

Instead of:

myClass classInstance1();

Will have to wait until later to test it on hardware.

Not sure how you expect anyone to help, when you clearly have NOT posted the part of the code containing the problem...

Regards,
Ray L.

Actually, I did in the very first post:

  • The first </> block in that post contains the code that produces the compile error.

  • The second </> block in that post contains the actual compiler error message(s).

  • The third </> block in that post contains the code that compiles without an error.

In Reply #1 I posted the correction (for the first </> in my original post) that fixed the compile error.

What’s missing?

Additional info. The first </> block in my original post produce the same error for both Uno and Teensy3.2 board selections.

Arduino IDE 1.8.2
Teensyduino 1.36

Anything else required?

myClass::myClass() : y(2) {}
myClass::myClass(uint16_t y) : y(y) {}

Note: The ": initializers" syntax is required if the target variable is 'const' but for regular variables I find it easier to read and understand a traditional assignment:

myClass::myClass() {y=2;}
myClass::myClass(uint16_t _y) {y=_y;}

Understood.

Turns out either constructor method causes a compile error if I don’t get the syntax right.

In order instantiate a variable using the “no parameter” constructor, it looks like the syntax needs to be:

myClass classInstance1;

It won’t compile if I try this:

myClass classInstance1();

I guess this makes sense, I just thought there might be some symmetry with the versions of the constructor that do take parameter(s). Meaning, if this is OK:

myClass classInstance2(44);

…why is this not OK:

myClass classInstance1();

It's not OK for creating an instance because it looks just like a function prototype for a function that takes no arguments and returns an instance of myClass. Similarly, if the parens contain a declaration then it IS a function prototype:

myClass identifier(int x);

Makes sense. Thanks.