C++通过多个函数返回类对象

标签 c++ function visual-c++ pointers

我正在尝试通过多个函数传递自定义对象 (TextureInfos)。

电话是:

TextureManager::Instance()->getTextureInfos("TEST",0)

纹理管理器.cpp

TextureInfos& TextureManager::getTextureInfos(std::string key, int id) 
{
    TextureSet textureSet = textureSets[key];

    return textureSet.getTextureInfos(id);
}

纹理集.cpp

TextureInfos& TextureSet::getTextureInfos(int id) 
{   
    sf::Texture texture;
    sf::IntRect rect;

    if (id < tileCount) {
        int x = (id % maxCol) * tileWidth;
        int y = (id / maxCol) * tileHeight;
        rect.left = x;
        rect.top = y;
        rect.width = tileWidth;
        rect.height = tileHeight;
    }

    TextureInfos *textureInfos = new TextureInfos(texture,rect);

    return textureInfos;
}

我是 C++ 的新手,我想我错过了运算符“&”和“*”等的一些东西。因为这段代码暂时不起作用...

有什么帮助吗? 非常感谢。

编辑:

好的,所以这段代码的目的是在流程结束时获取一个 TextureInfos 对象。为此,我需要从 TextureManager 调用 getTextureInfos 方法,该方法还从 TextureSet 调用 getTextureInfos

这里是TextureManager.cpp的完整代码

#include "TextureManager.h"
#include <iostream>
#include <fstream>

TextureManager TextureManager::m_TextureManager;
const std::string basePath="Assets/Graphics";

#pragma region Constructor
TextureManager::TextureManager()
{
    textureSets.clear();
}

TextureManager::~TextureManager()
{

}
#pragma endregion

#pragma region Textures management

// Charge un set de texture a partir d'un nom de fichier
void TextureManager::LoadTextureset(std::string fileName,std::string key) {

    TextureSet textureSet;
    textureSet.init(basePath + fileName, key);

    textureSets[key] = textureSet;
}

// Récupère une texture de la liste
TextureInfos TextureManager::getTextureInfos(std::string key, int id) 
{
    TextureSet textureSet = textureSets[key];

    return textureSet.getTextureInfos(id); // HERE I GET AN ERROR
}

#pragma endregion

最后注释的那行是我出错的地方:

no suitable user-defined conversion from "TextureInfos" to "TextureInfos" exists.

对于 TextureSet.cpp:

#include "TextureSet.h"
#include <iostream>
#include <fstream>
#include "RapidXML\rapidxml.hpp"
#include "Debug.h"

const std::string basePath="Assets/Graphics";

using namespace rapidxml;

#pragma region Constructor
TextureSet::TextureSet()
{

}

TextureSet::~TextureSet()
{

}
#pragma endregion

void TextureSet::init(std::string l_filePath,std::string l_key)
{
    filePath = l_filePath;
    key = l_key;

    // On détermine les URLs des fichiers
    std::string setDescriptorPath = filePath + ".xml";
    std::string setTilesetPath = filePath + ".png";

    // On charge la texture
    if (!textureSet.loadFromFile(setTilesetPath))
        throw "ça load pas";

    // On lis le xml
    std::ifstream xmlDescriptor(setDescriptorPath);
    if(!xmlDescriptor)
        throw "Could not load tileset: " + setDescriptorPath;

    std::string xmlDescriptorContents;
    {
        std::string line;
        while(std::getline(xmlDescriptor, line))
            xmlDescriptorContents += line;
    }

    std::vector<char> xmlData = std::vector<char>(xmlDescriptorContents.begin(), xmlDescriptorContents.end());
    xmlData.push_back('\0');

    //Create a parsed document with &xmlData[0] which is the char*
    xml_document<> doc;
    doc.parse<parse_no_data_nodes>(&xmlData[0]);

    //Get the root node
    xml_node<>* root = doc.first_node();
    xml_node<>* imagefile = root->first_node("params");

    maxRow = atoi(imagefile->first_attribute("maxRow")->value());
    maxCol = atoi(imagefile->first_attribute("maxCol")->value());
    tileWidth = atoi(imagefile->first_attribute("tileWidth")->value());
    tileHeight = atoi(imagefile->first_attribute("tileHeight")->value());
    tileCount = atoi(imagefile->first_attribute("tileCount")->value());
}

TextureInfos TextureSet::getTextureInfos(int id) 
{   
    sf::Texture texture;
    sf::IntRect rect;

    if (id < tileCount) {
        int x = (id % maxCol) * tileWidth;
        int y = (id / maxCol) * tileHeight;
        rect.left = x;
        rect.top = y;
        rect.width = tileWidth;
        rect.height = tileHeight;
    }

    TextureInfos textureInfos(texture,rect);

    return textureInfos;
}

纹理信息.h

#include <unordered_map>
#include <vector>
#include <SFML\Graphics.hpp>
#include <string>

class TextureInfos
{

private:

protected:

public:
    TextureInfos(sf::Texture& l_texture, sf::IntRect l_textureRect);
    ~TextureInfos();

    sf::Texture& texture;
    sf::IntRect textureRect;
};

最佳答案

不,它不起作用,因为它甚至无法编译。它不编译的原因是因为您试图返回一个指针作为引用。指针和引用是两个不同的东西。

要进行快速、简单和肮脏的修复,请将返回类型从 TextureInfos& 更改为 TextureInfos*


上面概述的快速修复是“脏”的,因为当您使用代码时,您将有内存泄漏(您使用 new 分配内存但不释放它)。

这可以通过两种方式解决:要么返回按值,而不是使用指针/引用。或者使用 std::unique_ptr 等智能指针.

关于C++通过多个函数返回类对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20476130/

相关文章:

javascript - 功能在 Safari 中不起作用

C++ 重载决议总是求助于别名 arg。超过其他人作为arg。对所有人有效

c++ - 如何在 C++ 中从 "public ref class"创建一个公共(public)变量?

c++ - 如何做 makefile 使用单个 makefile 编译不同目录中的多个 .cpp 文件?

c++ - 如何将宏 SIGNAL 和 SLOT 实现为函数 QT

c++ - 使用模板缓冲区概述对象会给出错误的结果

c++ - 切片无状态派生类是否安全?

c++ - 动态数组 C++

c++ - VS 2010 构建错误

c++ - 为函数创建别名