【发布时间】:2016-01-16 11:44:26
【问题描述】:
在 CLion 中生成代码总是导致在头文件中实现方法,我一直被告知它们应该放在 .cpp 文件中,我该如何改变这种行为,甚至可能吗?
例子:
在包含 main.cpp 和测试类(test.hpp 和 test.cpp)的项目中。
CMake文件如下:
cmake_minimum_required(VERSION 3.3)
project(testClion)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp
test.cpp
test.hpp)
add_executable(testClion ${SOURCE_FILES})
(注意这是clion提供的默认文件,我没改过)
test.hpp
#ifndef TESTCLION_TEST_HPP
#define TESTCLION_TEST_HPP
class test
{
protected:
int test;
};
#endif //TESTCLION_TEST_HPP
test.cpp
#include "test.hpp"
在 test.hpp 或 test.cpp 中按 ALT + INSERT 并生成 getter/setter 只会更改 test.hpp:
test.hpp
#ifndef TESTCLION_TEST_HPP
#define TESTCLION_TEST_HPP
class test
{
public:
int getTest() const
{
return test;
}
void setTest(int test)
{
test::test = test;
}
protected:
int test;
};
#endif //TESTCLION_TEST_HPP
【问题讨论】: