Initializing a const char **
Initializing a const char **
编辑:删除一些无用的代码
我使用IMGUI为我的游戏的GUI库,我正试图实现列表框。然而,我的const char **似乎不能被IMGUI正确读取。
#include <tinydir.h>
#include <tinyxml2.h>
#include <cpplocate/ModuleInfo.h>
#include <cpplocate/cpplocate.h>
#include <iostream>
#include <easylogging++.h>
#include "DmuxCommon.hpp"
#include "Garage.hpp"
#include "client/Game.hpp"
irr::f32 garageRotationRate = irr::f32(1.0f);
irr::f32 gChassisRotation;
irr::f32 gCameraRotation;
int selection = 0;
namespace menu {
const char **Garage::names;
Garage::Garage() :
Gui(),
pMainScreen(Game::device->getSceneManager()->addEmptySceneNode()),
pMoonScreen(Game::device->getSceneManager()->addEmptySceneNode()),
availableChassis(getAvailableChassises()) {
// Prepare double rendering
pRenderTarget = Game::device->getVideoDriver()->addRenderTargetTexture(irr::core::dimension2d<irr::u32>(384, 300), "Moon");
pRenderTextureID = pGUI->createTexture(pRenderTarget);
names = new const char *[availableChassis.size()];
for(unsigned int i = 0; i < availableChassis.size(); ++i) {
names[i] = availableChassis[i].c_str();
}
for(unsigned int i = 0; i < availableChassis.size(); ++i) {
std::cout << names[i] << std::endl; // This shows the content of the const char ** correctly
}
}
void Garage::show() {
//Rendering the node
Game::device->getVideoDriver()->setRenderTarget(pRenderTarget, true, true, irr::video::SColor(255, 120.0f, 120.0f, 120.0f));
pMoonScreen->setVisible(true);
pMainScreen->setVisible(false);
Game::device->getSceneManager()->setActiveCamera(pMoonCam);
Game::device->getSceneManager()->drawAll();
Game::device->getVideoDriver()->setRenderTarget(0, true, true, irr::video::SColor(255, 100, 101, 140));
pMoonScreen->setVisible(false);
pMainScreen->setVisible(true);
Game::device->getSceneManager()->setActiveCamera(pMainCam);
pGUI->updateTexture(pRenderTextureID, pRenderTarget);
if(gChassis == nullptr) {
gChassis = Game::device->getSceneManager()->addMeshSceneNode(Game::device->getSceneManager()->getMesh((std::string(cpplocate::findModule("dmux").value("chassisDir") + "el-camino/el-camino.obj")).c_str()));
gChassis->setParent(pMoonScreen);
gChassis->setPosition(irr::core::vector3df(0, 0, 0));
gChassis->setMaterialFlag(irr::video::EMF_LIGHTING, false);
gChassis->setMaterialFlag(irr::video::EMF_BACK_FACE_CULLING, false);
pMainCam = Game::device->getSceneManager()->addCameraSceneNode(pMoonScreen, irr::core::vector3df(0, 0, 0), irr::core::vector3df(0, 0, 0));
pMoonCam = Game::device->getSceneManager()->addCameraSceneNode(pMoonScreen, irr::core::vector3df(0, 0, -5), irr::core::vector3df(0, 0, 0));
pMoonCam->setTarget(gChassis->getPosition());
gCameraRotation = irr::f32(1.0f);
}
ImGui::SetNextWindowPos(ImVec2(0.0f, 0.0f));
ImGui::SetNextWindowSize(ImVec2(Game::playerSettings.currentWindowSize.first,
Game::playerSettings.currentWindowSize.second - (Game::playerSettings.currentWindowSize.second / 9)));
ImGui::Begin("Customize a combat vehicle");
ImGui::PushItemWidth(120);
ImGui::ListBox("", &selection, names, ((int)(sizeof(names)/sizeof(*names))));
ImGui::PopItemWidth();
ImGui::End();
}
}
编译这个文件与我的项目的其余部分,我得到一个窗口,看起来像这样
https://s4.postimg.org/cyrdl2p8d/DMUX_130.png可以看到,它没有打印const char **的全部内容。但是cout语句正确地打印出了数组的内容应该是
- "捷径"
- "以及"
- "ElCamino"
如果不加引号,它将正确获取第一个值,如图所示。是否有什么错误的我正在做涉及名称变量的初始化?sizeof很奇怪,因为IMGUI的imgui_demo.cpp中的代码使用了这个宏来处理列表框的sizeof
#define IM_ARRAYSIZE(_ARR) ((int)(sizeof(_ARR)/sizeof(*_ARR)))
所以我只是使用原始输入,而不是使用定义。
它正在做你告诉它的事情:在列表框中显示一个选择。你对列表元素数目的计算是错误的。sizeof(names)
与sizeof(*names)
相同,因为它们都是指针。
编辑:输入更好的答案
我看了一整天才找到它!我修改了availableChassis.size()
我设法修复了它,感谢@Ismail Badawi @Paul和@ 120programalarm为我指出了正确的方向。
如果你的数据不是在const char*[]格式,那么,而不是创建一个临时数组,你可以传递一个lambda到ListBox来访问你的字符串直接从你的availablech西斯[]名称
相关文章:
- 在字符串函数中抛出'char const*'实例后调用的终止
- 双重标准?为什么只有 char* const&a = "bla" 的警告?
- 包括"lvtocon.h",未定义对'operator<<(std::ostream&, char const*)的引用
- 为什么 const char* const & = 可以编译"hello"?
- 将字符串文本常量定义为 char const* 和 wchar const*
- 如何将std::wstring转换为char const[]
- "char const * name() const _WEBSOCKETPP_NOEXCEPT_TOKEN_ {"剂量是什么意思
- 使用提升对字符串进行标记化时,将令牌转换为 char* const* 时失败
- 如何从 std::initializer_list<char const* 构建 std::vector<std::string>>
- 将 std::vector<std::string> 转换为 const char* const*
- 引发"char const*"错误的实例后调用的终止
- 将const char * const参数成员分配给新值
- 从'const char**'到'char* const*'的转换无效
- 从 sub_match<常量字符 *>' 转换为'const char *const &'
- char*const和constchar*之间有什么区别?(重复-需要更多澄清)
- 使用工厂方法时编译器错误:无法转换"const std::p air<char* const
- HEVC 解码器端口 Android 警告:从 'signed char*' 到 'char const* 的转换无效*
- 如何从'char const*'中删除常量
- 'char const *str'作为模板参数
- 为什么不;t strlen(.)应为const char*const str,而不是const char*