【问题标题】:C++ inheritance issue error C2084 function already has a bodyC++ 继承问题错误 C2084 函数已经有一个主体
【发布时间】:2017-02-24 05:24:23
【问题描述】:

我在 c++ 中得到了以下代码...

如果我从 Actor 中删除继承,一切都很好。

我尝试查找问题并发现了类似的问题,但没有解决此问题。 我也多次清理和重建该项目。这可能只是我找不到的某个地方的愚蠢错误......

杜克.h

#pragma once

#include "cocos2d.h"
#include <string.h>
#include "Actor.h"
#include "HelloWorldScene.h"
#include "AssetManager.h"

class Duke : public Actor {

private:
    AssetManager* assets;
public:

    Duke() :Actor() {};
    ~Duke();
    void update(float) override;
    virtual bool init() override;

};

杜克.cpp

#include "Duke.h"
#include "cocos2d.h"



Duke::Duke() {}

Duke::~Duke()
{
}
void Duke::update(float)
{
}

bool Duke::init()
{
    return false;
}

这是我刚刚创建的一个非常简单的类,但是我收到一个关于函数的错误(我什至不相信它存在)而且我现在真的一无所知......

2>------ Build started: Project: MyCppGame, Configuration: Debug Win32 ------
2>Duke.cpp
2>g:\cpp\projects\corgi\mycppgame\classes\duke.cpp(6): error C2084: function 'Duke::Duke(void)' already has a body
2>g:\cpp\projects\corgi\mycppgame\classes\duke.h(15): note: see previous definition of '{ctor}'
2>Done building project "MyCppGame.vcxproj" -- FAILED.
========== Build: 1 succeeded, 1 failed, 4 up-to-date, 0 skipped ==========

(如果需要,这里是 Actor 类声明)

演员.h

#pragma once

#include "cocos2d.h"
#include <string.h>
USING_NS_CC;
class Actor : public cocos2d::Node {
private:
    Sprite* spr;
public:
    Actor(std::string*);

    Actor();

    ~Actor();

};

演员.cpp

#include "Actor.h"
#include "cocos2d.h"
#include <string>
USING_NS_CC;
Actor::Actor(std::string* str)
{
    spr = Sprite::createWithSpriteFrameName(*str);
    this->addChild(spr);
    this->setPosition(Vec2(100, 100));
    this->setScale(4);
    Texture2D::TexParams texParams = { GL_NEAREST, GL_NEAREST, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE };
    spr->getTexture()->setTexParameters(texParams);
}


Actor::Actor()
{
}

Actor::~Actor()
{
}

谢谢

【问题讨论】:

    标签: c++ inheritance constructor compiler-errors parent-child


    【解决方案1】:

    看看你的Duke 构造函数。您在头文件和源文件中都有定义(用函数体实现)。

    要么从源文件中完全删除它。或者从头文件中删除花括号{}。

    【讨论】:

    • 哇,这很简单,我从我的 .h 文件中删除了我的定义中的函数,并将 :actor() 部分移动到我的 .cpp 减速中,我不认为必须有那个.. . 现在我知道了,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-17
    • 2021-06-24
    • 1970-01-01
    相关资源
    最近更新 更多