简体   繁体   English

在C ++中返回结构的向量时,没有用于调用[class]的匹配函数

[英]no matching function for call to [class] when returning a vector of structures in c++

I get an error at the line indicated below: I get this error when i try to return a vector of structures. 在下面指示的行上出现错误:当我尝试返回结构向量时,出现此错误。 I used a template to be able to return these vectors. 我使用了一个模板来能够返回这些向量。 Im not where my error is. 我不是我的错误所在。 If i do not use the template, set the return type to vector. 如果我不使用模板,请将返回类型设置为vector。

Header File:

typedef unsigned long ulong_t;
class Tool {
public:
    Tool();
void toolInterface();
void run();
private:

ifstream allDevicesFile;
struct devStats {
    //structure variables,
};
template<class devStats>
vector<devStats> readDev();
vector<devStats> stats;
string fileNameAll;
Report report;
Commandline cmd;
Configuration conf;
Devices dev;
};

Tool.cpp: Tool.cpp:

Tool::Tool() {
fileNameAll = "/proc/diskstats";
    allDevicesFile.open((char*)fileNameAll.c_str());
    if (allDevicesFile.fail()) {
        cout << "Could not open /proc/diskstats\n";
    }// TODO Auto-generated constructor stub

}
template<class devStats>
vector<devStats> Tool::readDev() {
devStats dev;
while (!allDevicesFile.eof()) {
    allDevicesFile >> dev.decoy1;
    allDevicesFile >> dev.decoy2;
    allDevicesFile >> dev.devName;
    allDevicesFile >> dev.reads;
    allDevicesFile >> dev.readMerge;
    allDevicesFile >> dev.writes;
    allDevicesFile >> dev.secReading;
    allDevicesFile >> dev.mSecondsRead;
    allDevicesFile >> dev.writeCompleted;
    allDevicesFile >> dev.secWritting;
    allDevicesFile >> dev.mSecondWrite;
    allDevicesFile >> dev.currentI_O;
    allDevicesFile >> dev.mSecondsI_O;
    allDevicesFile >> dev.weightedI_O;
    dev.mSecondsRead = dev.mSecondsRead / 1000;
    dev.mSecondWrite = dev.mSecondWrite / 1000;
    dev.mSecondsI_O = dev.mSecondsI_O / 1000;
    stats.push_back(dev);
}
cout << stats[0].devName;
return stats;
}

void Tool::run() {
stats = readDev(); //error occurs here.
}

Ordinary overload resolution does not consider return type. 普通的重载解决方案不考虑返回类型。 The return type is not matched against anything. 返回类型与任何内容都不匹配。 So there is no way that the template parameter can be automatically resolved. 因此,无法自动解析template参数。

As a practical solution, just remove the templatization. 作为实际的解决方案,只需删除模板。


There is a special case where a template parameter that is only used as return type, can be resolved by the calling context, and that's for a conversion operator. 在特殊情况下,调用上下文可以解析仅用作返回类型的模板参数,该参数用于转换运算符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM