【发布时间】:2012-04-30 17:38:45
【问题描述】:
我正在用 C++ 制作一个 OpenGL 游戏。与其他语言相比,我在 C++ 方面相当缺乏经验。无论如何,我为一些图像创建了一个带有“基本”目录的字符串流。然后我将此字符串流作为函数参数传递给构造函数。构造函数附加一个图像文件名,然后尝试加载生成的路径。不过……
D:\CodeBlocks Projects\SnakeRoid\bin\Debug\Texts\ <-- before appending the filename
Ship01.tgacks Projects\SnakeRoid\bin\Debug\Texts\ <-- After.
显然不正确!结果应该是 D:\CodeBlocks Projects\SnakeRoid\bin\Debug\Texts\Ship01.tga
我的代码的相关部分:
std::stringstream concat;
std::string txtFullPath = "Path here";
...
concat.str(""); //Reset value (because it was changed in ...)
concat << texFullPath; //Restore the base path
PS = new PlayerShip(&TexMan, concat); //Call the constructor
构造函数的代码
PlayerShip::PlayerShip(TextureManager * TexMan, std::stringstream &path)
{
texId = 2;
std::cout << path.str(); //First path above
path << "Ship01.tga";
std::cout << path.str(); //Second - this is the messed up one
//Do more fun stuff
}
有人知道为什么它会“覆盖”字符串流中已有的内容吗?
【问题讨论】:
-
很难跟踪您的代码片段。您能否制作一个 short(大约 20 行)、complete(以便我们可以下载和编译它)程序来演示该问题?请参阅sscce.org 了解更多信息。
-
@CppLearner 不是很明显吗?
-
@Seth Carnegie:但描述性更强也无妨。:)
-
我的错,我在发布这个时很着急,有机会我会编辑更好的代码。
-
已编辑。是不是更清楚了?
标签: c++ std stringstream