m0_67644151 2022-05-25 13:13 采纳率: 71.4%
浏览 38
已结题

创建类时LNK2005错误

5.cpp

#include <iostream>
#include <thread>
#include <time.h>
#include <Windows.h>
#include "Header.h"
#include "Pause.cpp"

int main()
{
    using namespace std;
    cout << "\033[37m";    // reset font color to black
    thread skipping(skip);
    introduction.Run();
    return 0;
}

Header.h

// control
#define UP 1
#define DOWN 2
#define LEFT 3
#define RIGHT 4
#define CW 5    // CW: clockwise
#define CCW 6    // CCW: counterclockwise 

// display
extern int start();
extern void cout_in_turn(char letters[], int sum, int sleep);

// pause
extern int skip();

Pause.cpp

#include <condition_variable>
#include <mutex>
#include <thread>
#include <Windows.h>
#include "Header.h"

#define KEY_DOWN(vKey) ((GetAsyncKeyState(vKey) & 0x8000) ? 1:0)    //GetAsyncKeyState函数中的KEY_DOWN
using namespace std;
class SkipThread
{
public:
    SkipThread() = default;

public:
    void Run();
    void Stop();
    void Pause(); //暂停
    void Resume();//恢复
private:
    condition_variable m_cv;
    mutex mu;
    bool m_isStop{ false };
    bool m_isPause{ false };
    atomic<int> num = 0;
};
void SkipThread::Run()
{
    start();
}
void SkipThread::Stop()
{
    m_isStop = true;
}
void SkipThread::Pause()
{
    unique_lock<mutex> lock(mu);
    m_isPause = true;
    m_cv.notify_one();
}
void SkipThread::Resume()
{
    unique_lock<mutex> lock(mu);
    m_isPause = false;
    m_cv.notify_one();
}
SkipThread introduction;

int skip()
{
    while (1)
    {
        if (KEY_DOWN(220))  // 220 is "\"
        introduction.Pause();
        return 0;
    }
}

(Display.cpp)

#include <iostream>
#include <thread>
#include <Windows.h>
#include "Header.h"

int start()
{
    /* This is a Tetris game.
     * You can use your keyboard to control the tetris falling from the top of the screen.
     * 
     * Here is the operating instructions.
     * You can press "\" to skip the instruction.
     * 
     * S: move down
     * A: move left
     * D: move right
     * Q: Rotate 90° counterclockwise
     * E: Rotate 90° clockwise
     * 
     * WARNING: You can't use W to move the tetris up!        (this is shown in yellow)
     */
    using namespace std;
    static char a[23] = { "T""h""i""s"" ""i""s"" ""a"" ""T""e""t""r""i""s"" ""g""a""m""e""." };
    static char b[84] = { "Y""o""u"" ""c""a""n"" ""u""s""e"" ""y""o""u""r"" ""k""e""y""b""o""a""r""d"" ""t""o"" ""c""o""n""t""r""o""l"" ""t""h""e"" ""t""e""t""r""i""s"" ""f""a""l""l""i""n""g"" ""f""r""o""m"" ""t""h""e"" ""t""o""p"" ""o""f"" ""t""h""e"" ""s""c""r""e""e""n""." };
    static char c[36] = { "H""e""r""e"" ""i""s"" ""t""h""e"" ""o""p""e""r""a""t""i""n""g"" ""i""n""s""t""r""u""c""t""i""o""n""s""." };
    static char d[43] = { "Y""o""u"" ""c""a""n"" ""p""r""e""s""s"" ""\"""\\""\""" ""t""o"" ""s""k""i""p"" ""t""h""e"" ""i""n""s""t""r""u""c""t""i""o""n""." };
    static char e[13] = { "S"":"" ""m""o""v""e"" ""d""o""w""n" };
    static char f[13] = { "A"":"" ""m""o""v""e"" ""l""e""f""t" };
    static char g[14] = { "D"":"" ""m""o""v""e"" ""r""i""g""h""t" };
    static char h[33] = { "Q"":"" ""r""o""t""a""t""e"" ""9""0""d""e""g"" ""c""o""u""n""t""e""r""c""l""o""c""k""w""i""s""e" };
    static char j[26] = { "E"":"" ""r""o""t""a""t""e"" ""9""0""d""e""g"" ""c""l""o""c""k""w""i""s""e" };
    static char k[49] = { "W""A""R""N""I""N""G"":"" ""Y""o""u"" ""c""a""n""\'""t"" ""u""s""e"" ""\"""W""\""" ""t""o"" ""m""o""v""e"" ""t""h""e"" ""t""e""t""r""i"" ""u""p""!" };

    // Cout every letter in each array in turn
    cout_in_turn(a, 23, 800);
    cout_in_turn(b, 84, 1000);
    cout << endl;
    cout_in_turn(c, 36, 400);
    cout_in_turn(d, 43, 1000);
    cout << endl;
    thread t1(skip);
    cout_in_turn(e, 13, 200);
    cout_in_turn(f, 13, 200);
    cout_in_turn(g, 14, 200);
    cout_in_turn(h, 33, 200);
    cout_in_turn(j, 26, 600);
    cout << endl;
    for (int i = 0; i <= 48; i++)
    {
        cout << "\033[33m" << k[i];    // turn to yellow
        Sleep(40);
    }
    Sleep(1000);
    cout << "\033[37m" << endl;        // trun back to white

    return 0;
}

void cout_in_turn(char letters[], int sum, int sleep)
{
    using namespace std;
    for (int i = 0; i <= sum - 1; i++)
    {
        cout << letters[i];
        Sleep(40);
    }
    Sleep(sleep);
    cout << endl;
}

img

请问是哪里出了什么问题?

  • 写回答

1条回答 默认 最新

  • 浪客 2022-05-25 14:04
    关注
    pause.cpp的内容前后添加
    #ifdef _PAUSE_
    #undef  _PAUSE_
    
    #include <condition_variable>
    #include <mutex>
    #include <thread>
    #include <Windows.h>
    #include "Header.h"
    ...
    
    #endif
    
    
    5.cpp中include前面加一句
    #define  _PAUSE_
    
    你的pause.cpp被多次引用编译导致的重定义问题。文件结构不是很好。
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 6月2日
  • 已采纳回答 5月25日
  • 创建了问题 5月25日

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)