c++functiontypesdeclaration

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++...

I have read on various c++ tutorial websites, that if you want a function to not return anything, then you declare it as void. However when I did this, my compiler says invalid initializer. I am using dev-cpp.

I am thinking it is because my function is outputting text, but on the websites, the void function has some cout statements...

I have tried initializing it with char*, like my other functions, yet I get the same error. Same with int and char.


Solution

  • 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).

    Anyway, I don't know what you're trying to accomplish, but you really should read up on C++. It's a rather tricky language to use.