c ++ struct type重定义错误

时间:2018-01-03 21:48:57

标签: c++ struct header-files

我正在使用SFML库开发简单的游戏C ++游戏。这是我最初使用C ++的努力之一,我遇到了在头文件中定义结构的一些问题。

这是bullet.h:

#pragma once
#include <SFML\Graphics.hpp>

struct BulletTransform {
    sf::RectangleShape shape;
    //details
    BulletTransform(float, float);
};

class Bullet {
//class definition stuff, no problems here

然后我尝试在bullet.cpp文件中创建一个实现:

#include "Bullet.h"

struct BulletTransform {

    sf::RectangleShape shape;
    BulletTransform::BulletTransform(float mX, float mY)
    {
        //constructor for shape stuff
    }
};

现在当我尝试编译它时会抛出一个错误,说明bullet.cpp中的struct是一种类型重新定义。我知道我不能两次定义一个具有相同名称的结构,但我也不确定如何解决这个问题。我是否需要在标题中获得对定义的引用?或者我的实施完全错了?提前谢谢!

2 个答案:

答案 0 :(得分:2)

您已在实施文件中重复了结构定义。不要那样做。相反,为各个成员提供定义,如下所示:

#include "Bullet.h"


BulletTransform::BulletTransform(float mX, float mY)
{
    //constructor for shape stuff
}

答案 1 :(得分:2)

在头文件中,您可以进行声明。在源文件中定义 - 这是一般的经验法则。例如:

在bullet.h中:

title

在bullet.cpp中:

Dim rptGen as WSTestProject.testproject = Nothing
rptGen = New WSTestProject.testproject With {
        .Url = "http://x-xxx-xx-xx-01:8080//TestProject/testproject?wsdl",
        .Timeout = 1200000
        }

通常,你不会在构造函数中做一些繁重的事情 - 只是将数据成员初始化为例如某些默认值。 希望这会有所帮助。

相关问题