mardi 28 avril 2020

Terminate called after throwing an instance of 'range error' what(): 'Range Error' -1?

#include"std_lib_facilities.h"
int main()
{
  vector<double>numbers;
  cout<<"Enter any two numbers:\n";
  double two_numbers;
  //loop
  while(cin>>two_numbers){
    numbers.push_back(two_numbers);
    double vector_size = numbers.size();
    double two = 2;
    double formula_equal = 1.0/100;
    double od_ev = fmod(vector_size , two);
    //checking the conditions
    if(od_ev == 0)
      if(numbers[vector_size-1] > numbers[vector_size - 2])
        cout<<"The larger value is: "<<numbers[vector_size - 1]<<'\n'
            <<"The smaller value is: "<<numbers[vector_size - 2]<<'\n';
        if(numbers[vector_size-1] - numbers[vector_size - 2] < formula_equal)
          cout<<"These numbers are almost equal.";

      else if(numbers[vector_size-1] < numbers[vector_size - 2])
        cout<<"The larger value is: "<<numbers[vector_size - 2]<<'\n'
            <<"The smaller value is: "<<numbers[vector_size - 1]<<'\n';
        if(numbers[vector_size-2] - numbers[vector_size - 1] < formula_equal)
          cout<<"these numbers are almost equal.";

      else if(numbers[vector_size-1] == numbers[vector_size - 2])
        cout<<numbers[vector_size-1]<<" is equal to  "<<numbers[vector_size - 2]<<'\n';

      }
  }

I don't know why the compiler is showing such error I have searched a lot in google but I don't find anything related to this code please help me with this code.....I don't find any error but I am just a beginner....

OpenCL2 work_group_reduce_add float incorrect output

I'm learning opencl, but in one of my tests I tried to use work_group_reduce_add to add 3 floating values of an array (1.5f, 1.5f, 1.5f), the result I expected was 4.5f the most was 4.0f. And when I try a matrix with 2 values (1.5f, 1.0f), the result is the expected 2.5f, but when I try with more values from the third element of the matrix, the floating values start to be treated as integers. My code is just below.

std::vector<cl::Platform> plataforms;
cl::Platform::get(&plataforms);
std::vector<cl::Device> devices;
plataforms.front().getDevices(CL_DEVICE_TYPE_GPU, &devices);

cl::Context context(devices.front());

const char* code = "\
    __kernel void test(__global float* a, __global float* b) {\
        b[0] = work_group_reduce_add(a[get_global_id(0)]);\
    }\
";

cl::Program::Sources src(1, std::make_pair(code, strlen(code)));
cl::Program program(context, src);
program.build("-cl-std=CL2.0");
cl::CommandQueue queue_default(context, devices.front(), CL_QUEUE_ON_DEVICE | CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE | CL_QUEUE_ON_DEVICE_DEFAULT);
cl::CommandQueue queue(context, devices.front());

cl::Kernel kernel_test(program, "test");

float a[3] = { 1.5f, 1.5f, 1.5f };
float b[1] = { 0.0f };

cl::Buffer _a(context, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, sizeof(float) * 3, a);
cl::Buffer _b(context, CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR, sizeof(float), b);

kernel_test.setArg(0, _a);
kernel_test.setArg(1, _b);

queue.enqueueNDRangeKernel(kernel_test, cl::NDRange(), cl::NDRange(3), cl::NDRange(3));
queue.finish();

Using `extern template` with third-party header-only library

I am using the glm library, which is a header-only collection of math utilities intended for 3D graphics. By using -ftime-trace on Clang and ClangBuildAnalyzer, I've noticed that a lot of time is being spent instantiating glm types:

**** Templates that took longest to instantiate:
 16872 ms: glm::vec<4, signed char, glm::packed_highp> (78 times, avg 216 ms)
 15675 ms: glm::vec<4, unsigned char, glm::packed_highp> (78 times, avg 200 ms)
 15578 ms: glm::vec<4, float, glm::packed_highp> (78 times, avg 199 ms)

...

So, I decided to create a wrapper header/source pair for glm, and use extern template to avoid unnecessary instantiations:

// glmwrapper.h

#pragma once

#include <glm.hpp>

extern template struct glm::vec<4, signed char, glm::packed_highp>;
extern template struct glm::vec<4, unsigned char, glm::packed_highp>;
extern template struct glm::vec<4, float, glm::packed_highp>;
// glmwrapper.cpp

template struct glm::vec<4, signed char, glm::packed_highp>;
template struct glm::vec<4, unsigned char, glm::packed_highp>;
template struct glm::vec<4, float, glm::packed_highp>;

Now, in my project, instead of including <glm.hpp>, I include "glmwrapper.h" instead. Unfortunately, that did not change anything. Using -ftime-trace and ClangBuildAnalyzer again reports the same number of instantiations.

I suspect that this is because #include <glm.hpp> does actually end up including the template definition, and at that point the subsequent extern template declarations are just redundant.

Is there a way to achieve what I want without modifying the glm library?


In pseudocode, I kinda want something like this:

// glmwrapper.h (psuedocode)

#pragma once

#include <glm.hpp>

// Make definition of the templates unavailable:
undefine template struct glm::vec<4, signed char, glm::packed_highp>;
undefine template struct glm::vec<4, unsigned char, glm::packed_highp>;
undefine template struct glm::vec<4, float, glm::packed_highp>;

// Make declaration of the templates available:
extern template struct glm::vec<4, signed char, glm::packed_highp>;
extern template struct glm::vec<4, unsigned char, glm::packed_highp>;
extern template struct glm::vec<4, float, glm::packed_highp>;
// glmwrapper.cpp (psuedocode)

// Define templates only in the `.cpp`, not in the header:
template struct glm::vec<4, signed char, glm::packed_highp>;
template struct glm::vec<4, unsigned char, glm::packed_highp>;
template struct glm::vec<4, float, glm::packed_highp>;

User defined call back functions as parameter in ALT COM dll

I have created a c++ console application. Now I want to build a ALT COM object so that I can use the C++ code in other platforms(eg. C#). One of the functions RegisterListener(parameter1, parameter2) takes two call back (out) functions as parameters. I have seen videos/documentations but all of them build COM dll where parameters are built in data types(int, long, bool, HRESULT, etc). How can I use user defined callback(out) functions as parameter.
Example:

sigin(int,string);
signout(string);
RegisterListener( (sigin)(int,string), signout(string) );

I want to allow the function RegisterListener to be called by other programs using the COM dll How can I do this/Are there any tutorials regarding this? Note: I have never wrote any COM objectsdll before.

Getting error can not inherit constructors from indirect base in c++17 compiler

#include <iostream> 
#include <chrono>    

class A{
    public:
        A(){} // default constr

};

class B:public A{
    public:
    using A::A;
    protected:

};

class C:public B{
    public:
        using A::A; //scope resolution 

        int f1(A a);// dummy function

};

int main () 
{ 
    return 0; 
} 

// Above program is working fine with C++ and C++14 but its giving error with C++17 // why and how can it be resolved?

Debugger does not stop at breakpoint and displays "Frames are not available" and "Variables are not available" (CLion)

System

Latest macOS, CLion (2018)

Problem

I'm trying to solve an exercise for uni and once I download the project I put it into my HDD I always used and once opened a message from IDE (never saw it before) shows up saying something related to case sensitive problems. Clicking on it the website says it is not really a problem so I decide to ignore it, until now. I need to debug the project as a part of the exercise and when I try nothing shows up on the debugger even if there are 3 breakpoints.

What I tried

I tried to download a fresh project and the problem does not go away, on the IDE site there are not direct solutions and the documentation for "Debugger" is generic. I tried to debug other project and it doesn't work anywhere.

What should I do?

Now, the project is due to next Monday, what should I do? Keep in mind that I'm a complete noob, it was kinda the first time for me working with the debugger, the real first time in class everything worked fine (a couple months ago). The problem seems to be related to the HDD (file system: NTFS, I use it via Tuxera) but that doesn't make sense... I had projects there in the past.

Thanks to everyone will try to help!

How switch char* on std::string in my implementation of func?

How convert my function from const char* to string? I need it withot templates. Just rebuild my function pattern_founder(const char*, const char*) on pattern_founder(std::string, std::string). I trying to do it with iterators, but something went wrong. Thank you.

 static bool pattern_founder(const char* file_path_ptr, const char* pattern) noexcept
    {
        const char* supp_file_path_ptr = nullptr;
        const char* supp_pattern = nullptr;
        while (true) 
            if (*pattern == '*') {
                supp_file_path_ptr = file_path_ptr;
                supp_pattern = ++pattern;
            }
            else if (!*file_path_ptr)
                return !*pattern;
            else if (*file_path_ptr == *pattern || *pattern == '?') {
                ++file_path_ptr;
                ++pattern;
            }
            else if (supp_file_path_ptr) {
                file_path_ptr = ++supp_file_path_ptr;
                pattern = supp_pattern;
            }
            else 
                return false;
    }