表达式必须具有整数或无作用域枚举类型,并带有字符串向量

expression must have integral or unscoped enum type, with string vectors

本文关键字:类型 向量 字符串 枚举 整数 表达式 作用域      更新时间:2023-10-16

所以我正在尝试制作一个程序,该程序可以将某种类型的所有文件从我的下载文件夹移动到它们应该属于的文件夹。

我已经研究了一段时间,终于想出了:

#include <iostream>
#include <fstream>
#include <string>
#include <Windows.h>
#include <vector>
#include <stdio.h>
using namespace std;
vector<string> GetFileNamesInDirectory(string directory) {
    vector<string> files;
    HANDLE hFind;
    WIN32_FIND_DATA data;
    hFind = FindFirstFile(directory.c_str(), &data);
    if (hFind != INVALID_HANDLE_VALUE) {
        do {
            files.push_back(data.cFileName);
        } while (FindNextFile(hFind, &data));
        FindClose(hFind);
    }
    return files;
}
int main() {
    string *paths = new string[2];
    string line;
    ifstream pathFile("paths.txt");
    int i = 0;
    vector<string> rsFiles;
    string currentFile;
    int moveCheck;
    if (pathFile.is_open()) {
        while (getline(pathFile, line)) {
            paths[i] = line.substr(line.find_first_of(" ")+1);
            i++;
        }
        pathFile.close();
    }
    else {
        cout << "Unable to open file" << endl;
        return 0;
    }
    rsFiles = GetFileNamesInDirectory(paths[0]+"*.psarc");
    for (int j = 0; j < rsFiles.size(); j++) {
        currentFile = rsFiles[j];
        moveCheck = rename(paths[0].c_str() + currentFile.c_str(), paths[1].c_str() + currentFile.c_str());
    }
    system("pause");
    return 0;
}

因此,当我在 rename() 中移动文件时,我收到"currentFile"的错误,说"表达式必须具有完整或无作用域的枚举类型"。我假设这是因为你不能像我一样索引,或者类似的东西。

我是C++新手,但有其他编码经验,这对我来说很有意义。

另外,我知道我已经从其他来源获取了代码,但我不打算出售或公开它,它仅供我自己和我个人使用。

您需要更改连接两个字符串的方式,以便:

moveCheck = rename((paths[0] + currentFile).c_str(), (paths[1] + currentFile).c_str());

c_str() 将指针指向每个字符串内的字符缓冲区,因此添加两个指针没有意义。相反,您需要添加两个字符串,然后从串联字符串中获取数据缓冲区。


另一种写法,来自@Martin邦纳和@Nicky

std::string oldPath = paths[0] + currentFile; 
std::string newPath = paths[1] + currentFile; 
moveCheck = rename(oldPath.c_str(), newPath.c_str());