Ecere SDK/eC Forums
http://ec-lang.org/community/
Print view

How to write something like an interface ?
http://ec-lang.org/community/viewtopic.php?f=1&t=383
Page 1 of 1
Author:  nicktick [ Tue Jan 14, 2014 4:01 am ]
Post subject:  How to write something like an interface ?

Code: Select all

import "ecere"
class ITest
{
public:
  uint x;
  int  Read(int i);
  void Write();
}

class ITest2 : ITest
{
   uint x;
   int Read(int i)
   {
      return i;
   }
   void Write()
   {
   }
}

class M
{
   bool Test(ITest it)
   {
      it.Read(10);
   }
   int main(int argc,char **argv)
   {
      ITest2 it2{};
      Test(it2);
      return(0);
   }
}
On compiling the above code, the compiler tells:
Linking...
Linker Error: obj/debug.win32/Itest.o: In function `__ecereMethod_M_Test':
Itest.ec:31: error: undefined reference to `__ecereMethod_ITest_Read'
collect2.exe: error: ld returned 1 exit status
Author:  jerome [ Tue Jan 14, 2014 4:17 am ]
Post subject:  Re: How to write something like an interface ?

Hi nicktick!

eC does not have proper interface support yet (see http://www.ecere.com/mantis/view.php?id=514 for the wishlist feature)

To see what can be done in the current state of things, please take a look at this thread.

To be able to implement multiple interfaces from a single class, interface classes should not contain any data member, and all methods should be virtual and not require a this object, e.g.:

Code: Select all

class ITest
{
public:
  virtual int  ::Read(int i);
  virtual void ::Write();
}
In your case, you were getting this linking error because you declare a non-virtual method without defining it ( no { } body ). The compiler should have reported a compiler error (it's a bug that it did not -- http://www.ecere.com/mantis/view.php?id=1054).
All times are UTC-05:00 Page 1 of 1