When using Rcpp, I get "error: no viable overloaded '='. How can I resolve this? - rcpp

I am using Rcpp, and I'm struggling to resolve the "error: no viable overloaded '='. I believe that I'm using the correct set-up, where I load the R package mvtnorm, which contains the rmvnorm function.
R script:
library(mvtnorm)
Rcpp::sourceCpp("algorithm.cpp")
algorithm.cpp:
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
#include <RcppArmadilloExtensions/sample.h>
using namespace Rcpp;
using namespace arma;
arma::colvec algorithm() {
Environment myEnv = Environment::global_env();
Rcpp::Function rmvnorm = myEnv["rmvnorm"];
int p = 3;
arma::colvec test = arma::zeros(p);
test = rmvnorm(1, arma::zeros(p), arma::eye(p, p));
return test;
}
Error from VSCode:
error: no viable overloaded '='
test = rmvnorm(1, arma::zeros(p), arma::eye(p, p));
Then, I get the aforementioned error. I am found numerous posts that describe the same error, but none of the proposed solutions seem applicable; moreover, this error appears to be thrown for many reasons.
I tried the code provided in my post. I also referenced this article, which did not resolve my issue.

Related

How do i log errors while writing custom varnish module?

I am learning varnish and about extending the varnish vmod with inline c code. And I am starting it with writing my own custom varnish module. I want to log errors and failure from my custom module. How do i achieve that?
I have options to choose from various logging libraries available for C. But i want to check if there is any inbuilt varnish library to make use of it. Below is my sample code of a vmod c file.
#include "vrt.h"
#include "cache/cache.h"
#include "vcc_if.h"
#include <jansson.h>
#define JSON_ERROR "-1"
#define JSON_LOC "/etc/example.json"
VCL_STRING
vmod_validate_mymod(VRT_CTX) {
(void) ctx;
char *return_code = "0";
json_t *jobj;
json_error_t error;
jobj = json_load_file(JSON_LOC,0,&error);
if (!jobj) {
// error log here
return JSON_ERROR;
}
return return_code;
}
I want en error log line to be added in a cutom log file when the the if condition in the code above is true. Please help.
You want VSLb:
VSLb(ctx->vsl, SLT_VCL_Log, "%d", 5);
If you need to build larger string, or need allocations, use the WS_* functions, their allocations are freed at the end of the rquest automatically.
See how std.log() does it: https://github.com/varnishcache/varnish-cache/blob/389d7ba28e0d0e3a2d5c30a959aa517e5166b246/vmod/vmod_std.c#L145-L153

nodejs c++ addon -- unable to node-gyp build using nan module

I am learning how to build C/C++ addons for nodejs.
I could successfully node-gyp configure, node-gyp build and run node index.js on a simple hellow world program. So, my basic setup is working. The below code (copy-paste from official nodejs documentation) is the working version of my C++ code.
//hello.cc
//#include <node.h>
//#include <nan.h>
#include "./node_modules/nan/nan.h"
#include <iostream>
using namespace v8;
namespace demo {
using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Value;
void Method (const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
std::cout << "Executing some stupid func..." << std::endl;
args.GetReturnValue().Set(String::NewFromUtf8(isolate, "world"));
}
void init(Local<Object> exports) {
NODE_SET_METHOD(exports, "hello", Method);
}
NODE_MODULE(addon, init)
} // namespace demo
However, when I use Nan module, and the version of code documented in the nan site, I get compilation errors indicating NanScope is not declared in this scope.
//hello.cc
#include "./node_modules/nan/nan.h"
#include <iostream>
using namespace v8;
NAN_METHOD(Method) {
Nan::NanScope();
NanReturnValue(String::New("world"));
}
void init(Handle<Object> exports) {
exports->Set(NanSymbol("hello"),
FunctionTemplate::New(Method)->GetFunction());
}
NODE_MODULE(hello, init)
Part of the error output...
make: Entering directory '/home/rvnath/projects/comviva/node-addons/hello-world/build'
CXX(target) Release/obj.target/addon/hello1.o
../hello1.cc: In function ‘Nan::NAN_METHOD_RETURN_TYPE Method(Nan::NAN_METHOD_ARGS_TYPE)’:
../hello1.cc:7:14: error: ‘NanScope’ was not declared in this scope
NanScope();
^
After a bit of googling around, a few sites pointed out that we should be using Nan::Scope, and that the Nan documentation was out-of-date. I tried the change but it still didn't work. It gives an error saying "Scope is not a member of Nan".
I am unable to find, how to use the Nan version correctly. Any help here will be highly appreciated.

catch.hpp error messages with visual studio

Using visual studio 2015 to compile a test code using catch.hpp unit test. I need to write code to interface with serial port and will need to interface with Widnows API and need to include windows.h
But compiler generates complains with error messages below.
Severity Code Description Project File Line Suppression State
Error C2888 'Catch::Colour::Colour(Catch::Colour::Code)': symbol cannot be defined within namespace 'Catch' NMCR_Testing c:\users\ahajmousa\google drive\cto projects\new mc receiver\software\testing\nmcr_testing\nmcr_testing\catch.hpp 7796
Error C2888 'Catch::Colour::Colour(const Catch::Colour &)': symbol cannot be defined within namespace 'Catch' NMCR_Testing c:\users\ahajmousa\google drive\cto projects\new mc ....
......
code:
#include "stdafx.h"
#include <windows.h>
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
TEST_CASE("first Test")
{
REQUIRE(1 == 1);
}
TEST_CASE("2nd Test")
{
REQUIRE(1 == 0);
}
Errors will go away if I do not include widnows.h
Is there a way to get catch.hpp to compile without these errors.

Linux alternative to _NSGetExecutablePath?

Is it possible to side-step _NSGetExecutablePath on Ubuntu Linux in place of a non-Apple specific approach?
I am trying to compile the following code on Ubuntu: https://github.com/Bohdan-Khomtchouk/HeatmapGenerator/blob/master/HeatmapGenerator2_Macintosh_OSX.cxx
As per this prior question that I asked: fatal error: mach-o/dyld.h: No such file or directory, I decided to comment out line 52 and am wondering if there is a general cross-platform (non-Apple specific) way that I can rewrite the code block of line 567 (the _NSGetExecutablePath block) in a manner that is non-Apple specific.
Alen Stojanov's answer to Programmatically retrieving the absolute path of an OS X command-line app and also How do you determine the full path of the currently running executable in go? gave me some ideas on where to start but I want to make certain that I am on the right track here before I go about doing this.
Is there a way to modify _NSGetExecutablePath to be compatible with Ubuntu Linux?
Currently, I am experiencing the following compiler error:
HeatmapGenerator_Macintosh_OSX.cxx:568:13: error: use of undeclared identifier
'_NSGetExecutablePath'
if (_NSGetExecutablePath(path, &size) == 0)
Basic idea how to do it in a way that should be portable across POSIX systems:
#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
static char *path;
const char *appPath(void)
{
return path;
}
static void cleanup()
{
free(path);
}
int main(int argc, char **argv)
{
path = realpath(argv[0], 0);
if (!path)
{
perror("realpath");
return 1;
}
atexit(&cleanup);
printf("App path: %s\n", appPath());
return 0;
}
You can define an own module for it, just pass it argv[0] and export the appPath() function from a header.
edit: replaced exported variable by accessor method

Error : Expected a declaration

I am writing a code in Visual C++ to access serial port.
Code is given below:-
#include<stdio.h>
#include<cstring>
#include<string.h>
#include<conio.h>
#include<iostream>
using namespace std;
//#include "stdafx.h"
#ifndef __CAPSTONE_CROSS_SERIAL_PORT__
#define __CAPSTONE_CROSS_SERIAL_PORT__
HANDLE hSerial= CreateFile(L"COM1", GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
if(hSerial==INVALID_HANDLE_VALUE)
{
if(GetLastError()==ERROR_FILE_NOT_FOUND){
//serial port does not exist. Inform user.
}
//some other error occurred. Inform user.
}
In the above code I am getting error at if in line
if(hserial==INVALID_HANDLE_VALUE)
Error is given below:-
Error:expected a declaration
I am getting same error at both braces } at the end of if statement
I want to know why I am getting this error and how to resolve it
I think you may want to read this. The problem is you are trying to use an if statement at namespace scope (global namespace) where only a declaration is valid.
You will need to wrap your logic in a function of some kind.
void mySuperCoolFunction()
{
if(hSerial==INVALID_HANDLE_VALUE)
{
if(GetLastError()==ERROR_FILE_NOT_FOUND)
{
//serial port does not exist. Inform user.
}
//some other error occurred. Inform user.
}
}

Resources