Я пытаюсь передать пользовательский объект (TextureInfos
) через многократные функции.C++ возвращающий объект класса через множественную функцию
Вызов:
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++, и я думаю, что я пропустил что-то с оператором «&» и «*» и т. Д. Поскольку этот код не работает на данный момент ...
Любая помощь? Большое спасибо.
EDIT:
Ok поэтому цель этого кода, чтобы получить TextureInfos
объект в конце процесса. Для этого мне нужно вызвать метод getTextureInfos
из TextureManager, который также вызывает 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;
};
Спасибо за ваши объяснения. Значение «по значению» означает передать объект класса в параметрах функции, например ссылку? – ApheX
@ApheX Нет, просто верните его без указателей или ссылок, поэтому возвращайте тип как простой «TextureInfos», а затем объявите переменную как простой «textureInfos», а не указатель. –
Спасибо, я использую простой объект вместо указателя. Но при этом я получил еще одну ошибку. Я редактировал свой вопрос. – ApheX