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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀