C++ 在OpenCL库中找不到cl::Error类

C++ 在OpenCL库中找不到cl::Error类,c++,error-handling,opencl,C++,Error Handling,Opencl,我在一些代码中看到,在OpenCL库中,有一个名为cl::Error的类,通过它可以捕获OpenCL代码中的错误和错误类型。但是当我在代码中添加时,就像这样 #include <CL/cl.hpp> #include <fstream> #include <iostream> #include <cassert> #include <exception> #define __CL_ENABLE_EXCEPTIONS int main(

我在一些代码中看到,在OpenCL库中,有一个名为
cl::Error
的类,通过它可以捕获
OpenCL
代码中的错误和错误类型。但是当我在代码中添加时,就像这样

#include <CL/cl.hpp>
#include <fstream>
#include <iostream>
#include <cassert>
#include <exception>
#define __CL_ENABLE_EXCEPTIONS

int main()
{
  std::vector<cl::Platform> platforms;
  cl::Platform::get(&platforms);

  assert(platforms.size() > 0);

  auto myPlatform = platforms.front();
  std::cout << "Using platform: " << myPlatform.getInfo<CL_PLATFORM_NAME>() << std::endl;

  std::vector<cl::Device> devices;
  myPlatform.getDevices(CL_DEVICE_TYPE_GPU, &devices);

  auto myDevice = devices.front();
  std::cout<< "Using device: "<< myDevice.getInfo<CL_DEVICE_NAME>() << std::endl;

  std::ifstream helloworldfile("helloWorldKernel.cl");
  std::string src(std::istreambuf_iterator<char>(helloworldfile), (std::istreambuf_iterator<char>()));

  cl::Program::Sources source(1,std::make_pair(src.c_str(), src.length() + 1));

  cl::Context context(myDevice);
  cl::Program program(context,source);

  cl::CommandQueue queue(context,myDevice);

  try
  {
    program.build("-cl-std=CL1.2");
  } catch(cl::Error& e)
  {
    std::cout << e.what() << std::cout;
  }

  int err;
  int szChar = 16;
  char  buf[szChar];

  cl::Buffer memBuf(context, CL_MEM_READ_WRITE, sizeof(char) * szChar);

  cl::Kernel kernel(program, "helloWorld", &err);

  if(err != CL_SUCCESS)
  {
    std::cout<<" Error in creating kernel, error: "<< err << std::endl;
    exit(1);
  } 


  kernel.setArg(0,memBuf);

  queue.enqueueTask(kernel);

  err =  queue.enqueueReadBuffer(memBuf,CL_TRUE, 0, sizeof(buf), buf);

  if(err != CL_SUCCESS)
  {
    std::cout<<" Error in reading from device, error: "<< err << std::endl;
    exit(1);
  }

  std::cout << buf;
  std::cin.get();

  return 0;

}

我想知道这个类是否存在,或者至少存在于OpenCL库的特定版本中,如果存在,应该如何调用它。

您需要定义
\uu CL\u启用\u异常
或者如果您使用的是最新的
\include
您需要
CL\u启用\u异常

您需要定义
\uu CL\u启用\u异常
,或者如果您使用的是最新的
\include
CL\u ENABLE\u EXCEPTIONS

中,通过
\define\u CL\u ENABLE\u EXCEPTIONS
,我再次得到了相同的错误。没有任何更改。请编辑您的问题以准确显示您尝试的内容。我使问题变得简单,但现在我上传了完整的示例代码,其中包含了我提到的错误。您需要在包含opencl之前进行定义,理想情况下将其放入编译器设置中。它是有效的,但是你能告诉我为什么我应该在包含之前定义它吗?通过
中的
\define\CL\u ENABLE\u EXCEPTIONS
,我再次得到了相同的错误。没有任何更改。请编辑您的问题以准确显示您尝试的内容。我使问题变得简单,但现在我上传了完整的示例代码,其中包含了我提到的错误。您需要在包含opencl之前进行定义,理想情况下将其放入编译器设置中。它是有效的,但是你能告诉我为什么我应该在包含之前定义它吗。
helloWorld.cc:46:12: error: expected type-specifier
   } catch (cl::Error& e)
            ^
helloWorld.cc:46:21: error: expected unqualified-id before ‘&’ token
   } catch (cl::Error& e)
                     ^
helloWorld.cc:46:21: error: expected ‘)’ before ‘&’ token
helloWorld.cc:46:21: error: expected ‘{’ before ‘&’ token
helloWorld.cc:46:23: error: ‘e’ was not declared in this scope
   } catch (cl::Error& e)
                       ^
helloWorld.cc:46:24: error: expected ‘;’ before ‘)’ token
   } catch (cl::Error& e)