gpt4 book ai didi

c++ - clockTick 函数未在此范围内声明

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:50:15 25 4
gpt4 key购买 nike

我将 CPP 和 HPP 文件都放在这里以及我收到的错误:我在这里添加完整的代码和我遇到的错误;请帮助。也请帮我发布这个问题。消费品:

#include "TimeWindowCounter.hpp"
#include <iostream>
#include "../../Common/TimeAnalyser.hpp"

TimeWindowCounter::TimeWindowCounter()
{
init();
}

void TimeWindowCounter::init()
{
boost::atomic<bool> first_order_sent(false);
}

TimeWindowCounter::TimeWindowCounter(double currTime)
{
first_order_sent = true;
startTime = currTime;
endTime = currTime;
orderCount = 0;
ind = 0;
N = 1000;
for (int j =0; j<= 99; j++ )
numberofOrder[j] = 0;
}

TimeWindowCounter::~TimeWindowCounter()
{
}

void TimeWindowCounter::addOrder()
{
orderCount++;
numberofOrder[ind] = orderCount;
}

void TimeWindowCounter::clockTick(const boost::system::error_code& /*e*/, boost::asio::deadline_timer* t, int* count, double* currTime)
{

stopTime = *currTime;
WindowSize = timeAnalyser.getDiffTime_rt(startTime, stopTime);
if (WindowSize.count()*1000 > N)
startTime = startTime + (WindowSize.count()*1000-N);
ind = (ind + 1) % 10;

std::cout << *count << std::endl;
++(*count);

t->expires_at(t->expires_at() + boost::posix_time::milliseconds(1));
t->async_wait(boost::bind(clockTick,boost::asio::placeholders::error, t, count, currTime));

} // 10ms or 100 times a second

int TimeWindowCounter::getNumOfOrders()
{
int sum = 0;
for (int i = 0; i<= 99; i++)
sum = sum + numberofOrder[i];
return sum;
}


void TimeWindowCounter::run(){

io.run();

}

int main()
{

boost::asio::io_service io;

int count = 0;
TimeAnalyser timeAnalyser;
double currTime = timeAnalyser.getClockTime();
TimeWindowCounter * timewindowCounter = new TimeWindowCounter();
boost::asio::deadline_timer t(io, boost::posix_time::milliseconds(10));

t.async_wait(boost::bind(clockTick,boost::asio::placeholders::error, &t, &count, &currTime));

timewindowCounter->run();

for (int j = 0; j <=100000 ; j++)
timewindowCounter->addOrder();
std::cout << "Final count is " << count << std::endl;

}

HPP:

#ifndef TIMEWINDOWCOUNTER
#define TIMEWINDOWCOUNTER

#include <iostream>
#include <boost/chrono.hpp>
#include <boost/atomic.hpp>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <cmath>
#include <unistd.h>
#include "../../Common/TimeAnalyser.hpp"

using namespace std;

class TimeWindowCounter
{

TimeAnalyser timeAnalyser;
boost::asio::io_service io;

public:

TimeWindowCounter();
~TimeWindowCounter();

TimeWindowCounter(double currTime);

boost::atomic<bool> first_order_sent;
boost::chrono::duration<double> interval;

double startTime;
double stopTime;
double endTime;
double currTime;

void init();

int numberofOrders;
int orderCount;

int numberofOrder[99];
int ind;

boost::chrono::duration<double> WindowSize;
int N; // WindowSize
void addOrder();
void clockTick(const boost::system::error_code&, boost::asio::deadline_timer* t, int* count, double* currTime); // 10ms or 100 times a second
int getNumOfOrders();
void timer_thread();
void run();

};

#endif

编译:

g++ -std=c++11 -o TA TimeWindowCounter.cpp -lboost_chrono -lboost_system

错误:

TimeWindowCounter.cpp: In member function ‘void TimeWindowCounter::clockTick(const boost::system::error_code&, boost::asio::deadline_timer*, int*, double*)’:
TimeWindowCounter.cpp:50:93: error: invalid use of non-static member function
t->async_wait(boost::bind(clockTick,boost::asio::placeholders::error, t, count, currTime));
^
TimeWindowCounter.cpp: In function ‘int main()’:
TimeWindowCounter.cpp:80:26: error: ‘clockTick’ was not declared in this scope
t.async_wait(boost::bind(clockTick,boost::asio::placeholders::error, &t, &count, &currTime));

最佳答案

您正在绑定(bind)一个实例成员函数。

您需要限定函数名称并传递一个合适的“this”参数作为第一个参数:

t->async_wait(boost::bind(&TimeWindowCounter::clockTick, this, boost::asio::placeholders::error, t, count, currTime));

t.async_wait(boost::bind(&TimeWindowCounter::clockTick, timewindowCounter, boost::asio::placeholders::error, &t, &count, &currTime));

分别。

现场演示

Live On Coliru

#ifndef TIMEWINDOWCOUNTER
#define TIMEWINDOWCOUNTER

#include <iostream>
#include <boost/chrono.hpp>
#include <boost/atomic.hpp>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <cmath>
#include <unistd.h>
//#include "../../Common/TimeAnalyser.hpp"

struct TimeAnalyser {
double getClockTime() const { return 0; }
boost::chrono::duration<double> getDiffTime_rt(double, double) const { return boost::chrono::milliseconds(1); }
};

using namespace std;

class TimeWindowCounter {

TimeAnalyser timeAnalyser;
boost::asio::io_service io;

public:
TimeWindowCounter();
~TimeWindowCounter();

TimeWindowCounter(double currTime);

boost::atomic<bool> first_order_sent;
boost::chrono::duration<double> interval;

double startTime;
double stopTime;
double endTime;
double currTime;

void init();

int numberofOrders;
int orderCount;

int numberofOrder[99];
int ind;

boost::chrono::duration<double> WindowSize;
int N; // WindowSize
void addOrder();
void clockTick(const boost::system::error_code&, boost::asio::deadline_timer* t, int* count,
double* currTime); // 10ms or 100 times a second
int getNumOfOrders();
void timer_thread();
void run();
};

#endif
//#include "TimeWindowCounter.hpp"
#include <iostream>
//#include "../../Common/TimeAnalyser.hpp"

TimeWindowCounter::TimeWindowCounter() { init(); }

void TimeWindowCounter::init() { boost::atomic<bool> first_order_sent(false); }

TimeWindowCounter::TimeWindowCounter(double currTime)
{
first_order_sent = true;
startTime = currTime;
endTime = currTime;
orderCount = 0;
ind = 0;
N = 1000;
for (int j = 0; j <= 99; j++)
numberofOrder[j] = 0;
}

TimeWindowCounter::~TimeWindowCounter() {}

void TimeWindowCounter::addOrder()
{
orderCount++;
numberofOrder[ind] = orderCount;
}

void TimeWindowCounter::clockTick(const boost::system::error_code& /*e*/, boost::asio::deadline_timer* t, int* count,
double* currTime)
{

stopTime = *currTime;
WindowSize = timeAnalyser.getDiffTime_rt(startTime, stopTime);
if (WindowSize.count() * 1000 > N)
startTime = startTime + (WindowSize.count() * 1000 - N);
ind = (ind + 1) % 10;

std::cout << *count << std::endl;
++(*count);

t->expires_at(t->expires_at() + boost::posix_time::milliseconds(1));
t->async_wait(boost::bind(&TimeWindowCounter::clockTick, this, boost::asio::placeholders::error, t, count, currTime));

} // 10ms or 100 times a second

int TimeWindowCounter::getNumOfOrders()
{
int sum = 0;
for (int i = 0; i <= 99; i++)
sum = sum + numberofOrder[i];
return sum;
}

void TimeWindowCounter::run() { io.run(); }

int main()
{

boost::asio::io_service io;

int count = 0;
TimeAnalyser timeAnalyser;
double currTime = timeAnalyser.getClockTime();
TimeWindowCounter* timewindowCounter = new TimeWindowCounter();
boost::asio::deadline_timer t(io, boost::posix_time::milliseconds(10));

t.async_wait(boost::bind(&TimeWindowCounter::clockTick, timewindowCounter, boost::asio::placeholders::error, &t, &count, &currTime));

timewindowCounter->run();

for (int j = 0; j <= 100000; j++)
timewindowCounter->addOrder();
std::cout << "Final count is " << count << std::endl;
}

打印

Final count is 0

关于c++ - clockTick 函数未在此范围内声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30395245/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com