gcc有多个定义错误

时间:2014-10-19 20:58:28

标签: c++ gcc

我在gcc下工作的项目中遇到奇怪的多重定义错误。我浏览了人们在论坛上抱怨的所有类似错误,但我根据他们的解决方法无法解决问题。我正在linux下使用ncurses库进行蛇游戏 我有一个带有

的point.h
#ifndef POINT_H
#define POINT_H

struct point
{
    int x,y;
    point();
    point(int col,int row);
};
point::point(){
    x=0;
    y=0;
}
point::point(int col,int row)
{
    y=col;
    x=row;
}
#endif

draw.h只有使用ncurses渲染snake的方法 和snake.h

#ifndef SNAKE_H
#define SNAKE_H

#include "point.h"
#include "draw.h"
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <deque>

class snake
{
 private:
  int maxwidth,maxheight;
  float delay,acceleration;
  int old_axis,direction,tailtime,tailcurrent;
  int limit_down,limit_right;
  std::deque<point> dsnake;
  std::deque<point> food;
   char blk,partchr;
 public:
  snake();
  void drawBorders();
  void drawSnake();
  void clearSnake();
  void start();
  void moveSnake(int towards);
  void moveSnake();
  void play();
  void TailAdd();
  point getmovePoint(point pos,int axis);
  void getDirection();
  void generateFood();
  void drawFood();
};
#endif

但是当我编译时我得到错误 我尝试了很多不同的g ++标志。尝试制作点类而不是结构并定义cpp文件中的declerations,但不幸的是它们不起作用。 该项目只有在我包含&#34; snake.cpp&#34;在main.cpp中,这不是一个理智的解决方案 我想。

g++ -std=c++11 main.cpp snake.cpp -lncurses
/tmp/ccCSPcUp.o: In function `point::point()':
snake.cpp:(.text+0x0): multiple definition of `point::point()'
/tmp/cc1BDIIH.o:main.cpp:(.text+0x0): first defined here
/tmp/ccCSPcUp.o: In function `point::point()':
snake.cpp:(.text+0x0): multiple definition of `point::point()'
/tmp/cc1BDIIH.o:main.cpp:(.text+0x0): first defined here
/tmp/ccCSPcUp.o: In function `point::point(int, int)':
snake.cpp:(.text+0x20): multiple definition of `point::point(int, int)'
/tmp/cc1BDIIH.o:main.cpp:(.text+0x20): first defined here
/tmp/ccCSPcUp.o: In function `point::point(int, int)':
snake.cpp:(.text+0x20): multiple definition of `point::point(int, int)'
/tmp/cc1BDIIH.o:main.cpp:(.text+0x20): first defined here
/tmp/ccCSPcUp.o: In function `init()':
snake.cpp:(.text+0x43): multiple definition of `init()'
/tmp/cc1BDIIH.o:main.cpp:(.text+0x43): first defined here
/tmp/ccCSPcUp.o: In function `getMax(bool)':
snake.cpp:(.text+0x85): multiple definition of `getMax(bool)'
/tmp/cc1BDIIH.o:main.cpp:(.text+0x85): first defined here
/tmp/ccCSPcUp.o: In function `drawColumn(int, char)':
snake.cpp:(.text+0xe8): multiple definition of `drawColumn(int, char)'
/tmp/cc1BDIIH.o:main.cpp:(.text+0xe8): first defined here
/tmp/ccCSPcUp.o: In function `drawLine(int, char)':
snake.cpp:(.text+0x140): multiple definition of `drawLine(int, char)'
/tmp/cc1BDIIH.o:main.cpp:(.text+0x140): first defined here
collect2: error: ld returned 1 exit status
  

这是我的其他文件。

     

http://pastie.org/private/gpzbuohvlxhzyz5z4vjsw draw.h   http://pastie.org/private/pfvriwssdzwreab5ibyww main.cpp   http://pastie.org/private/ehkyvcodge7w1wqwpsbw point.h   http://pastie.org/private/eomqodk9di3tj1ramyfg snake.cpp   http://pastie.org/private/wznpkpayh9iopvjnrdfjw snake.h

1 个答案:

答案 0 :(得分:0)

这在标题中不正确:

point::point(){
    x=0;
    y=0;
}

如果两个不同的单位中包含相同的函数,则此函数体被定义两次,违反了一个定义规则。

函数体在整个程序中最多只能有一个非内联定义。允许多个内联定义,只要它们完全相同即可。 (在这两种情况下,每个翻译单元仍然只能有至多一个定义。)

要解决此问题,请:

  • 在类定义
  • 中定义内联函数
  • 将该功能移至.cpp文件

其他构造函数也是如此。

相关问题