How to avoid Conflict of defined seconds() method and ptime seconds(default) method? - linux

Here unsigned long EVTime::seconds() method is conflicting with ptime p(d,seconds(s));. If I change ptime seconds(s) to minutes/hours then it works fine.
If i change that seconds(s) to minutes(s) or hours(s) then only it will work. I am new to C++, anyone please help to resolve this conflict.
evt.cpp:
unsigned long EVTime::seconds()
{
ptime t(date(1901,Jan,1),time_duration(0,0,0));
time_duration td = utcdatetime - t;
return (unsigned long)td.total_seconds();
}
EVTime::EVTime(unsigned long s)
{
date d(1901,1,1);
ptime p(d,seconds(s));
utcdatetime=p;
}
evt.h:
class EVTime
{
public:
EVTime(unsigned long s);
unsigned long seconds();
ptime utcdatetime;
};
main.cpp:
int main()
{
EVTime t(222l);
cout<<"seconds since 1901: "<<t.seconds()<<endl;
}
Error code:
evtime.cpp: In constructor ‘EVTime::EVTime(long unsigned int)’:
evtime.cpp:35: error: no matching function for call to ‘EVTime::seconds(long
unsigned int&)’
evtime.cpp:14: note: candidates are: long unsigned int EVTime::seconds()

Your function seconds() takes no parameters, but you send it s when you call it: ptime p(d,seconds(s));
This causes the error "no matching function for call to EVTime::seconds(long unsigned int&)", since you don't have a matching function.
Either call ptime p(d,seconds()); or change the seconds function signature to take the parameter and do something with it.

Related

Error initialization from incompatible pointer of IOCTL function in Linux kernel 4.8.0-53-generic Linux Mint 64 bit

I've got an error while writing a char device module, using Ioctl command.
static struct file_operations my_fops =
{
.unlocked_ioctl = my_ioctl, error is here. I can not fix this.
};
Note: please ignore all my print_k.
Please, help me fix this. My thanks to all of you.
Here is my code :
static long my_ioctl(struct file *f,unsigned int cm,unsigned long arg[b])
{
int re;
unsigned long arg[3];
switch (cm)
{
case H_ADD:
arg[2] = arg[0] + arg[1];
print_k("Driver:Calculating is complete,Result = %d \n",arg[2]);
break;
case H_SUB:
print_k ("Driver: Start ...\n");
arg[2] = arg[0] - arg[1];
print_k("Driver:Calculating is complete,Result = %d \n",arg[2]);
break;
case H_MULL:
print_k ("Driver: Start ...\n");
arg[2] = arg[0] * arg[1];
print_k("Driver:Calculating is complete,Result = %d \n",arg[2]);
break;
case H_DIV:
print_k ("Driver: Start ...\n");
arg[2] = arg[0] / arg[1];
print_k("Driver:Calculating is complete,Result = %d \n",arg[2]);
break;
default:
print_k ("Driver: I don't have this operation!\n");
re = -Er;
break;
}
return re;
}
static struct file_operations my_fops =
{
.unlocked_ioctl = my_ioctl,
};
The third argument unsigned long arg[b] in the function prototype is dubious. It should be simply unsigned long arg even if it's supposed to be a pointer. It's easy to cast it to the type of interest within the function body.
..the optional arg argument is passed in the form of an unsigned long, regardless of whether it was given by the user as an integer or a pointer.
( Linux Device Drivers 3, Chapter 6, Section 1 )
Also, it's wrong to declare a variable within the function body which has the same name as one of the arguments. Please choose another name for unsigned long arg[3];.

C++11 std::thread accepting function with rvalue parameter

I have some homework, and I have troubles understanding, (probably) how passing parameters to std::thread constructor works.
Assume following code (I deleted unneeded parts)
template<typename T, typename Task>
class Scheduler
{
private:
typedef std::unordered_map<std::size_t, T> Results;
class Solver
{
public:
Solver(Task&& task) : m_thread(&Solver::thread_function, std::move(task))
{
m_thread.detach();
}
Solver(Solver&& solver) = default; // required for vector::emplace_back
~Solver() = default;
private:
void thread_function(Task&& task)
{
task();
}
std::thread m_thread;
};
public:
Scheduler() = default;
~Scheduler() = default;
void add_task(Task&& task)
{
m_solvers.emplace_back(std::move(task));
}
private:
std::vector<Solver> m_solvers;
};
template<typename T>
struct Ftor
{
explicit Ftor(const T& t) : data(t) { }
T operator()() { std::cout << "Computed" << std::endl; return data; }
T data;
};
int main()
{
Scheduler<int, Ftor<int>> scheduler_ftor;
Scheduler<int, std::function<int(void)>> scheduler_lambda;
Ftor<int> s(5);
scheduler_ftor.add_task(std::move(s));
scheduler_lambda.add_task([](){ std::cout << "Computed" << std::endl; return 1; });
}
Why it doesn't compile?
MVS2015 is complaining about
functional(1195): error C2064: term does not evaluate to a function taking 1 arguments
functional(1195): note: class does not define an 'operator()' or a user defined conversion operator to a pointer-to-function or reference-to-function that takes appropriate number of arguments
note: while compiling class template member function 'Scheduler<int,Ftor<int> >::Solver::Solver(Task &&)'
While G++ 4.9.2
functional: In instantiation of ‘struct std::_Bind_simple<std::_Mem_fn<void (Scheduler<int, Ftor<int> >::Solver::*)(Ftor<int>&&)>(Ftor<int>)>’:
required from ‘void Scheduler<T, Task>::add_task(Task&&) [with T = int; Task = Ftor<int>]’
functional:1665:61: error: no type named ‘type’ in ‘class std::result_of<std::_Mem_fn<void (Scheduler<int, Ftor<int> >::Solver::*)(Ftor<int>&&)>(Ftor<int>)>’
typedef typename result_of<_Callable(_Args...)>::type result_type;
I suppose there are some problems with std::moving to std::thread.
If you use member function as first thread argument, second argument supposed to be this pointer, pointing to the object to which member function could be called to
UPDATE
Good discussion here
Start thread with member function
I don't follow your code, but addressing the question, a extrapolated answer will be( most of the code is psuedocode)
lets assume that there is a function int test(int name).
thread t0;
t0 = thread(test,32);
thread t1(test,43);
Passing a argument to function.
int temp = 0;
int testfunc(int& q)
{
cout<<q;
}
thread t1;
t1 = thread(testfunc,ref(temp));
In short, you just pass the name of the function that must be run in the thread as the first argument, and the functions parameters follow it in same order as they are in the function definition, for passing by reference you can use the ref() wrapper.See the below example.
#include <iostream>
#include <thread>
#include <string>
using namespace std;
void test(int a,int &a,string test)
{
\\do something
}
int main()
{
int test1 = 0;
string tt = "hello";
thread t1;
t1 = thread(23,&test1,tt);
t1.detach();
return 0;
}
if you are wondering about the use of join() and detach(), refer to this thread: When should I use std::thread::detach?, refer to my answer post in that thread.

Convert from void * to pointer to struct C++

I'm using pthreads to pass multiple parameters to a function by way of a struct. I'm attempting to convert the struct I pass in using a conversion from void * to struct thread_args *. This is giving me an error. Can someone please explain how to manage this in C++.
Here is my code:
struct thread_args
{
int customer_num;
int res_vector[NUMBER_OF_RESOURCES];
};
typedef struct thread_args theArgs;
int request_resources(int customer_num, int request[]);
int release_resources(int customer_num, int release[]);
void req_converter( void * x );
void rel_converter( void * x );
void req_converter( void * x )
{
theArgs * args = dynamic_cast<theArgs *>(x); //(struct arg_struct *) x;
request_resources( x.customer_num, x.res_vector);
}
The error message I'm getting is:
error: cannot dynamic_cast ‘x’ (of type ‘void*’) to type ‘theArgs* {aka struct thread_args*}’ (source is not a pointer to class)
Can someone please explain the best way to pass a struct using pthreads to a function that will use the struct parameters to call a second function with multiple parameters in C++?

warning C4020: 'CreateVertex' : too many actual parameters

What does that warning says?
I could not find any typos or extra characters in the code.:
if (!(connectedComp = (Vertex**)malloc(sizeof(Vertex*)))) {
AllocationError();
}
if (!(created = (unsigned int*)malloc(sizeof(unsigned int)))) {
AllocationError();
}
connectedComp[++counter] = CreateVertex(id, edgesMatrix, maxValue, created);
I declared
Vertex** connectedComp = NULL;
and the function signature is:
Vertex* CreateVertex(unsigned int id, unsigned int** edgesMatrix, unsigned int maxValue);
Thanks in advance,
Function prototype of CreateVertex has 3 arguments and you are trying to send 4. That is the warning
Well your function signature is
Vertex* CreateVertex(unsigned int id, unsigned int** edgesMatrix, unsigned int maxValue);
And you are passing 4 arguments. This is the problem and hence compiler is giving the following compilation error.
connectedComp[++counter] = CreateVertex(id, edgesMatrix, maxValue, created);
By looking your code your call should be
connectedComp[++counter] = CreateVertex(id, edgesMatrix, maxValue);
BTW, you should avoid using malloc/free in C++ instead of that use new/delete or smart_pointer mechanism.

Why aren't these arguments valid?

//Block.h
#pragma once
class Block
{
public:
CRect pos;
int num;
public:
Block(void);
~Block(void);
};
//view class
public:
Block currentState[5]; // stores the current state of the blocks
void CpuzzleView::OnDraw(CDC* pDC)
{
CpuzzleDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
//draw the 4 blocks and put text into them
for(int i=0;i<4;i++)
{
pDC->Rectangle(currentState[i].pos);
// i'm getting an error for this line:
pDC->TextOut(currentState[i].pos.CenterPoint(), currentState[i].num);
}
pDC->TextOut(currentState[i].pos.CenterPoint(), currentState[i].num);
The error says that no instance of overloaded function CDC::TextOutW() matches the argument list . But the prototype for the function is:
CDC::TextOutW(int x, int y, const CString &str )
all i've done is that instead of the 2 points i've directly given the point object returned by CenterPoint() ... shouldn't it work?
That's because you didn't supplied arguments list correctly. Please read compiler error message carefully, it's usually helps to solve the problem.
TextOut(currentState[i].pos.CenterPoint(), currentState[i].num);
In this call you passed CPoint object and int. This is not correct, you need to pass int, int and CString (or const char* and int length).
To fix this you shall do something like this:
CString strState;
strState.Format("%d", currentState[i].num); // Or use atoi()/wtoi() functions
TextOut(currentState[i].pos.CenterPoint().x, currentState[i].pos.CenterPoint().x, strState);

Resources