源文件
Gun.h
#pragma once
#include <string>
class Gun
{
private:
/* data */
int bullet_count;
std::string type;
public:
Gun(std::string type){
this->bullet_count = 0;
this->type = type;
}
void addBullet(int num);
void shoot();
~Gun();
};
Gun.cpp
#include "../include/Gun.h"
#include <iostream>
void Gun::addBullet(int num){
this->bullet_count = num;
}
void Gun::shoot(){
if(this->bullet_count<=0){
std::cout<<"there is no bullet, please add bullet" <<std::endl;
}else{
this->bullet_count--;
std::cout<<"shoot"<<std::endl;
}
}
Gun::~Gun(){
}
Soldier.h
#pragma once
#include <string>
#include "Gun.h"
class Soldier
{
private:
/* data */
std::string name;
Gun* ptr_gun;
public:
Soldier(std::string name);
~Soldier();
void addBulletToGun(int num);
void fire();
void addGun(Gun* gun);
};
Soldier.cpp
#include <iostream>
#include "../include/Soldier.h"
// #include "Gun.h"
Soldier::Soldier(std::string name){
this->name = name;
this->ptr_gun = nullptr;
}
void Soldier::addGun(Gun* gun){
this->ptr_gun = gun;
}
void Soldier::addBulletToGun(int num){
this->ptr_gun->addBullet(num);
}
void Soldier::fire(){
this->ptr_gun->shoot();
}
Soldier::~Soldier()
{
if(this->ptr_gun == nullptr){
return;
}else{
delete this->ptr_gun;
this->ptr_gun=nullptr;
}
}
CMakeLists编辑
cmake_minimum_required(VERSION 3.0)
project(SOLIDER)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O2 -Wall")
include_directories(include)
add_executable(my_cmake_exe main.cpp src/Gun.cpp src/Soldier.cpp)
- CMakeLists.txt常见必选项
- cmake_minimum_required(VERSION 3.0)
- 设置CMAKE最低支持的版本
- project(SOLIDER)
- 设置项目名称
- set(CMAKE_CXX_FLAGS “${CMAKE_CXX_FLAGS} -g -O2 -Wall”)
- 设置编译时的选项,-g最后的执行文件可以debug; -O2代码优化; -Wall 显示所有警告
- include_directories(include)
- 将include文件夹内容可以被识别到
- add_executable(my_cmake_exe main.cpp src/Gun.cpp src/Soldier.cpp)
- 相当于执行g++ main.cpp src/Gun.cpp src/Soldier.cpp -o my_cmake_exe
**上述set设置可能在debug时会有冲突,可进行以下修改
**
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
set(CMAKE_BUILD_TYPER Debug)
配置json
launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++ - 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/my_cmake_exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "Build",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
主要修改模板中的
# 可执行文件
"program": "${workspaceFolder}/build/my_cmake_exe"
# 详细编译配置在task.json中设置
"preLaunchTask": "Build",
task.json
{
"version": "2.0.0",
"options":{
"cwd": "${workspaceFolder}/build"
},
"tasks": [
{
"type": "shell",
"label": "cmake",
"command": "cmake",
"args": [
".."
]
},
{
"label": "make",
"group": {
"kind":"build",
"isDefault": true
},
"command": "make",
"args": [
]
},
{
"label": "Build",
"dependsOrder": "sequence",
"dependsOn":[
"cmake",
"make"
]
}
]
}
相当于自动运行cmake …
make