我在C ++中用两个字段创建了一个struct SensorRecordTuple
。第一个是long
,第二个是通用的typename T
我的课程SensorRecorder
有两种方法。第一种方法是将SensorRecordTuple
保存在文件上,第二种方法是从文件中读取它。为了阅读,我有一个没有参数的方法read()
,它正在用一个参数调用template<typename T> SensorRecordTuple<T> read(long relativeTime)
。这是针对文件的第一行,其相对时间为零。
template<typename T>
struct SensorRecordTuple {
long time;
T *value;
SensorRecordTuple() {
time = -1;
value = 0;
}
SensorRecordTuple(long _time, T _value) {
time = _time;
value = _value;
}
};
class SensorRecorder {
private:
std::ofstream outFile;
std::ifstream inFile;
std::string fileName;
boost::filesystem::path full_path;
long long duration = -1;
public:
const std::string OUTPUT_DIR = "out";
const std::string EXTENSION = ".out";
const char SEPARATOR = ',';
SensorRecorder(const char *classNameType, PRIVILEGES privileges);
~SensorRecorder();
template<typename T>
int write(T value) {
if (outFile.is_open()) {
auto now = std::chrono::system_clock::now();
auto now_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(now);
auto time = now_ms.time_since_epoch();
if (duration == -1) {
duration = 0;
} else {
duration = time.count() - duration;
}
// write in the file
outFile << duration << SEPARATOR << value << '\n';
// evaluate duration time to the next write
duration = time.count();
std::cout << "value: " << value << std::endl;
} else {
std::cerr << "error writing..." << std::endl;
return 0;
}
return 1;
}
template<typename T>
SensorRecordTuple<T> read() {
return read<T>(-1L);
}
template<typename T>
SensorRecordTuple<T> read(long relativeTime) {
SensorRecordTuple tuple = {-1, 0.0};
if (!inFile.is_open()) {
inFile.open(full_path.c_str());
}
// read from the file
long d;
char comma;
double data;
while (inFile >> d >> comma >> data) {
std::cout << d << "," << data << " - size: " << data << std::endl;
if (relativeTime == -1 && d == 0) {
// First relative time
inFile.close();
// cast the value
tuple.time = d;
tuple.value = (T) data;
return tuple;
} else if (d > relativeTime) {
// Next relative time
inFile.close();
// cast the value
tuple.time = d;
tuple.value = (T) data;
return tuple;
}
}
inFile.close();
tuple.time = -1;
tuple.value = (T) 0.0;
return tuple;
}
我创建了一个写入和读取文件后的测试。编译器说read方法不存在:No matching member function for call to 'read'
。但是write
存在。怎么会来?
SensorRecorder *mySensorRecorderWriter = new SensorRecorder("SensorRecorderTest", PRIVILEGES::RW);
int ret = mySensorRecorderWriter->write(inputValues01);
ASSERT_EQ(ret, 1);
SensorRecorder *mySensorRecorderReader = new SensorRecorder(mySensorRecorderWriter->getFileName().c_str(), PRIVILEGES::RO);
SensorRecordTuple tuple01 = mySensorRecorderReader->read();
答案 0 :(得分:0)
read()
不是函数。这是一个功能模板。
但是,您没有提供任何模板参数,也没有可从中推导出模板参数的函数参数(与其他read
重载不同)。
因此,您的呼叫仅尝试调用普通函数read
,而该函数不存在。
就像您的编译器一样,我不知道您想要什么T
,但是您需要在此处提供一个:
SensorRecordTuple tuple01 = mySensorRecorderReader->read<HERE>();
顺便说一句,您的构造函数已损坏。它尝试将T
分配给T*
。这行不通。
也不要试图只分配&_value
,因为这是一个局部变量。
稍后从此行:
tuple.value = (T) 0.0;
…您似乎确实希望value
是一个数字值,而不是指针。因此,假设您实际上并不想延迟该成员的构造,只需存储一个值并正确初始化它即可:
template<typename T>
struct SensorRecordTuple {
long time;
T value;
SensorRecordTuple()
: time(-1)
, value(0)
{}
SensorRecordTuple(long _time, T _value)
: time(_time)
, value(_value)
{}
};
通常,您希望使用, value(std::move(_value))
,但是由于示例T
都是内置数字类型(不能移动构造),并且由于std::move
使示例复杂,我已经省略了。