[英]Compiler doesn't like void :'( - function declaration
I am trying to write a function to change some folder names, but I am having difficulties in the actual declaration. 我正在尝试编写一个函数来更改一些文件夹名称,但我在实际声明中遇到了困难。
Here is my code: 这是我的代码:
void ChangeVersion(char* NewVer)
{
ifstream show_current_version;
show_current_version.open(*removed*);
char show_current_version_var[1024];
if (show_current_version.is_open())
{
while (!show_current_version.eof())
{
show_current_version >> show_current_version_var;
}
}
show_current_version.close();
// show_current_version_var is old version
char OldVersion[1024] = show_current_version_var;
// start rename
cout << "Changing versions...";
rename("*removed*", OldVersion);
rename(NewVer, "*removed*");
cout << "done!" << endl;
}
As you can tell, I am new to c++... 你可以告诉我,我是c ++的新手......
I have read on various c++ tutorial websites, that if you want a function to not return anything, then you declare it as void. 我已阅读各种c ++教程网站,如果你想要一个函数不返回任何东西,那么你将它声明为无效。 However when I did this, my compiler says invalid initializer. 但是,当我这样做时,我的编译器说无效的初始化程序。 I am using dev-cpp. 我正在使用dev-cpp。
I am thinking it is because my function is outputting text, but on the websites, the void function has some cout statements... 我在想这是因为我的函数输出文本,但在网站上,void函数有一些cout语句......
I have tried initializing it with char*, like my other functions, yet I get the same error. 我尝试用char *初始化它,就像我的其他函数一样,但我得到了同样的错误。 Same with int and char. 与int和char相同。
Thank you for reading. 谢谢你的阅读。
You really should give the output of the compiler a closer look. 你真的应该仔细看看编译器的输出。 It generally tells you what line contains the error, which gives you a lot to work with. 它通常会告诉您哪行包含错误,这会让您有很多工作要做。
Getting to the particular problem the offending line (from what I gather, havent tried compiling) is: 找到特定问题的违规行(从我收集的,没有尝试编译)是:
char OldVersion[1024] = show_current_version_var;
You can't assign a variable to a static array like that. 您不能将变量分配给这样的静态数组。 There's only a handful of things one can use to initialize a static array. 只有少数可以用来初始化静态数组的东西。 For example: 例如:
char OldVersion[1024] = "Static string";
char example[1024] = { 0 };
Try doing: 尝试做:
char OldVersion[1024];
strncpy(OldVersion, show_current_version_var, 1024);
// Null-terminate the string for good measure
OldVersion[1023] = 0;
Or simply use show_current_version_var
where you would use OldVersion
(I see no reason to copy the string in the code you pasted). 或者只是使用show_current_version_var
,你将使用OldVersion
(我认为没有理由在你粘贴的代码中复制字符串)。
Anyway, I don't know what you're trying to accomplish, but you really should read up on C++. 无论如何,我不知道你想要完成什么,但你真的应该阅读C ++。 It's a rather tricky language to use. 这是一种相当棘手的语言。
Couple of small things 几件小事
When you post code, post everything that we need to compile it: 当您发布代码时,发布我们编译它所需的所有内容:
// You need these to make it compile.
#include <iostream>
#include <fstream>
using namespace std;
OK. 好。 Bugs: 错误:
show_current_version.open(*removed*);
^^^^^^^^^ What is this supposed to be ?
This function is expecting a C-String. 这个函数期待一个C-String。 So "removed" is valid. 所以“删除”是有效的。 But the stars *
don't make sense. 但是星星*
没有意义。
char OldVersion[1024] = show_current_version_var;
You can't copy arrays like that: 你无法像这样复制数组:
char OldVersion[1024];
std::copy(show_current_version_var, show_current_version_var+1024, OldVersion);
What would be better though is to use std::vector rather than an array. 更好的是使用std :: vector而不是数组。 Then you are allowed to copy. 然后你可以复制。
std::vector<char> show_current_version_var(1024);
// STUFF
std::vector<char> OldVersion(show_current_version_var);
Alternatively if you are specifically storing strings (not blobs of char) then std::string is probably your best bet. 或者,如果你专门存储字符串(不是字符blo),那么std :: string可能是你最好的选择。
I don't think that you can do this: 我认为你不能这样做:
char OldVersion[1024] = show_current_version_var;
You can only initialise arrays with a brace-list (like {'p', 'a', 'x'}
for example). 您只能使用大括号列表初始化数组(例如{'p', 'a', 'x'}
)。
Instead, you should try: 相反,你应该尝试:
char OldVersion[1024];
memcpy (OldVersion, show_current_version_var, sizeof (OldVersion));
A complete, compilable version is shown below: 完整的可编译版本如下所示:
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
void ChangeVersion(char* NewVer)
{
ifstream show_current_version;
show_current_version.open("qqq");
char show_current_version_var[1024];
if (show_current_version.is_open())
{
while (!show_current_version.eof())
{
show_current_version >> show_current_version_var;
}
}
show_current_version.close();
// show_current_version_var is old version
char OldVersion[1024];
memcpy (OldVersion, show_current_version_var, sizeof (OldVersion));
// start rename
cout << "Changing versions...";
rename("*removed*", OldVersion);
rename(NewVer, "*removed*");
cout << "done!" << endl;
}
int main() {
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.