error: expected nested-name-specifier before 'b_arr' - c++

using b_arr = bool [10];
i have written this code in code::blocks on windows. i have seen many other posts on the same error but do not understand what is wrong in my code.
i actually want to use the resulting user-defined data type for a function to return a boolean array created inside the function. i also do not understand how i will do that(return a boolean array). please help.
i am a novice in programming and coding, so it would be nice have some explanations

Related

Transfering v8 types to c++ types (string)

I am making a c++ addon for node.js and I am struggling with passing and getting data. I understood how to transform v8::Number to double, double to v8::Number and int to v8:Number, but I need some more. Mainly, v8::String to std::string and back, v8::Number to int and v8::Array to Array and back. Also it would be great to transfer js objects to some c++ variables, but it is less necessary. Does someone know, how to do that?
P.S. I looked over docs and I found nothing about arrays and objects and only this string a (*v8::String::Utf8Value(args[0]->ToString())) according to strings. But it does not work, I get an error error C2660: v8::Value::ToString: function does not get 0 arguments and error C2512: v8::String::Utf8Value: no suitable default constructor. I do not have any more ideas how to implement that. Can someone help?
And also I tried to do something with returning data from c++. In this way args.GetReturnValue().Set(v8::String::NewFromUtf8(isolate, "world").ToLocalChecked()); it works, but if I make like this:
string s = "world";
args.GetReturnValue().Set(v8::String::NewFromUtf8(isolate, s).ToLocalChecked());
I get an error error C2664: "v8::MaybeLocal<v8::String> v8::String::NewFromUtf8(v8::Isolate *,const char *,v8::NewStringType,int)": cannot convert argument 2 from "std::string" to "const char *"
And I have no idea what is wrong here. Did someone has exprience working with c++ addons and v8 types, How to do that?
For understanding v8::String, the latest version of the docs help: https://v8docs.nodesource.com/node-14.1/d4/d1b/classv8_1_1_string_1_1_utf8_value.html (some signatures have changed a bit since the days of Node 0.8 -- learning how to make sense of the compiler's error messages is highly recommended if you want to develop with C++!).
For understanding std::string, see some C++ documentation, e.g.: https://en.cppreference.com/w/cpp/string/basic_string
If you need an actual example, you can look at V8's samples/process.cc, which has a function ObjectToString that converts any JS object (e.g. a v8::String) to a std::string. It's only two lines!

What is going on with 'gets(stdin)' on the site coderbyte?

Coderbyte is an online coding challenge site (I found it just 2 minutes ago).
The first C++ challenge you are greeted with has a C++ skeleton you need to modify:
#include <iostream>
#include <string>
using namespace std;
int FirstFactorial(int num) {
// Code goes here
return num;
}
int main() {
// Keep this function call here
cout << FirstFactorial(gets(stdin));
return 0;
}
If you are little familiar with C++ the first thing* that pops in your eyes is:
int FirstFactorial(int num);
cout << FirstFactorial(gets(stdin));
So, ok, the code calls gets which is deprecated since C++11 and removed since C++14 which is bad in itself.
But then I realize: gets is of type char*(char*). So it shouldn't accept a FILE* parameter and the result shouldn't be usable in the place of an int parameter, but ... not only it compiles without any warnings or errors, but it runs and actually passes the correct input value to FirstFactorial.
Outside of this particular site, the code doesn't compile (as expected), so what is going on here?
*Actually the first one is using namespace std but that is irrelevant to my issue here.
I'm the founder of Coderbyte and also the guy who created this gets(stdin) hack.
The comments on this post are correct that it is a form of find-and-replace, so let me explain why I did this really quickly.
Back in the day when I first created the site (around 2012), it only supported JavaScript. There was no way to "read in input" in JavaScript running in the browser, and so there would be a function foo(input) and I used the readline() function from Node.js to call it like foo(readline()). Except I was a kid and didn't know better, so I literally just replaced readline() with the input at run-time. So foo(readline()) became foo(2) or foo("hello") which worked fine for JavaScript.
Around 2013/2014 I added more languages and used third-party service to evaluate code online, but it was very difficult to do stdin/stdout with the services I was using, so I stuck with the same silly find-and-replace for languages like Python, Ruby, and eventually C++, C#, etc.
Fast forward to today, I run the code in my own containers, but never updated the way stdin/stdout works because people have gotten used to the weird hack (some people have even posted in forums explaining how to get around it).
I know it is not best practice and it isn't helpful for someone learning a new language to see hacks like this, but the idea was for new programmers to not worry about reading input at all and just focus on writing the algorithm to solve the problem. One common complaint about coding challenge sites years ago was that new programmers would spend a lot of time just figuring out how to read from stdin or read lines from a file, so I wanted new coders to avoid this problem on Coderbyte.
I'll be updating the entire editor page soon along with the default code and stdin reading for languages. Hopefully then C++ programmers will enjoy using Coderbyte more :)
I am intrigued. So, time to put the investigation goggles on and since I don't have access to the compiler or compilation flags I need to get inventive. Also because nothing about this code makes sense it's not a bad idea question every assumption.
First let's check the actual type of gets. I have a little trick for that:
template <class> struct Name;
int main() {
Name<decltype(gets)> n;
// keep this function call here
cout << FirstFactorial(gets(stdin));
return 0;
}
And that looks ... normal:
/tmp/613814454/Main.cpp:16:19: warning: 'gets' is deprecated [-Wdeprecated-declarations]
Name<decltype(gets)> n;
^
/usr/include/stdio.h:638:37: note: 'gets' has been explicitly marked deprecated here
extern char *gets (char *__s) __wur __attribute_deprecated__;
^
/usr/include/x86_64-linux-gnu/sys/cdefs.h:254:51: note: expanded from macro '__attribute_deprecated__'
# define __attribute_deprecated__ __attribute__ ((__deprecated__))
^
/tmp/613814454/Main.cpp:16:26: error: implicit instantiation of undefined template 'Name<char *(char *)>'
Name<decltype(gets)> n;
^
/tmp/613814454/Main.cpp:12:25: note: template is declared here
template <class> struct Name;
^
1 warning and 1 error generated.
gets is marked as deprecated and has the signature char *(char *). But then how is FirstFactorial(gets(stdin)); compiling?
Let's try something else:
int main() {
Name<decltype(gets(stdin))> n;
// keep this function call here
cout << FirstFactorial(gets(stdin));
return 0;
}
Which gives us:
/tmp/286775780/Main.cpp:15:21: error: implicit instantiation of undefined template 'Name<int>'
Name<decltype(8)> n;
^
Finally we are getting something: decltype(8). So the entire gets(stdin) was textually replaced with the input (8).
And the things get weirder. The compiler error continues:
/tmp/596773533/Main.cpp:18:26: error: no matching function for call to 'gets'
cout << FirstFactorial(gets(stdin));
^~~~
/usr/include/stdio.h:638:14: note: candidate function not viable: no known conversion from 'struct _IO_FILE *' to 'char *' for 1st argument
extern char *gets (char *__s) __wur __attribute_deprecated__;
So now we get the expected error for cout << FirstFactorial(gets(stdin));
I checked for a macro and since #undef gets seems to do nothing it looks like it isn't a macro.
But
std::integral_constant<int, gets(stdin)> n;
It compiles.
But
std::integral_constant<int, gets(stdin)> n; // OK
std::integral_constant<int, gets(stdin)> n2; // ERROR wtf??
Doesn't with the expected error at the n2 line.
And again, almost any modification to main makes the line cout << FirstFactorial(gets(stdin)); spit out the expected error.
Moreover the stdin actually seems to be empty.
So I can only conclude and speculate they have a little program that parses the source and tries (poorly) to replace gets(stdin) with the test case input value before actually feeding it into the compiler. If anybody has a better theory or actually knows what they are doing please share!
This is obviously a very bad practice. While researching this I found there is at least a question here (example) about this and because people have no idea that there is a site out there who does this their answer is "don't use gets use ... instead" which is indeed a good advice but only confuses the OP more since any attempt at a valid read from stdin will fail on this site.
TLDR
gets(stdin) is invalid C++. It's a gimmick this particular site uses (for what reasons I cannot figure out). If you want to continue to submit on the site (I am neither endorsing it neither not endorsing it) you have to use this construct that otherwise would not make sense, but be aware that it is brittle. Almost any modifications to main will spit out an error. Outside of this site use normal input reading methods.
I tried the following addition to main in the Coderbyte editor:
std::cout << "gets(stdin)";
Where the mysterious and enigmatic snippet gets(stdin) appears inside a string literal. This shouldn't possibly be transformed by anything, not even the preprocessor, and any C++ programmer should expect this code to print the exact string gets(stdin) to the standard output. And yet we see the following output, when compiled and run on coderbyte:
8
Where the value 8 is taken straight from the convenient 'input' field under the editor.
From this, it's clear that this online editor is performing blind find-and-replace operations on the source code, substitution appearances of gets(stdin) with the user's 'input'. I would personally call this a misuse of the language that's worse than careless preprocessor macros.
In the context of an online coding challenge website, I'm worried by this because it teaches unconventional, non-standard, meaningless, and at least unsafe practices like gets(stdin), and in a manner that can't be repeated on other platforms.
I'm sure it can't be this hard to just use std::cin and just stream input to a program.

How to stringify an Integer in V8?

I'm trying to stringify an integer in v8.
The nearest I've come to success so far is using String::Concat. I tried writing this method (in a node.js 9.11.1 native addon), but it doesn't compile.
void Method(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
const int num = 42;
args.GetReturnValue().Set(
String::Concat(String::NewFromUtf8(isolate, "The num is: "),
Integer::New(isolate, num)));
}
The compile error is:
'=': cannot convert from 'v8::Integer *' to 'v8::String *volatile '
I haven't been able to figure out the right parts of the v8 API to make use of to format an integer into a string. I'm (perhaps obviously) not familiar with the v8 API, and I'm having trouble finding good examples to learn from.
I was going by the answer to this question: How to convert an Integer to a String in V8? although it appears to be stale compared to the modern v8 API. The example appears to be pre-"isolate" for example.
I was under the impression that Concat would accept this because in JavaScript it just coerces the int to a string (that seems to be the gist of that question I referenced). But I guess maybe I have to be explicit about that when coding with v8?
I'm sure it's something simple I'm missing. I'd appreciate someone suggesting a better way.
Try calling ToString on the Integer you just created.
See the function declaration here: https://chromium.googlesource.com/v8/v8/+/6.5.254.41/include/v8.h#2333
The magic where "JavaScript just coerces the int" must be implemented somewhere -- namely, on the C++ side, where all such conversions are done manually ;-)

Cross compiling for arm gcc5 - Proper use of memcpy

I am trying to compile a kernel (uImage) for the S805/S812 SoC from amlogic. I have managed to solve all compilation error except for one, probably because my knowledge of c++ and compilers don't reach far enough. Let me explain the problem, which exist in a file phydev.c :
The line giving the error is :
memcpy(&phydev->name, &dev_para->name, MAX_DEVICE_NAME_LEN*sizeof(char));
These are the (cross)compiler errors:
drivers/amlogic/../../../hardware/amlogic/nand/amlnf/dev/phydev.c: In function ‘amlnand_phydev_init’:
drivers/amlogic/../../../hardware/amlogic/nand/amlnf/dev/phydev.c:1114:10: error: passing argument 1 of ‘memcpy’ discards ‘const’ qualifier from pointer target type [-Werror=discarded-array-qualifiers]
memcpy(&phydev->name, &dev_para->name, MAX_DEVICE_NAME_LEN*sizeof(char));
^
In file included from include/linux/string.h:17:0,
from include/linux/dynamic_debug.h:111,
from include/linux/kernel.h:14,
from include/linux/cache.h:4,
from include/linux/time.h:4,
from include/linux/stat.h:18,
from include/linux/module.h:10,
from drivers/amlogic/../../../hardware/amlogic/nand/amlnf/dev/../include/../include/amlnf_type.h:18,
from drivers/amlogic/../../../hardware/amlogic/nand/amlnf/dev/../include/../include/amlnf_dev.h:4,
from drivers/amlogic/../../../hardware/amlogic/nand/amlnf/dev/../include/phynand.h:4,
from drivers/amlogic/../../../hardware/amlogic/nand/amlnf/dev/phydev.c:14:
/root/Downloads/Beelink3/arm-src-kernel-2015-01-15-321cfb5a46/arch/arm/include/asm/string.h:16:15:
note: expected ‘void *’ but argument is of type ‘const char (*)[16]’
extern void * memcpy(void *, const void *, __kernel_size_t);
^
I have been trying to figure this out for several days, also trying direct assignment (which obviously doesn't work on arrays), using strcpy instead and so on, but this only yields more errors and I can't find the exact root cause. I think I need some kind of cast or intermediate constant void ptr but I have no clue how to fix this.
Could anyone be as kind as to give me advice on this and how to solve it. The memcpy function is defined in arch/arm/include/asm/string.h
But it did not seem a good idea messing with this file.
Many thanks,
Wim
Indeed someone had been smart enough to :
Struct phydev {
const char* name;
...
Removed the const and all was fine (at least the memcpy line. But I could use strcpy or strdup or whatever. It was trying to write to a constant.
After debugging trough all of the other errors I was able to make the U-Boot Image. Haven't tested it yet.
Also, made a lot of functions static instead of copying used static variables into local attributes. Don't know what it's gonna do... (probably crash with some segmentation fault or something:-).
I'll have to wait for my ttl usb adapter now...
Unless anyone know where to find an adb driver for this thing and it supports fw_setenv or something....
Guess it will be pressing the space bar on tty1 once the ttl adapter arrives.
Thanks guys for all your help. If I manage to get Ubuntu on this box I'll post a blog about it :-)
Cheers & thanks
Wim

amp - C++ Assistor

Is it possible to specify parameters to an pointer with something called & in C/C++?
Basically let's say I have a function:
int vba (unsigned long *a, unsigned long *b){.....}
Can I declare non-pointer values to the parameters using this mysterious & call?
I am reading this book and the author is using it to do so, but he doesn't declare what
it is, or what it is in general. I googled "&amp C++ call" and I didn't get any results.
So then he specifies a non ptr assignment using it in anothe function like so:
int vca ()
{
unsigned long c, d;
vba(&c, &d);
//etc ...
}
This is where I am confused, I don't know what just happened here, or what that call does, how did he assign a pointer to a non-pointer like that ... seems quite awesome though I never heard of it before. Anyone mind elaborating on what this is, or how it is possible?
Also when I put this into my code it says in the error log: "Intellisense: identifier "amp is undefined"? This is where I got really lost because the compiler highlights the amp call in blue, so how is it undefined?
That is just HTML encoding for the & character. The website you got this from clearly got something wrong. Hm, I just reread your question and noticed that you're talking about a book. Doesn't really change anything other than make the mistake even more appalling.
Anyway, replace the & with & and you'll be good to go.