【问题标题】:allocating an object of abstract class type分配抽象类类型的对象
【发布时间】:2013-09-19 02:08:43
【问题描述】:

我试图在我的 cocos2d-x 应用程序中加入 CCTable 视图。我遵循了 testcpp 的源代码,但我仍然收到此错误,并且不是 100% 确定原因

"分配抽象类类型'GameList'的对象"

这是我的源代码

GameList.h

#ifndef __Squares__GameList__
#define __Squares__GameList__

#include "cocos2d.h"
#include "cocos-ext.h"
#include "GameListScene.h"
#include "GameManager.h"

using namespace cocos2d;

class GameList : public CCLayer, public extension::CCTableViewDataSource, public extension::CCTableViewDelegate

{
public:
    virtual bool init();
    CREATE_FUNC(GameList);

    ~GameList(void);

    CCLabelTTF* titleLabel;
    CCLabelTTF* loginLabel;
    CCLabelTTF* passwordLabel;

    virtual void tableCellTouched(extension::CCTableView* table, extension::CCTableViewCell* cell);
    virtual CCSize tableCellSizeForIndex(extension::CCTableView *table, unsigned int idx);
    virtual unsigned int numberOfCellsInTableView(extension::CCTableView *table);
};

#endif

游戏列表.cpp

USING_NS_CC;
USING_NS_CC_EXT;

bool GameList::init()
{
    if ( !CCLayer::init() )
    {
        return false;
    }

    CCSize size = CCDirector::sharedDirector()->getWinSize();

    CCTableView* tableView = CCTableView::create(this, CCSizeMake(250, 60));
    tableView->setDirection(kCCScrollViewDirectionHorizontal);
    tableView->setPosition(ccp(20,size.height/2-30));
    tableView->setDelegate(this);
    this->addChild(tableView);
    tableView->reloadData();

    return true;
}

GameList::~GameList(void)
{

}

void GameList::tableCellTouched(CCTableView* table, CCTableViewCell* cell)
{
    CCLOG("cell touched at index: %i", cell->getIdx());
}

CCSize GameList::tableCellSizeForIndex(CCTableView *table, unsigned int idx)
{
    return CCSizeMake(60, 60);
}

unsigned int GameList::numberOfCellsInTableView(CCTableView *table)
{
    return 20;
}

任何帮助将不胜感激

谢谢

【问题讨论】:

  • 这不是问题,但是包含两个连续下划线的名称(__Squares__GameList__,带有报复性)和以下划线后跟大写字母的名称保留给实现。不要使用它们。
  • 那些是在我创建每个新文件时创建的。
  • 然后你需要重新配置你正在使用的任何工具来生成这些东西。它们是保留名称。不要使用它们。

标签: c++ uitableview cocos2d-x


【解决方案1】:

您正在继承或使用 CCtableViewDataSource 和 CCTableViewDelegate 类,因此您必须定义它的所有虚拟方法 如下:

# CCTableViewDataSource

virtual CCSize cellSizeForTable(CCTableView *table);

virtual  CCTableViewCell* tableCellAtIndex(CCTableView *table, unsigned int idx);

virtual unsigned int numberOfCellsInTableView(CCTableView *table);

virtual bool hasFixedCellSize();

virtual CCSize cellSizeForIndex(CCTableView *table, unsigned int idx);

# CCTableViewDelegate

virtual void tableCellTouched(CCTableView* table,CCTableViewCell* cell);

【讨论】:

    【解决方案2】:

    最好看看CCTableViewDataSource, CCTableViewDelegate 类中有什么。

    无论如何,我认为您的问题是GameList 类没有实现上述类中的所有虚拟抽象方法。只需检查您错过了什么。

    【讨论】:

      【解决方案3】:

      我发现你必须实现CCTableViewDelegateCCScrollViewDelegate,因为CCTableViewDelegate 继承自CCScrollViewDelegate

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多