Go Down

Topic: Compile error in no-arg constructor (solved) (Read 1 time) previous topic - next topic

yomb

Aug 23, 2009, 05:21 am Last Edit: Aug 23, 2009, 10:48 pm by yomb Reason: 1
Hi,

I've been trying to create a library class, and I get a compile error in the sketch when trying to access members of an instance, wich class has a no-arg constructor.

This is the library class, wich I've put in the hardware\libraries dir as usual:

This is Test.h:
Code: [Select]

#ifndef Test_h
#define Test_h

#include "WProgram.h"


class Test
{

 public:
     
      int testVar;
 
     Test();
     void testMethod();

};

#endif


This is Test.cpp:
Code: [Select]

#include "WProgram.h"
#include "Test.h"

Test::Test()
{
     testVar = 1;
}

void Test::testMethod()
{
     testVar = 2;
}


And this is the sketch testing the Test class:
Code: [Select]

#include <Test.h>

void setup() {

 Test myTest();
 myTest.testVar = 34;
 myTest.testMethod();

}

void loop() {

}


When compiling the sketch, the compiler says:

In function 'void setup()':
error: request for member 'testVar' in 'myTest', wich is of non-class type 'Test ()()'


This happens when accessing the member variable or the method.

But, when adding an integer parameter to the constructor, it compiles nicely. I suppose that it's a bug, because no-arg constructor should be allowed, isn't it?

Thanks in advance

I forgot to mention that I'm using compiler version 0017

AlphaBeta

It will probably not fix your problem, but remove the WProgram from the cpp.

Some compilers will roduce erratic code if both the header and the source includes the same file.

BenF

You should be able to instantiate a class with a single no-args constructor using the following syntax:

Code: [Select]
#include "WProgram.h"
#include <Test.h>

void setup() {

 Test myTest;  // use constructor with no args
 myTest.testVar = 34;
 myTest.testMethod();

}


Go Up