字符串到 TStream
我正在尝试将字符串转换为 TStream。 下面的代码在 CopyFrom 行上给出了“抽象错误”消息。 我在这里遇到了一堵砖墙,关于如何解决这个问题有什么想法吗?
procedure StringToStream(const AString: string; out AStream: TStream);
var
SS: TStringStream;
begin
SS := TStringStream.Create(AString);
try
SS.Position := 0;
AStream.CopyFrom(SS, SS.Size); //This is where the "Abstract Error" gets thrown
finally
SS.Free;
end;
end;
I'm attempting to convert a string to a TStream. My code below gives me an "Abstract Error" message on the CopyFrom line. I'm against a brick wall here, any ideas on how to solve this?
procedure StringToStream(const AString: string; out AStream: TStream);
var
SS: TStringStream;
begin
SS := TStringStream.Create(AString);
try
SS.Position := 0;
AStream.CopyFrom(SS, SS.Size); //This is where the "Abstract Error" gets thrown
finally
SS.Free;
end;
end;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以下过程应该完全符合您的要求。 请注意,您对 AStream 的使用负责释放在此过程中创建的实例。 返回父类(在本例中为 tStream)而不是特定的后代是完全可以的。
您还可以将其编码为函数:
The following procedure should do excactly what your looking for. Please note that your usage of AStream is responsible for freeing the instance that is created in this procedure. It is perfectly fine to return the parent class (in this case tStream) rather than the specific descendant.
You can also code this as a function:
AStream 被声明为 OUT 参数,这意味着它没有在过程开始时分配,过程负责为其分配适当的值。
如果我正确解释您的代码,您应该省略 OUT 并确保在调用例程时正确实例化 AStream。
更多显示 StringToStream 调用的代码可能会提供更多线索。
AStream is declared as OUT parameter, which means it isn't assigned at the beginning of the procedure and the procedure is responsible to assign a proper value to it.
If I interpret your code correct, you should omit the OUT and make sure AStream is instantiated properly when you call the routine.
Some more code showing the call of StringToStream may give some more clues.
CopyFrom 调用 ReadBuffer,ReadBuffer 调用 Read,并且 Read 被声明为抽象。 您要传递给 AStream 的流类型是什么? 如果它没有实现 Read,你会在那里得到一个抽象错误。 (当你实例化它时,编译器应该给你一个警告。)
CopyFrom calls ReadBuffer, which calls Read, and Read is declared abstract. What sort of stream are you passing to AStream? If it doesn't implement Read, you'll get an abstract error there. (And the compiler should give you a warning when you instantiate it.)
在我看来,将 AStream 声明为 out 是错误的。 尝试删除掉。
如果这没有帮助,这是我使用的函数:
Declaring AStream as out looks wrong to me. Try removing the out.
If that doesn't help, here is the function I use: