Cannot convert ‘boost::multiprecision::cpp_int - c++11

Giving this error on compilation:-
no matching function for call to ‘to_string(boost::multiprecision::cpp_int&)’ string s = to_string(i);
#include <boost/lexical_cast.hpp>
#include <boost/multiprecision/cpp_int.hpp>
namespace mp = boost::multiprecision;
using namespace std;
#define int long long int
int32_t main()
{
mp::cpp_int l,i;
for(i=l;i<r;i++)
{
string s = to_string(i);
}
return 0;
}

You're including boost::lexical_cast's header file and boost::to_string. Include the proper header file for boost::to_string, which is "boost/exception/to_string.hpp", or use boost::lexical_cast.

Related

Error E0146 : Too many initializer values C++

I have a school project and I have to use the AM in the Student.h as a char*.The AM have to have numbers in it. I can't understand why what I am doing is not working.
Student.cpp
#include <iostream>
#include <string>
#include "Student.h"
using namespace std;
int main()
{
Student dlg;
dlg.AM[10]={2,1,3,9,0,2,6,6};
}
Student.h
#pragma once
#include <string>
using namespace std;
class Student
{
public:
char *AM[20];
string Name;
unsigned int Semester = 1;
};
If you really need your student number to be a char string, then you need to convert your ints to char* before assigning them to the array.
int main()
{
Student dlg;
int j = 0;
for (auto i : {2,1,3,9,0,2,6,6})
{
auto strInt { std::to_string(i) }; // create a C++ string containing a int
// next copy the internal memory of the C++ string to a read-writable memory buffer
// and assign a pointer to that buffer casted to a char* to the appropriate slot in the array
dlg.AM[j++] = static_cast<char*> (std::memcpy (new char[16], strInt.c_str(), strInt.size()));
}
// test
for (int i = 0; i < 8; i++)
{
cout << dlg.AM[i] << ' ';
}
}
Are you sure the student number should be a char* ?

Why the function don't work on the string?

#include <iostream>
#include <string>
using namespace std;
void chuli(string a)
{
a.erase(0,2);
}
int main()
{
string a = "012345";
a = chuli(a);
cout << a;
}
I am beginner in C++, I want to know why after this function, this string doesn't change. Is this something about the namespace?
The string is passed by value, so your action is applied to a copy of your object.
If you need to modify your value, you need to pass it by pointer or by reference:
void chuli(string &a)
{
a.erase(0,2);
}
void chuli(string *a)
{
a->erase(0,2);
}

Failure of #include<string>?

I am using the following code for a class project, but for some reason the #include string is not working, and the compiler is flagging every declaration using string. What did I do wrong?
#ifndef MEMORY_H
#define MEMORY_H
#include <string>
class Memory
{
private:
string mem[1000];
public:
Memory()
{
for each(string s in mem)
{
s = "nop";
}
};
string get(int loc)
{
return mem[loc];
};
void set(int loc, string input)
{
mem[loc] = input;
}
};
#endif
string is part of the std namespace, instead of string, you need:
std::string
For more on namespaces go here.
Add this after your include statement:
using namespace std;

Convert a char16_t to long/integer, (atoi / atol equivalent)

I want to convert a char16_t to an integer and/or a long.
const char16_t* t = u"12";
long lt = atol( t);
Does such a function not exist?
Try this function: (it's not necessarily the most efficient solution, but it gets the job done if you have no choice (i.e. you have to have a char16_t* as input) and you're sure that you actually have a valid sequence of digits in your char16_t* string)
Run It Online
#include <codecvt>
#include <locale>
#include <string>
int char16_to_int(const char16_t* s16)
{
// https://stackoverflow.com/a/7235204/865719
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
std::string str = convert.to_bytes(s16);
return std::stoi(str);
}
long char16_to_long(const char16_t* s16)
{
// https://stackoverflow.com/a/7235204/865719
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
std::string str = convert.to_bytes(s16);
return std::stol(str);
}
EDIT: I've refactored the solution just a little bit to reduce repetition:
Run It Online
#include <codecvt>
#include <locale>
#include <string>
std::string char16_to_string(const char16_t* s16)
{
// https://stackoverflow.com/a/7235204/865719
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
return convert.to_bytes(s16);
}
long char16_to_long(const char16_t* s16)
{
return stol(char16_to_string(s16));
}
int char16_to_int(const char16_t* s16)
{
return stoi(char16_to_string(s16));
}

How to create proc entry under /proc/driver?

I want to create a file under a /proc/driver directory. I would like to use a macro like proc_root_driver (or something else provided) rather than use "driver/MODULE_NAME" explicitly. I use create_proc_entry :
struct proc_dir_entry *simpleproc_fops_entry;
simpleproc_fops_entry = create_proc_entry(MODULE_NAME, 0400, NULL /* proc_root_dir */);
After googling, I found suggestion to use proc_root_driver, but when I use it, I get the error
proc_root_driver undeclared in this function
And also, proc_root_driver is not available in linux/proc_fs.h.
I have tried to declare structure like this:
struct proc_dir_entry proc_root;
struct proc_dir_entry *proc_root_driver = &proc_root;
The compilation errors gone, but the file didn't appear under /proc/driver or /proc. How can I make create an entry in /proc?
Looking at proc_fs.h, proc_root_driver is defined as :
extern struct proc_dir_entry *proc_root_driver;
so long as CONFIG_PROC_FS is enabled. If you have CONFIG_PROC_FS selected when you configure your kernel, you should be able to use it as you suggested yourself i.e. :
#include <linux/proc_fs.h>
struct proc_dir_entry * procfile
procfile = create_proc_entry("myprocfile", 0400, proc_root_driver);
If this does not work, check that you have CONFIG_PROC_FS set. To make sure, you can compile your source file with the -E option and check that the create_proc_entry call includes a non NULL parameter as the last parameter. If it is NULL, or the call is not there at all, then CONFIG_PROC_FS is not enabled.
/* proc entries for ayyaz */
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/timer.h>
#include <linux/major.h>
#include <linux/fs.h>
#include <linux/err.h>
#include <linux/ioctl.h>
#include <linux/init.h>
#include <linux/proc_fs.h>
#ifdef CONFIG_PROC_FS
/*====================================================================*/
/* Support for /proc/ayyaz */
static struct proc_dir_entry *proc_ayyaz;
DEFINE_MUTEX(ayyaz_table_mutex);
/*====================================================================*/
/* Init code */
static int ayyaz_read_proc (char *page, char **start, off_t off, int count,
int *eof, void *data_unused)
{
int len, l, i;
off_t begin = 0;
mutex_lock(&ayyaz_table_mutex);
len = sprintf(page, "hello ayyaz here\n");
mutex_unlock(&ayyaz_table_mutex);
if (off >= len+begin)
return 0;
*start = page + (off-begin);
return ((count < begin+len-off) ? count : begin+len-off);
}
static int __init init_ayyaz(void)
{
if ((proc_ayyaz = create_proc_entry( "ayyaz_maps", 0, NULL )))
proc_ayyaz->read_proc = ayyaz_read_proc;
return 0;
}
static void __exit cleanup_ayyaz(void)
{
if (proc_ayyaz)
remove_proc_entry( "ayyaz", NULL);
}
module_init(init_ayyaz);
module_exit(cleanup_ayyaz);
#else
#error "Please add CONFIG_PROC_FS=y in your .config "
#endif /* CONFIG_PROC_FS */
MODULE_LICENSE("proprietary");
MODULE_AUTHOR("Md.Ayyaz A Mulla <md.ayyaz#gmail.com>");
MODULE_DESCRIPTION("proc files for ayyaz");
Compile this driver. If it compiles sucessfully, then you will see /proc/ayyaz.
#define PROC_ENTRY_NAME "driver/XX"
static struct proc_dir_entry *proc_XX;
static int XX_read_proc (char *page, char **start, off_t off, int count,
int *eof, void *data_unused)
{
return 0;
}
static int XX_write_proc (struct file *file, const char __user *buffer,
unsigned long count, void *data)
{
return 0;
}
static int __init XX_add_driver(void)
{
if ((proc_flash = XX_entry(PROC_ENTRY_NAME, 0, NULL))) {
proc_XX->read_proc = XX_read_proc;
proc_XX->write_proc = XX_write_proc;
}
...
}
static void __exit XX_remove(void)
{
if (proc_flash)
remove_proc_entry(PROC_ENTRY_NAME, NULL);
return;
}
Then you can find the /proc/driver/XX entry.

Resources