Search code examples
c++refactoringheader-filesclionautomated-refactoring

Refactoring: Move (F6) in CLion - why does it work like that?


I have in my main.cpp this function:

double median(double values[], int count) {
    int i, j, n = count, t;
    for (i = 1 ; i <= n - 1 ; i++)
    {
        for (j = 1 ; j <= n - i ; j++)
        {
            if (values[j] <= values[j + 1])
            {
                t = (int) values[j];
                values[j] = values[j + 1];
                values[j + 1] = t;
            }
        }
    }
    if ( n % 2 == 0)
        return (values[n / 2] + values[n / 2 + 1]) / 2.0 ;
    else
        return values[n / 2 + 1];
}

and I want to move that function into another file. So, I click on this function and then click F6 and then I write some name of the file eg. MathFunctions and then I end up with:

MathFunctions.h:

#ifndef PROJECT_NAME_MATHFUNCTIONS_H
#define PROJECT_NAME_MATHFUNCTIONS_H

#endif //PROJECT_NAME_MATHFUNCTIONS_H

MathFunctions.cpp:

#include "MathFunctions.h"

double median(double values[], int count) {
    int i, j, n = count, t;
    for (i = 1 ; i <= n - 1 ; i++)
    {
        for (j = 1 ; j <= n - i ; j++)
        {
            if (values[j] <= values[j + 1])
            {
                t = (int) values[j];
                values[j] = values[j + 1];
                values[j + 1] = t;
            }
        }
    }
    if ( n % 2 == 0)
        return (values[n / 2] + values[n / 2 + 1]) / 2.0 ;
    else
        return values[n / 2 + 1];
}

I'm a beginner in C++ and I don't understand why this is working like that. I'd rather expect it to put declarations of methods like:

double median(double values[], int count);

In *.h file and the interior of the method in *.cpp file and then include the *.h file in my main.cpp like that:

#include "MathFunctions.h"

Can someone explain me why this is working like that? What am I supposed to do with the files created by CLion? Should I include MathFunctions.cpp instead of header file in my main.cpp?


Solution

  • Unfortunately this is a CLion bug, tracked by https://youtrack.jetbrains.com/issue/CPP-9329, please create a Jetbrains account and vote for this issue.

    I am not sure what you mean by "workaround". To make your code working, you need to

    1. Create the function declaration for double median(double values[], int count); in the MathFunctions.h include file.
    2. In your main.cpp, add #include "MathFunctions.h as you mention
    3. In CMakeLists.txt (and this is not CLion specific, you need to know the basics of CMake), add file MathFunctions.cpp to the list of source files for your executable.

    For example, assuming you have a CLion-created CMakeLists.txt, you should have something similar to

    set(SOURCE_FILES main.cpp MathFunctions.cpp)
    add_executable(foo ${SOURCE_FILES})
    

    Some other random comments:

    • median() is not a "method", it is a "function", or more precisely a "free-standing function" (that is, a function that is not part of a class).
    • median() is not really C++, is C, since it uses a low-level C array. You may want to use a std::vector<double> instead.
    • C and C++ are low-level and, depending from which languages you are coming from, can be confusing :-) I suggest picking a good, recent book on modern C++. Modern C++ refers to at least C++11 and a style of programming where you almost never perform direct memory allocation/deallocation (that is, you don't use new and delete). Good luck in your journey!

    Edit

    Changed link to Jetbrains bug to the first one reporting the issue.