【发布时间】:2020-06-04 02:37:55
【问题描述】:
在此处的示例中:https://en.cppreference.com/w/cpp/chrono/high_resolution_clock/now
他们用auto声明了时钟时间点。
auto start = std::chrono::high_resolution_clock::now();
文档说它返回“代表当前时间的时间点”。
但是我不确定如何在下面的代码中声明,因为我习惯于在函数的开头声明变量并且我不知道将其声明为什么。代码已在此处简化以说明我的意思。我要为??? 写什么?
我已经在那里尝试过auto,但编译器不允许这样做。 auto orderRecvedTime; 给了我这个错误:
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);
}
}
}
【问题讨论】:
-
Google for high_resolution_clock::now,您会看到带有类型的描述(单词
static不是类型的一部分)。 -
你的意思是:std::chrono::time_point?
-
我尝试使用
std::chrono::time_point orderRecvedTime,但编译器出错:``` 错误:在没有参数列表的情况下无效使用模板名称'std::chrono::time_point' ``` -
@D.Zou 您链接的页面顶部有
now()的声明。static和now()之间的所有内容都是返回值的类型:std::chrono::time_point<std::chrono::high_resolution_clock>。 -
auto通常很有用。
标签: c++ chrono high-resolution-clock