Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C2665 error from simple classes in a module

I am trying to wrap my head around modules and classes. For example:

firstFile.cpp

export module ModTest;

export import :Two;

export class FirstClass {
public:
    FirstClass();
    int firstClassFunc();
};

firstFile_impl.cpp

module ModTest;

int FirstClass::firstClassFunc() { return 8; }

FirstClass::FirstClass()
{
    SecondClass sec(this);
}

secondFile.cpp

export module ModTest:Two;

class FirstClass;

export class SecondClass {
public:
    SecondClass(FirstClass *parent){}
    SecondClass(const SecondClass&) noexcept = delete;
    SecondClass(const SecondClass&&) noexcept = delete;
    SecondClass& operator=(const SecondClass&) noexcept = delete;
    SecondClass& operator=(SecondClass&&) noexcept = delete;
};

This compiles just fine, but when I try to use it in a different project:

FirstClass *firstCl = new FirstClass();
    SecondClass sec(firstCl);

I get these errors, that don't make sense to me:

1>C:\ecworks_mep\_WINOHNEDLL\src\mainframe.cpp(358,17): error C2665: 'SecondClass::SecondClass': no overloaded function could convert all the argument types
1>(compiling source file '../src/mainframe.cpp')
1>    C:\ecworks_mep\moduleTest\moduleTest\secondFile.cpp(9,2):
1>    could be 'SecondClass::SecondClass(const SecondClass &&) noexcept'
1>        C:\ecworks_mep\_WINOHNEDLL\src\mainframe.cpp(358,17):
1>        'SecondClass::SecondClass(const SecondClass &&) noexcept': cannot convert argument 1 from 'FirstClass *' to 'const SecondClass &&'
1>            C:\ecworks_mep\_WINOHNEDLL\src\mainframe.cpp(358,18):
1>            Reason: cannot convert from 'FirstClass *' to 'const SecondClass'
1>    C:\ecworks_mep\moduleTest\moduleTest\secondFile.cpp(8,2):
1>    or       'SecondClass::SecondClass(const SecondClass &) noexcept'
1>        C:\ecworks_mep\_WINOHNEDLL\src\mainframe.cpp(358,17):
1>        'SecondClass::SecondClass(const SecondClass &) noexcept': cannot convert argument 1 from 'FirstClass *' to 'const SecondClass &'
1>            C:\ecworks_mep\_WINOHNEDLL\src\mainframe.cpp(358,18):
1>            Reason: cannot convert from 'FirstClass *' to 'const SecondClass'
1>    C:\ecworks_mep\moduleTest\moduleTest\secondFile.cpp(7,2):
1>    or       'SecondClass::SecondClass(FirstClass *)'
1>        C:\ecworks_mep\_WINOHNEDLL\src\mainframe.cpp(358,17):
1>        'SecondClass::SecondClass(FirstClass *)': cannot convert argument 1 from 'FirstClass *' to 'FirstClass *'
1>            C:\ecworks_mep\_WINOHNEDLL\src\mainframe.cpp(358,18):
1>            Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or parenthesized function-style cast
1>    C:\ecworks_mep\_WINOHNEDLL\src\mainframe.cpp(358,17):
1>    while trying to match the argument list '(FirstClass *)'
1>Done building project "mepOhneDll4.vcxproj" -- FAILED.

What´s going on here?!

I tried to explicitly delete the constructor mentioned in the error message, but it appears to just get ignored by the compiler.

like image 447
mael15 Avatar asked Apr 24 '26 21:04

mael15


1 Answers

The declaration of FirstClass in ModTest:Two isn’t exported, so it has module linkage and can’t be exported in the primary interface unit. It would be nicer if the compiler actually diagnosed this (which it ought to be able to do, since it processes the interface partition first).

like image 57
Davis Herring Avatar answered Apr 26 '26 12:04

Davis Herring



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!