Error : Expected a declaration - visual-c++

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.
}
}

Related

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

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.

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

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.

Broken code generation for out function parameters

I made my own interface, simplistic version looks like this:
#ifndef _FOO_IDL_
#define _FOO_IDL_
module FOO {
typedef unsigned long Bar;
interface FOOInterface {
void getBar(out FOO::Bar b);
};
};
#endif
After that I made "REDHAWK IDL Project", used that IDL, compiled, installed.
Then I made Redhawk component, added output port and used that interface on it, did code generation. During compilation I got error:
port_impl.h:26:29: error: expected ‘,’ or ‘...’ before ‘&&’ token
void getBar(FOO::Bar&& b);
It looks like code generator adds excessive ampersand. What could I do about it?
Thank you.

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

Resources