-3

I've been experiencing a linking issue on a project, and I've found a workaround I can't really explain.

I had this kind of class and I was getting an unresolved external symbol on getType<>(), which was correctly imported from another lib.

MyClass( CustomType& iType = getType<OtherClass>() ) 
{...}

The workaround found is a little less reusable, but still satisfies conviniently my use case for now. Same linker properties, etc.

MyClass( ) 
: mType( getType<OtherClass>() ) 
{...}
  • What difference does it make to set a member variable with a default argument value or through the initializer list?
  • Is there a difference of scope? e.g. could it be because otherClass and getType<>() exist in different namespaces?

EDIT: Declaration of getType:

template<typename type>
CustomType* getType() {
  static CustomType* oType = getType(typeid(type));
  return oType;
}

Linker error:

MyClass.obj : error LNK2019: unresolved external symbol "public: class CustomType * __thiscall Metadata::getType<class OtherClass>(void)" ...
VincentDM
  • 469
  • 6
  • 17
  • You did implement `getType<>()` in the header? –  Nov 15 '17 at 14:07
  • @manni66 yes, in an included header – VincentDM Nov 15 '17 at 14:10
  • 2
    Show code, show the meaasge. –  Nov 15 '17 at 14:12
  • 2
    "CustomType& iType = getType..." is it a typo, or are you really binding a CustomType& to a CustomeType* ? if yes, what's CustomType ? – Massimiliano Janes Nov 15 '17 at 14:26
  • `Metadata::` ?? –  Nov 15 '17 at 14:28
  • `Metadata` is the namespace where `getType` is declared – VincentDM Nov 15 '17 at 14:37
  • there is no typo, this is actually how the code is – VincentDM Nov 15 '17 at 14:38
  • _there is no typo, this is actually how the code is_ that will **not** result in in linker error but in a copiler error. You do not show the relevant code. You are on your own. –  Nov 15 '17 at 14:43
  • alright, sorry for the disturbance. thank you both for your interest and have a good day – VincentDM Nov 15 '17 at 14:46
  • again, what's CustomType ? as of now, the only explanation I see is that CustomType has a non explicit constructor taking CustomType* and either CustomType is an alias for some const T or you're compiling with an old version of vc++ that allows binding lvalue references to rvalues ... either way, I don't see how this relates to the linker error though. Can you create an [mcve] ? – Massimiliano Janes Nov 15 '17 at 14:50
  • @Massimiliano I'll do that in a near future, it's really bugging me. Also this project does use an older compiler (VS2005) – VincentDM Nov 15 '17 at 14:53

1 Answers1

0

Default arguments are only "exectuted" at call site: so for

MyClass(CustomType& iType = getType<OtherClass>())

You only need declaration of getType<OtherClass>().

Instantiation would be done only when you use something like:

MyClass myClass{}; // MyClass myClass{getType<OtherClass>()}

I assume that definition of getType<OtherClass> is inaccessible. (you might have implementation of template inside cpp file)

For

MyClass() : mType(getType<OtherClass>()) {}

getType<OtherClass> is instantiated.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • ... but the declaration should still be valid ( but this could be explained by a converting ctor and older vc++ allowing rvalue to lvalue reference binding, see comments ); moreover, if getType is not instantiated why the linker complains ? isn't there a contradiction here ? – Massimiliano Janes Nov 15 '17 at 15:56
  • @MassimilianoJanes: Does the code split in several projects ? is definition of `getType()` is in header ? That might explain the link error. – Jarod42 Nov 15 '17 at 16:56