Pages: [1]   Go Down
Author Topic: Compile error in no-arg constructor (solved)  (Read 889 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#ifndef Test_h
#define Test_h

#include "WProgram.h"


class Test
{

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

};

#endif

This is Test.cpp:
Code:
#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:
#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
« Last Edit: August 23, 2009, 03:48:28 pm by yomb » Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 12
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Edison Member
*
Karma: 3
Posts: 1001
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
#include "WProgram.h"
#include <Test.h>

void setup() {

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

}
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It worked, thanks.
Logged

Pages: [1]   Go Up
Jump to: