LuaBridge icon indicating copy to clipboard operation
LuaBridge copied to clipboard

.addData() cannot access base class's member variable

Open addictgamer opened this issue 7 years ago • 3 comments

If I have a class Vector with member float x,

class Vector {
public: float x; ... };

And then later a class WideVector which inherits from Vector:

class WideVector : public Vector {
...
};

When I then subsequently try to do this:

	luabridge::getGlobalNamespace(lua)
		.beginClass<WideVector>("WideVector")
		.addConstructor<void (*) (float, float, float, float)>()
		.addData("x", &WideVector::x, true)
                ...

I get the following error with GCC on the .addData() line:

   .addData("x", &WideVector::x, true)
...
/usr/local/include/LuaBridge/detail/Namespace.h:658:16: note:   template argument deduction/substitution failed:
note:   mismatched types ‘WideVector’ and ‘Vector’
   .addData("x", &WideVector::x, true)

EDIT: Tried under clang, similar result:

                .addData("x", &WideVector::x, true)
                ~^~~~~~~
/usr/local/include/LuaBridge/detail/Namespace.h:658:16: note: candidate template ignored: could not
      match 'WideVector' against 'Vector'
    Class <T>& addData (char const* name, const U T::* mp, bool isWritable = true)

addictgamer avatar Dec 20 '18 07:12 addictgamer

Which LuaBridge version (commit) do you use?

When I run the following test:

struct Vector
{
  float x = 0;
};

struct WideVector : Vector
{
  WideVector (float, float, float, float w)
  {
    x = w;
  }
};

TEST_F (IssueTests, Issue178)
{
  luabridge::getGlobalNamespace (L)
    .beginClass <WideVector> ("WideVector")
    .addConstructor <void (*) (float, float, float, float)> ()
    .addData ("x", &WideVector::x, true)
    .endClass ();
  
  runLua ("result = WideVector (0, 1, 2, 3).x");

  ASSERT_EQ (3.f, result ().cast <float> ());
}

I get it compiled and run:

Note: Google Test filter = IssueTests.Issue178
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from IssueTests
[ RUN      ] IssueTests.Issue178
[       OK ] IssueTests.Issue178 (1 ms)
[----------] 1 test from IssueTests (1 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (4 ms total)
[  PASSED  ] 1 test.

D:\LuaBridge\build32\Tests\Debug\LuaBridgeTests52.exe (process 13344) exited with code 0.
Press any key to close this window . . .

dmitry-t avatar Mar 11 '19 19:03 dmitry-t

I was able to reproduce the issue using GCC 7.3.0. As far as I understand this should not compile with MSVC as well.

In order to make you WideVector working, you should register the Vector and then register the WideVector as a derived class.

  luabridge::getGlobalNamespace (L)
    .beginClass <Vector> ("Vector")
    .addData ("x", &Vector::x, true)
    .endClass ()
    .deriveClass <WideVector, Vector> ("WideVector")
    .addConstructor <void (*) (float, float, float, float)> ()
    .endClass ();

Let's keep the issue open nevertheless, because it should be possible to make it working without the base class registration.

dmitry-t avatar Mar 17 '19 19:03 dmitry-t

There is no need for exposing the base class anymore. Fixed in https://github.com/kunitoki/LuaBridge3/commit/e62b12c07e1645c06755a646d54c955a3affe7ce

kunitoki avatar Nov 23 '20 22:11 kunitoki