函数定义 document_path(const char * root_path,folder_t... folders,const folder_t&... folders2) { } 调用 document_path("DebugX", "1111", "222", "333"); 报错 error: no matching member function for call to 'document_path' GlobalApplication.h:41:13: note: candidate template ignored: deduced packs of different lengths for parameter 'folder_t' (<> vs. <char [5], char *, char *>) linkManager->start();
时间: 2024-02-10 09:08:21 浏览: 127
根据你提供的信息,可以看出 `document_path` 函数是一个可变参数模板函数,其中参数 `folders` 和 `folders2` 都是可变参数模板参数。
在你的调用中,你传递了三个 `const char*` 类型的参数给 `document_path` 函数,但是你没有为可变参数提供任何参数。这会导致编译器无法推断可变参数的类型和数量,从而导致编译错误。
为了解决这个问题,你需要为可变参数提供至少一个参数,以便编译器可以推断出参数类型和数量。例如,你可以这样修改调用 `document_path` 的代码:
```
document_path("DebugX", "1111", "222", "333", "");
```
在这个例子中,我添加了一个空字符串 `""` 作为可变参数的第一个参数。这样就可以使编译器可以推断出可变参数的类型和数量,从而避免编译错误。
如果你需要传递更多的参数,你可以根据需要向可变参数列表中添加更多的参数。
相关问题
函数定义 template<typename... folder_t> QString GlobalApplication::document_path(folder_t... folders) {va_list arg_list; va_start(arg_list, folders...);} 调用 QString dir =document_path("DebugX","1111","222","333"); 报错 /home/dev/work/uGround/GlobalApplication.cpp:164: error: too many arguments to function call, expected 2, have 5 报错2: error: ‘va_start’ used in function with fixed args
这个错误是因为 `va_start()` 函数只能用于可变参数列表的函数,而 `document_path()` 函数定义中并没有可变参数列表。因此,你不能在 `document_path()` 函数中使用 `va_start()` 函数。
解决这个问题的方法是将 `document_path()` 函数定义改为可变参数列表函数。具体来说,你可以使用 C++11 中的新特性 `std::initializer_list` 来实现可变参数列表。下面是修改后的代码:
```cpp
template<typename... folder_t>
QString GlobalApplication::document_path(folder_t... folders)
{
QStringList paths{folders...};
QString dir = QCoreApplication::applicationDirPath();
for (const QString& path : paths) {
dir = QDir(dir).filePath(path);
}
return dir;
}
```
然后,你可以像这样调用 `document_path()` 函数:
```cpp
QString dir = document_path("DebugX", "1111", "222", "333");
```
这样就不会再出现你遇到的错误了。
用C++写There are n folders in a computer, indexed from 1 to n . The folder indexed 1 is the root folder, and each folder may contain several subfolders, forming a tree structure. You can perform several "cut" operations, each cutting a source folder into a target folder. Note that the target folder cannot be the same as the source folder, nor can it be the source folder's subfolder, subfolder's subfolder, and so on. Obviously, the root folder cannot be cut, and it will always be the root folder. Find out that at least how many "cut" operations should be performed so that each folder contains at most one subfolder. Input The first line contains a single integer n (1≤n≤105) — the number of folders. The second line of each test case contains n−1 space separated integers p2,p3,⋯,pn (1≤pi≤n) — pi represents the index of the parent folder of folder i . Output Output the minimum number of "cut" operations performed. Note that there may not be a need to perform "cut" operation. Please output 0 in this case.
以下是使用 C++ 解决该问题的代码:
```cpp
#include <iostream>
#include <vector>
using namespace std;
const int MAXN = 1e5 + 5;
vector<int> G[MAXN];
int ans;
void dfs(int u, int fa) {
int cnt = 0;
for (int v : G[u]) {
if (v == fa) continue;
dfs(v, u);
cnt++;
}
ans += max(cnt - 1, 0);
}
int main() {
int n;
cin >> n;
for (int i = 2; i <= n; i++) {
int x;
cin >> x;
G[x].push_back(i);
}
dfs(1, 0);
cout << ans << endl;
return 0;
}
```
该问题可以用一个简单的 DFS 解决。我们首先读入每个节点的父亲节点,然后建立一棵树。对于每个节点,我们统计它的子节点个数,记为 $cnt$。如果 $cnt \geq 2$,则需要将其中的 $cnt - 1$ 个子节点切断,使得每个节点最多只有一个子节点。因此,我们在 DFS 的过程中累加所有需要切断的次数,最终输出即可。
阅读全文