有没有佬帮我看下为什么不行?是什么问题?
//file.h
#ifdef FILE_H
#define FILE_H
#include <iostream>
#include <fstream>
#include <filesystem>
#include <string>
using namespace std;
namespace file{
#ifdef _WIN32
const char PATH_SEPARATOR = '\\';
#else
const char PATH_SEPARATOR = '/';
#endif
bool createFile(const string& filePath) {
ofstream file(filePath);
if (file.is_open()) {
file.close();
return true;
}
return false;
}
string readFile(const string& filePath) {
ifstream file(filePath);
if (!file.is_open()) {
return "";
}
string content((istreambuf_iterator<char>(file)), istreambuf_iterator<char>());
file.close();
return content;
}
bool writeFile(const string& filePath, const string& content) {
ofstream file(filePath);
if (!file.is_open()) {
return false;
}
file << content;
file.close();
return true;
}
bool deleteFile(const string& filePath) {
return std::filesystem::remove(filePath);
}
bool fileExists(const string& filePath) {
return std::filesystem::exists(filePath) && !std::filesystem::is_directory(filePath);
}
bool createDirectory(const string& directoryPath) {
return std::filesystem::create_directory(directoryPath);
}
bool deleteDirectory(const string& directoryPath) {
return std::filesystem::remove_all(directoryPath) > 0;
}
uintmax_t fileSize(const string& filePath) {
return std::filesystem::file_size(filePath);
}
vector<string> fileList(const string& directoryPath) {
vector<string> files;
for (const auto& entry : std::filesystem::directory_iterator(directoryPath)) {
if (!std::filesystem::is_directory(entry.path())) {
files.push_back(entry.path().filename().string());
}
}
return files;
}
vector<string> directoryList(const string& directoryPath) {
vector<string> directories;
for (const auto& entry : std::filesystem::directory_iterator(directoryPath)) {
if (std::filesystem::is_directory(entry.path())) {
directories.push_back(entry.path().filename().string());
}
}
return directories;
}
}
#endif
//main.cpp
#include <iostream>
#include "file.h"
#include "random.h"
int main(int argc, char *argv[]){
if (file::createFile("test")){
std::cout << "done";
}
return 0;
}
报错为
main.cpp: In function ‘int main(int, char**)’:
main.cpp:5:13: error: ‘file’ has not been declared
5 | if (file::createFile(“test”)){
| ^~~~