No matching function for call to 'init_pair' - c++

I use termux with clang. I've tried compiling the following code in clang but it outputs an error, mentioned in the title.
Here is the code.
#include <ncurses.h>
#include <string>
using namespace std;
int main(int argc, char** argv) {
initscr();
start_color();
init_pair(1,argv[1],argv[2]);
attron(COLOR_PAIR(1));
for (int i = 3; i < argc; ++i) {
printw("%s",argv[i]);
attroff(COLOR_PAIR(1));
}
refresh();
}

The actual error message tells you what the problem is:
> clang -c foo.cc
foo.cc:8:1: error: no matching function for call to 'init_pair'
init_pair(1,argv[1],argv[2]);
^~~~~~~~~
/usr/include/curses.h:648:28: note: candidate function not viable: no known
conversion from 'char *' to 'short' for 2nd argument; dereference the
argument with *
extern NCURSES_EXPORT(int) init_pair (NCURSES_PAIRS_T,NCURSES_COLOR_T,NC...
^
1 error generated.
The init_pair function uses short-integer parameters (not a char*). You could make it compile by converting those char*'s to integer, e.g.,
> diff -u foo.cc.orig foo.cc
--- foo.cc.orig 2020-09-21 17:35:04.000000000 -0400
+++ foo.cc 2020-09-21 17:36:42.000000000 -0400
## -1,11 +1,12 ##
#include <ncurses.h>
+#include <stdlib.h>
#include <string>
using namespace std;
int main(int argc, char** argv) {
initscr();
start_color();
-init_pair(1,argv[1],argv[2]);
+init_pair(1,atoi(argv[1]),atoi(argv[2]));
attron(COLOR_PAIR(1));
for (int i = 3; i < argc; ++i) {
printw("%s",argv[i]);
(though that's just a quick fix).

Related

Why I get "mpir_ui" was not declared in this scope?

after installing mpir-3.0 on fedora 31. Now I try to build project:
#include <stdio.h>
#include <gmp.h>
#include <mpir.h>
#include <mpfr.h>
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
mpf_t a; //mpir float variable
mpf_init(a); //initialise a
mpir_ui two = 2; //mpir unsigned integer variable
FILE* stream; //file type pointer to output on standard output (console)
mpf_init_set_ui (a, 2); //set value of a to 2
mpf_out_str (stream, 10, 2, a); //output value of a
cout << "\nMPIR working" << "\n" ;
}
But when I compile it I get this error:
‘mpir_ui’ was not declared in this scope; did you mean ‘mpfr_ai’?|
I've used the flags:
-lmpir -lmpfr -lgmp

How to convert a command line argument to an int?

Im trying to convert the command line argument(*argv[]) to an integer using the atoi function
int main(int argc, char *argv[]) {
This is my attempt
#include <iostream>
#include <sstream>
#include <string>
#include <cstdlib>
#include <conio.h>
using namespace std;
int main(int argc, char *argv[]) {
int x = 0;
for ( x=0; x < argc; x++ )
{
int x = atoi(argv[1]);
cout << x;
}
return 0;
}
However this returns 0 and im unsure why. Thankyou
It's hard to say having the arguments you pass to your program, but there are few problems here.
Your loop goes from 0 to argc, but your inside your loop you always use argv[1], if you didn't pass any arguments you're going out of bounds, because argv[0] is always the path to your executable.
atoi is a function from C, and when it fails to parse it's argument as an int, it returns 0, replace it with std::stoi, and you will get and execption if the conversion failed. You can catch this exception with try/catch, and then check the string that you tried to convert to int.
Well, this
#include <iostream>
#include <sstream>
#include <string>
#include <cstdlib>
#include <conio.h>
using namespace std;
int main(int argc, char* argv[]) {
int x = 0;
for (x = 0; x < argc; x++)
{
cout << argv[x];
}
return 0;
}
just prints the path to the .exe, the path is a string, it has no numbers. And as I understood from my "research" about command line arguments, you need to use your program through a command line, a terminal, to initialise the argv argument.
Link : https://www.tutorialspoint.com/cprogramming/c_command_line_arguments.htm
Also, as I understood at least, the argv[0] is always the path of the .exe
I hope I will be of some help, if I am mistaken at something, pls tell me where and I will correct my self by editing the answer

Unable to pass the arguments to R function from cpp code

My cpp source code:
#include <iostream>
using namespace std;
int main(int argc, char **argv) {
cout<<"Before"<<endl;
system("Rscript /Desktop/R_TENS/rtest.R");
return 0;
}
rtest.R
rtest = function(input ,output){
a <- input
b<- output
outpath<-a*b
print(a*b)
return(outpath)
}
Compile: g++ name.cpp
This creates a executable file. But how the parameters (eg:2,3) is could be passed?
./a.out 2 3 is not working. Expected result for this input is 6.

Code blocks isn't displaying the program arguments properly

My code is as follows in c:
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char *argv[])
{
int i;
for (i=0;i<argc;i++)
{
printf("Hello world! The arguments are %d, argc is %d and the string is %s\n",argc,i,argv);
}
return 0;
}
I'm unable to see arguments properly in the output. It sort of comes encrypted.
I went to projects-> set program arguments. It's not working though. Please help?
You are printing a pointer to pointer, as your compiler is telling you:
test.c:12:9: warning: format ‘%s’ expects argument of type ‘char *’, but argument 4 has type ‘char **’ [-Wformat=]
printf("Hello world! The arguments are %d, argc is %d and the string is %s\n",argc,i,argv);
^
argv is an array of pointers and you want to print the string pointed by each item of that array:
Corrected code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char *argv[])
{
int i;
for (i=0;i<argc;i++)
{
printf("Hello world! The arguments are %d, argc is %d and the string is %s\n",argc,i,argv[i]);
}
return 0;
}

Error make:*** [unit_test] in googleTest

I have a project named Movies and I run some tests from googletest on it. I included the whole project and started running tests:
#include "gtest/gtest.h"
#include "Counter.h"
#include <iostream>
using namespace std;
TEST(CodeTest, failTest)
{
Counter c;
EXPECT_EQ( 7, c.getValue() );
}
TEST(CodeTest, plusEqualsConstructor)
{
Counter f,g;
f=g+1;
EXPECT_FALSE(f==g);
}
and my main program is:
#include "gtest/gtest.h"
using namespace std;
int main(int argc, char** argv)
{
testing::InitGoogleTest(&argc, argv);
RUN_ALL_TESTS();
return 0;
};
at first it worked as expected and then it gave me the following weird error:
make:*** [unit_test] Error 1
does anyone know what the problem can be? Thank you !!