ITK build Error in itk::ShapedNeighborhoodIterator

Hello all,

We have created a DLL project to perform pre-processing operation and morphological operation on dicom data.
After that, we wrote an application project to use this generated DLL to perform all operations. In that process, I am getting below error.
Error C2516 ‘itk::ShapedNeighborhoodIterator<TImage,TBoundaryCondition>::ConstIterator’: is not a legal base class

I build ITK5.3.0 with visual studio 2019. Can you please suggest, how to resolve this issue.

1 Like

You will need to provide the code fragment which triggers the error.

Seeing the same error with ITK 5.3.0 release:

1>C:\itk\src-5.3.0\Modules\Core\Common\include\itkShapedNeighborhoodIterator.h(183,1): error C2516: 'itk::ShapedNeighborhoodIterator<TImage,TBoundaryCondition>::ConstIterator': is not a legal base class
1>C:\itk\src-5.3.0\Modules\Core\Common\include\itkShapedNeighborhoodIterator.h(171,3): message : see declaration of 'itk::ShapedNeighborhoodIterator<TImage,TBoundaryCondition>::ConstIterator'
1>C:\itk\src-5.3.0\Modules\Core\Common\include\itkShapedNeighborhoodIterator.h(209,1): message : see reference to class template instantiation 'itk::ShapedNeighborhoodIterator<TImage,TBoundaryCondition>::Iterator' being compiled
1>C:\itk\src-5.3.0\Modules\Core\Common\include\itkShapedNeighborhoodIterator.h(265,2): message : see reference to class template instantiation 'itk::ShapedNeighborhoodIterator<TImage,TBoundaryCondition>' being compiled

I’m using Visual Studio 2022 to compile, and I’m not using the ShapedNeighborhoodIterator directly.

I get the error in the compilation of several files in our larger project (which compiles perfectly fine with ITK 5.2.x); I have tried hard (but have failed so far) in extracting a minimal example where this error occurs; I suppose it might come from a conflict resulting from pulling in a bunch of different ITK header files in the wrong order?

I do not even understand what the error means exactly - Microsoft Learn suggests this happens for example when the thing given as base class is not a class, or only forward-declared using typedef? I suppose in this case it would be some problem with template resolution?

Any hints on how I could narrow down where exactly the error is coming from? I have experimented with the “ShowIncludes” feature in Visual Studio, but that just shows me that for our large project, I get above error in between

1>Note: including file:         C:\itk\src-5.3.0\Modules\Core\Common\include\itkConstShapedNeighborhoodIterator.hxx

and

1>Note: including file:        C:\itk\src-5.3.0\Modules\Core\Common\include\itkShapedNeighborhoodIterator.hxx

while for the minimal example using the same filter pulling in the include (e.g. itkMorphologicalWatershedImageFilter, itkSignedMaurerDistanceMapImageFilter, itkBinaryFillholeImageFilter, …), I don’t get that error…

Might this issue be related:

?

However, with the given code I cannot reproduce the issue.

@Niels_Dekker have you encountered something similar?

@dzenanz I have the same issue @Niels_Dekker

@Ahmed_Saleh_Tolba Do you by any chance have a “minimal” example with which the issue can be reproduced?

@codeling

#include “itkConnectedComponentImageFilter.h”
#include “itkRelabelComponentImageFilter.h”

void main(void)
{

}

2 Likes

@codeling

2 Likes

Thanks Ahmed, that’s very helpful. Honestly I still don’t have a clue. It looks like a compiler bug to me. Does it only occur with Visual Studio 2022? Not with Visual Studio 2019?

Do I understand correctly that it already occurs with the following two include’s?

#include “itkConnectedComponentImageFilter.h”
#include “itkRelabelComponentImageFilter.h”

void main(void)
{
}

I still need to install VS2022 myself so until then I guess I cannot reproduce it now. Unless it may be minimized to a code snippet that would fit into https://godbolt.org/ without external #include’s

@Niels_Dekker Yes it happens only while including those two header files.

Thanks! How did you build ITK? Any specific CMake configuration flags?

@Niels_Dekker Not really, but what’s interesting is include those two headers files in your examples projects, no errors occur. Just in an Empty Project.

1 Like

Can you please show us the compiler options used to compile your Project1? You can do so by adjusting the Project Properties, C/C++ ==> General ==> Suppress Startup Banner No ! Then recompile and copy the Build output from the Output window.

By the way, does this error occur with a Release build as well?

1 Like

cl /c /I"C:\work\ots\ITKN\ITK-CPU\include\ITK-5.3" /ZI /JMC /W3 /WX- /diagnostics:column /sdl /Od /D _DEBUG /D _CONSOLE /D _UNICODE /D UNICODE /Gm- /EHsc /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Zc:inline /std:c++17 /permissive- /Fo"x64\Debug\" /Fd"x64\Debug\vc143.pdb" /external:W3 /Gd /TP /FC /errorReport:prompt Source.cpp

Yes it does occur in release build
@Niels_Dekker

2 Likes

Which particular example projects do you mean? I guess those example projects are created by CMake, whereas your Project1 project file (.vcxproj) is generated directly by Visual Studio. Maybe that could explain something…

BTW, I see your compile options include the option /std:c++17. ITK still assumes C++14 (even though it may compile with C++17, I’m not sure). Would the error go away when “downgrading” your Project1 to C++14? By Project Properties, C/C++ ==> Language ==> C++ Language Standard: ISO C++14 Standard

1 Like

Update: Looks like I reproduced the error locally, using just Visual Studio 2019 with C++14!!!

Can you please check if this pull request would fix the error for you as well?

2 Likes

Looks like the error appears when compiling in Visual C++ “Conformance mode”, /permissive- (mind the minus sign). The following piece of code triggers the error with /permissive-, just using Godbolt’s Compiler Explorer at Compiler Explorer

template <typename T>
class ConstShapedNeighborhoodIterator
{
public:
  struct ConstIterator { };
};

template <typename T>
class  ShapedNeighborhoodIterator
  : public ConstShapedNeighborhoodIterator<T>
{
public:
  using typename ConstShapedNeighborhoodIterator<T>::ConstIterator;

  struct Iterator : public ConstIterator { };
};
2 Likes

I can confirm those findings:

  • I can reproduce the error with the simple example above if I switch on /permissive- mode (which we have configured in our project as well, where I saw the error originally); (EDIT: on further tests, I can even reproduce it with only #include "itkConnectedComponentImageFilter.h")

  • and the errors go away with the fix provided above

Thanks!

2 Likes

Please check the bug report I submitted to Microsoft on this compile error: Class template gets undeserved error C2516: “is not a legal base class” from /permissive- (“Conformance mode”) And please upvote the bug report!

2 Likes