代码之家  ›  专栏  ›  技术社区  ›  Darky

多重定义…c++vsCode[重复]

  •  0
  • Darky  · 技术社区  · 2 年前

    我需要更改Vs代码中的某些内容吗?

    这些是问题:

    Starting build...
    /usr/bin/g++ -fdiagnostics-color=always -g -o /home/kali/path/snakeMain /home/kali/path/*.cpp -lncurses
    /usr/bin/ld: /tmp/ccCKG1t0.o:/home/kali/path/Snake.h:9: multiple definition of `gameOver'; /tmp/ccJxCKEW.o:/home/kali/path/Snake.h:9: first defined here
    /usr/bin/ld: /tmp/ccCKG1t0.o:/home/kali/path/Snake.h:14: multiple definition of `headX'; /tmp/ccJxCKEW.o:/home/kali/path/Snake.h:14: first defined here
    /usr/bin/ld: /tmp/ccCKG1t0.o:/home/kali/path/Snake.h:14: multiple definition of `headY'; /tmp/ccJxCKEW.o:/home/kali/path/Snake.h:14: first defined here
    /usr/bin/ld: /tmp/ccCKG1t0.o:/home/kali/path/Snake.h:17: multiple definition of `tailX'; /tmp/ccJxCKEW.o:/home/kali/path/Snake.h:17: first defined here
    /usr/bin/ld: /tmp/ccCKG1t0.o:/home/kali/path/Snake.h:17: multiple definition of `tailY'; /tmp/ccJxCKEW.o:/home/kali/path/Snake.h:17: first defined here
    /usr/bin/ld: /tmp/ccCKG1t0.o:/home/kali/path/Snake.h:18: multiple definition of `tailLength'; /tmp/ccJxCKEW.o:/home/kali/path/Snake.h:18: first defined here
    /usr/bin/ld: /tmp/ccCKG1t0.o:/home/kali/path/Snake.h:21: multiple definition of `current_dir'; /tmp/ccJxCKEW.o:/home/kali/path/Snake.h:21: first defined here
    /usr/bin/ld: /tmp/ccCKG1t0.o:/home/kali/path/Snake.h:23: multiple definition of `key'; /tmp/ccJxCKEW.o:/home/kali/path/Snake.h:23: first defined here
    collect2: error: ld returned 1 exit status
    
    Build finished with error(s).
    
     *  The terminal process terminated with exit code: -1. 
     *  Terminal will be reused by tasks, press any key to close it. 
    
    

    我在Linux中使用最新的Vs代码。我今天又安装了一次。我认为问题在于链接错误。

    这是我的Snake.cpp:

    #include <iostream>
    #include <ncurses.h>
    #include <unistd.h>
    #include "Snake.h"
    
    // Setup
    void initTerminal() {
    }
    
    void initSnake() {
    }
    
    // Draw
    void drawPixel(int y, int x) {
    
    }
    
    void drawBorder() {
      // PRINT MAP
    }
    
    void drawSnake() {
    }
    
    // Logic
    bool collidesWithBorder() {
    
    }
    
    bool collidesWithSelf() {  
    }
    
    void moveSnake() {
    }
    
    bool handleKey(int key) {
    }
    
    

    我的snakeMain.cpp:

    #include <iostream>
    #include <ncurses.h>
    #include <unistd.h>
    #include "Snake.h"
    
    int main() {
    //-----here is only code and no #includes------/
    return 0;
    }
    
    

    我的蛇

    // Copyright 2022
    // Author: Darky
    
    #ifndef SNAKE_H_
    #define SNAKE_H_
    
    //Golabal var
      // Used for the Loop
      bool gameOver;
      //field Dimensions
      const int f_width = 40;
      const int f_height = 40;
      // Coordinates for the Head of Snake
      int headX, headY;
      // Coordinates for the Tail, max Length (if we continue develop)
      // inital tailLenght (actual length is in drawSnake())
      int tailX[25], tailY[25];
      int tailLength = 0;
      // Direction param
      enum direction { STOP = 0, LEFT, RIGHT, UP, DOWN};
      direction current_dir;
      // input of the User
      int key;
    
    // Functions
    //  Initialize the Game
      void initTerminal();
      void initSnake();
    
    // Draw the Game
      // Draw a Singel Pixel into Gamefield
      void drawPixel(int y, int x);
      // Draws the Gamefield
      void drawBorder();
      // Draw Snake with tail
      void drawSnake();
    
    // Logic of the Game
      // Check if hit the border
      bool collidesWithBorder();
      // Check if collides with Snaketail
      bool collidesWithSelf();
      // set the direction of the snake
      void moveSnake();
      // pick input and transfer to direction
      bool handleKey(int key);
    
    #endif // SNAKE_H_
    
    

    my tasks.json:

    {
        "tasks": [
            {
                "type": "cppbuild",
                "label": "C/C++: g++ build active file",
                "command": "/usr/bin/g++",
                "args": [
                    "-fdiagnostics-color=always",
                    "-g",
                    "-o",
                    "${fileDirname}/${fileBasenameNoExtension}",
                    "${workspaceFolder}/*.cpp",
                    "-lncurses"
                ],
                "options": {
                    "cwd": "${fileDirname}"
                },
                "problemMatcher": [
                    "$gcc"
                ],
                "group": {
                    "kind": "build",
                    "isDefault": true
                },
                "detail": "Task generated by Debugger."
            }
        ],
        "version": "2.0.0"
    }
    
    

    {
        "files.associations": {
            "iostream": "cpp"
        }
    }
    

    同样,如果我在控制台中启动游戏并用makefile编译它,那么游戏就可以运行了。但是在Vs代码中,我遇到了如上所述的问题。

    谢谢大家抽出时间。

    1 回复  |  直到 2 年前
        1
  •  1
  •   Sam Varshavchik    2 年前

    在头文件中:

    bool gameOver;
    

    此头文件获取 #include .cpp

    #包括 逻辑上等同于将头文件的内容物理插入到 文件

    .cpp 文件定义 gameOver

    这违反了C++的一个定义规则:每个对象只能定义一次。这就是编译失败的原因。

    您需要将所有变量的定义移动到 文件,并使用 extern 头文件中的关键字 声明 .cpp 文件夹。

    有关所谓“转发声明”的更多信息和解释,请参阅您的C++教科书,以及 外部 关键字。