问题
In the example here: https://en.cppreference.com/w/cpp/chrono/high_resolution_clock/now
They declared the clock time point with auto
.
auto start = std::chrono::high_resolution_clock::now();
and the doc says it returns 'A time point representing the current time.'
But I am not sure how do declare in my code below because I am used to declaring the variables at the beginning of the function and I don't know what to declare it as. Code has been simplified here to show what I mean. What do I put for the ???
?
I already tried auto
there but the compiler won't allow it. auto orderRecvedTime;
gives me this error:
error: non-static data member declared with placeholder 'auto'
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <string.h>
//#include "load_symbol.h"
//#include "check_symbol.h"
#include "windows.h"
#include <vector>
#include <chrono>
using namespace std;
class order {
private:
string orderID;
??? orderRecvedTime;
char buysell;
string symbol;
double price;
int qty;
public:
void newOrder(string &_orderID, char &_buysell, string &_symbol, double &_price, int &_qty){
orderID = _orderID;
buysell = _buysell;
symbol = _symbol;
price = _price;
qty = _qty;
orderRecvedTime = std::chrono::high_resolution_clock::now();
}
};
int main() {
cout << "!!!Hello once more" << endl; // prints !!!Hello once more
vector<order> thebook;
string user_order = "";
string done = "done trading";
string orderID;
string orderaction;
string orderRecvedTime;
char buysell;
string symbol;
double price;
int qty;
while (user_order.compare(done) != 0) {
cout << "enter order"<< endl;
getline(cin, user_order);
stringstream lineStream(user_order);
lineStream >>orderaction>>orderID>> buysell >> symbol >> price>> qty;
order user_order;
if (orderaction.compare("D") == 0) {
cout << "you are making a new order."<< endl;
user_order.newOrder(orderID, buysell,symbol,price,qty);
thebook.push_back(user_order);
}
}
}
回答1:
std::chrono::high_resolution_clock::time_point orderRecvedTime;
In practice, high_resolution_clock
is a type alias for either system_clock
or steady_clock
, so my advice is to choose either one of those instead for a portable experience.
system_clock
is like a watch. It can tell you what time it is.steady_clock
is like a stop watch. It is really good for timing things, but doesn't really know the time of day.
回答2:
pulled this from the debugger msg but if i declare it like this it compiles.
std::chrono::_V2::system_clock::time_point orderRecvedTime;
来源:https://stackoverflow.com/questions/62185958/how-to-declare-a-variable-for-high-resolution-clock-in-c