对于学校作业,我必须使用 Borland C++ Builder 在 C++ 中实现一个项目。
由于 VCL 对所有 GUI 组件使用 AnsiString,我必须将所有 std::strings 转换为 AnsiString 以便显示。
std::string inp = "Hello world!";
AnsiString outp(inp.c_str());
当然可以,但是我想避免编写和代码重复有点乏味。当我们在其他上下文中使用 Boost 时,我决定提供一些帮助函数 go get boost::lexical_cast 来使用 AnsiString。到目前为止,这是我的实现:
std::istream& operator>>(std::istream& istr, AnsiString& str) {
istr.exceptions(std::ios::badbit | std::ios::failbit | std::ios::eofbit);
std::string s;
std::getline(istr,s);
str = AnsiString(s.c_str());
return istr;
}
一开始我在访问冲突之后得到了访问冲突,但是由于我添加了 .exceptions() 东西,所以图片变得更清晰了。执行转换时,我得到以下异常:
ios_base::eofbit set [Runtime Error/std::ios_base::failure]
有谁知道如何解决它并可以解释为什么会发生错误?我的 C++ 经验非常有限。
反过来,转换例程将是:
std::ostream& operator<<(std::ostream& ostr,const AnsiString& str) {
ostr << (str.c_str());
return ostr;
}
也许有人也会在这里发现错误:)
最诚挚的问候!
编辑:
目前我正在使用 Jem 的编辑版本,它在一开始就可以工作。在使用该程序一段时间后,Borland Codeguard 在已经释放的区域中提到了一些指针算法。任何想法这怎么可能相关?
Codeguard 日志(我使用的是德文版本,译文标有星号):
------------------------------------------
Fehler 00080. 0x104230 (r) (Thread 0x07A4):
Zeigerarithmetik in freigegebenem Speicher: 0x0241A238-0x0241A258. **(pointer arithmetic in freed region)**
| d:\program files\borland\bds\4.0\include\dinkumware\sstream Zeile 126:
| { // not first growth, adjust pointers
| _Seekhigh = _Seekhigh - _Mysb::eback() + _Ptr;
|> _Mysb::setp(_Mysb::pbase() - _Mysb::eback() + _Ptr,
| _Mysb::pptr() - _Mysb::eback() + _Ptr, _Ptr + _Newsize);
| if (_Mystate & _Noread)
Aufrufhierarchie: **(stack-trace)**
0x00411731(=FOSChampion.exe:0x01:010731) d:\program files\borland\bds\4.0\include\dinkumware\sstream#126
0x00411183(=FOSChampion.exe:0x01:010183) d:\program files\borland\bds\4.0\include\dinkumware\streambuf#465
0x0040933D(=FOSChampion.exe:0x01:00833D) d:\program files\borland\bds\4.0\include\dinkumware\streambuf#151
0x00405988(=FOSChampion.exe:0x01:004988) d:\program files\borland\bds\4.0\include\dinkumware\ostream#679
0x00405759(=FOSChampion.exe:0x01:004759) D:\Projekte\Schule\foschamp\src\Server\Ansistringkonverter.h#31
0x004080C9(=FOSChampion.exe:0x01:0070C9) D:\Projekte\Schule\foschamp\lib\boost_1_34_1\boost/lexical_cast.hpp#151
Objekt (0x0241A238) [Größe: 32 Byte] war erstellt mit new **(Object was created with new)**
| d:\program files\borland\bds\4.0\include\dinkumware\xmemory Zeile 28:
| _Ty _FARQ *_Allocate(_SIZT _Count, _Ty _FARQ *)
| { // allocate storage for _Count elements of type _Ty
|> return ((_Ty _FARQ *)::operator new(_Count * sizeof (_Ty)));
| }
|
Aufrufhierarchie: **(stack-trace)**
0x0040ED90(=FOSChampion.exe:0x01:00DD90) d:\program files\borland\bds\4.0\include\dinkumware\xmemory#28
0x0040E194(=FOSChampion.exe:0x01:00D194) d:\program files\borland\bds\4.0\include\dinkumware\xmemory#143
0x004115CF(=FOSChampion.exe:0x01:0105CF) d:\program files\borland\bds\4.0\include\dinkumware\sstream#105
0x00411183(=FOSChampion.exe:0x01:010183) d:\program files\borland\bds\4.0\include\dinkumware\streambuf#465
0x0040933D(=FOSChampion.exe:0x01:00833D) d:\program files\borland\bds\4.0\include\dinkumware\streambuf#151
0x00405988(=FOSChampion.exe:0x01:004988) d:\program files\borland\bds\4.0\include\dinkumware\ostream#679
Objekt (0x0241A238) war Gelöscht mit delete **(Object was deleted with delete)**
| d:\program files\borland\bds\4.0\include\dinkumware\xmemory Zeile 138:
| void deallocate(pointer _Ptr, size_type)
| { // deallocate object at _Ptr, ignore size
|> ::operator delete(_Ptr);
| }
|
Aufrufhierarchie: **(stack-trace)**
0x004044C6(=FOSChampion.exe:0x01:0034C6) d:\program files\borland\bds\4.0\include\dinkumware\xmemory#138
0x00411628(=FOSChampion.exe:0x01:010628) d:\program files\borland\bds\4.0\include\dinkumware\sstream#111
0x00411183(=FOSChampion.exe:0x01:010183) d:\program files\borland\bds\4.0\include\dinkumware\streambuf#465
0x0040933D(=FOSChampion.exe:0x01:00833D) d:\program files\borland\bds\4.0\include\dinkumware\streambuf#151
0x00405988(=FOSChampion.exe:0x01:004988) d:\program files\borland\bds\4.0\include\dinkumware\ostream#679
0x00405759(=FOSChampion.exe:0x01:004759) D:\Projekte\Schule\foschamp\src\Server\Ansistringkonverter.h#31
------------------------------------------
Ansistringkonverter.h 是带有已发布运算符的文件,第 31 行是:
std::ostream& operator<<(std::ostream& ostr,const AnsiString& str) {
ostr << (str.c_str()); **(31)**
return ostr;
}
谢谢你的帮助 :)