[英]Compiler insists rename() function is of type void
I'm trying to write a program capable of renaming files, but I'm encountering a weird issue.我正在尝试编写一个能够重命名文件的程序,但我遇到了一个奇怪的问题。 When I write this:
当我写这个时:
string oldname, newname;
rename(oldname, newname);
Everything works fine.一切正常。 The file is renamed and there are no problems.
该文件已重命名,没有问题。 But then when I go ahead and try this:
但是当我提前 go 并尝试这个时:
int result;
result = rename(oldname, newname);
if(result)
//stuff
else
//other stuff
The "=" gets a fresh new red underline and I get an error "a value of type "void" cannot be assigned to an entity of type "int"". “=”有一条新的红色下划线,我收到一条错误消息“无法将类型为“void”的值分配给类型为“int”的实体”。 I also tried
我也试过
if(rename(oldname, newname))
//stuff
Then I just get "expression must have bool type (or be convertible to bool)"然后我就得到“表达式必须具有 bool 类型(或可转换为 bool)”
Microsoft's documentation says that the rename function returns 0 if successful and nonzero if not successful. Microsoft 的文档说重命名 function 如果成功则返回 0,如果不成功则返回非零。 After a lot of googling I found countless examples of code that use the second or third snippet without issue, and none of it has helped me understand what might be different in my situation.
经过大量谷歌搜索后,我发现无数使用第二个或第三个片段的代码示例没有问题,但没有一个能帮助我理解我的情况可能有什么不同。 The language standard is C++17. I can't think of any other relevant details, but just in case, here is a snippet of the actual code with all the "includes":
语言标准是 C++17。我想不出任何其他相关细节,但以防万一,这里是包含所有“包含”的实际代码片段:
#include <windows.h>
#include <stdlib.h>
#include <tchar.h>
#include <vector>
#include <string>
#include <filesystem>
#include <fstream>
#include <time.h>
#include <iostream>
#include <codecvt>
#include <locale>
#include <sstream>
#include <windowsx.h>
#include <Shlobj.h>
using namespace std;
using namespace std::filesystem;
tmp_wstr = oldname + L"\n\nwill be renamed to:\n\n" + newname + L"\n\nWould you like to proceed?";
msgboxID = MessageBox(NULL, tmp_wstr.c_str(), L"Renaming...", MB_YESNO | MB_SYSTEMMODAL);
switch (msgboxID)
{
case IDYES:
result = rename(oldname, newname);
if (result)
{
error = true;
msgboxID = MessageBox(NULL, L"Unable to rename", NULL, MB_OK | MB_SYSTEMMODAL);
}
else
error = false;
break;
case IDNO:
error = true;
break;
}
Here oldname and newname are wstring instead of string, but I have tried using string and the error persists.这里oldname和newname是wstring而不是string,但是我试过用string还是报错。
You are expecting to call the C standard library function rename()
with signature:您期望使用签名调用 C 标准库 function
rename()
:
int rename(const char*, const char*);
But that function would not be a viable overload for rename(oldname, newname)
if oldname
and newname
are of type std::(w)string
, since std::(w)string
is not implicitly convertible to const char*
.但是,如果
oldname
和newname
的类型为std::(w)string
,那么 function 将不是rename(oldname, newname)
的可行重载,因为std::(w)string
不能隐式转换为const char*
。
So, you must be calling a different function named rename()
.因此,您必须调用另一个名为
rename()
的 function。 Since you used:由于您使用了:
using namespace std::filesystem;
you are, in fact, calling the following overload of the C++ standard library function std::filesystem::rename()
with signature:实际上,您正在调用带有签名的 C++ 标准库 function
std::filesystem::rename()
的以下重载:
void rename(const std::filesystem::path&, const std::filesystem::path&);
which is viable with your arguments, since std::filesystem::path
is constructible from std::(w)string
.这对于您的 arguments 是可行的,因为
std::filesystem::path
可以从std::(w)string
构造。
This function has a void
return type, explaining the error messages.这个 function 有一个
void
返回类型,解释了错误信息。 As explained in the link, it doesn't return a status code, but instead throws an exception if there is an error.如链接中所述,它不返回状态代码,而是在出现错误时抛出异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.