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

电话是:

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

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

    return textureSet.getTextureInfos(id);
}

TextureSet .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方法,该方法也要从getTextureInfos调用TextureSet

这是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;
}

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++ - C++通过多个函数返回类对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20476130/

10-10 22:33