ошибка при приведении изображения к разным типам в C ++ - PullRequest
0 голосов
/ 24 апреля 2020

Я пытаюсь использовать следующие предложения для приведения изображения к различным типам (я использую библиотеку ITK 5.1, если это помогает):

  if (outputPixelType == std::string("double"))
  {
    using OutputPixelType = double;
  }
  else
  {
    using OutputPixelType = int;
  }

  using OutputImageType = itk::Image<OutputPixelType, Dimension>;

Я получаю ошибку, подобную этой, с помощью компилятора clang

  error: use of undeclared identifier 'OutputPixelType'; did you mean 'outputPixelType'?
  using OutputImageType = itk::Image<OutputPixelType, Dimension>;
                                     ^~~~~~~~~~~~~~~
                                     outputPixelType

Но если я использую следующие предложения, это работает.

  using OutputPixelType = int;
  using OutputImageType = itk::Image<OutputPixelType, Dimension>;

Почему оператор if не работает? Как это исправить? Спасибо.

Я попробовал следующую команду, используя комментарий Довахина:

  using OutputPixelType = (outputPixelType == std::string("double")) ? double : int16_t;

После компиляции я получил другую ошибку:

error: expected a type
  using OutputPixelType = (outputPixelType == std::string("double")) ? double : int16_t;
                          ^
error: type-id cannot have a name
  using OutputPixelType = (outputPixelType == std::string("double")) ? double : int16_t;
                           ^~~~~~~~~~~~~~~
error: expected ')'
  using OutputPixelType = (outputPixelType == std::string("double")) ? double : int16_t;
                                           ^
note: to match this '('
  using OutputPixelType = (outputPixelType == std::string("double")) ? double : int16_t;
                          ^
error: expected ';' after alias declaration
  using OutputPixelType = (outputPixelType == std::string("double")) ? double : int16_t;

Кажется, что using не принимает ()? A: B оператор.

1 Ответ

1 голос
/ 24 апреля 2020

Вы, вероятно, хотите использовать шаблоны :

template<typename OutputPixelType>
void dostuff() {
   using OutputImageType = itk::Image<OutputPixelType, Dimension>;
   ...
}

void fun(std::string outputPixelType) {
  if (outputPixelType == std::string("double")) {
    dostuff<double>();
  } else {
    dostuff<int>();
  }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...