C ++ std :: string赋值失败 [英] C++ std::string assignment failure

查看:830
本文介绍了C ++ std :: string赋值失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我在结构中将字符串分配给字符串字段时遇到问题。结构如下:

Hello, i have a problem with assigning string to string field in my structure. Here is structure:

struct CIMUNTYPEDFRAMEWORK_API ConnectionInfo {
    AuthMethod authmethod;
    string certificate;
    bool local;
    string oid;
    string host;
    int port;

    string username;
    string password;

    string proxy_host;
    string proxy_user;
    string proxy_password;
    bool secure;

    ConnectionInfo();
    ~ConnectionInfo();
};





这里有一段代码导致问题:





And here is piece of code causing the "problem":

    ConnectionInfo connection;

connection.authmethod = DIGEST;
connection.host.assign("...");
connection.port = 0;
connection.username.assign("...");
connection.password.assign("...");
connection.secure = false;
connection.certificate.assign("");
connection.local = false;
connection.oid.assign("");
connection.proxy_host.assign("");
connection.proxy_user.assign("");
connection.proxy_password.assign("");





在调试器中通过任何字符串赋值后,我遇到错误:



After stepping over in debugger throught any of string assignments, i have got an error:

Unhandled exception at 0x002a42a8 in AccessMonitor.exe: 0xC0000005: Access violation writing location 0x002f2885.





谢谢你的回答。



Thank you for answers.

推荐答案

我的想法是, ConnectionInfo 和你的问题代码。



我认为它们都是 std :: string 。如果 ConnectionInfo 实现是以二进制形式(静态或动态链接)提供的,那么在构建代码时可能会与编译器设置或std lib实现发生冲突。



如果是这种情况,那么错误实际上在于库的提供者,因为在库接口上放置除基本类型以外的任何东西通常是个坏主意,如stl标准没有规定实现之间的二进制兼容性 [ ^ ]。



可悲的是甚至同一实现的调试和发布版本都可能导致库接口出现问题。
My immediate thought is that there is some conflict between the string types in the implementation of ConnectionInfo and your piece of problem code.

I shall presume they are both std::string. If the ConnectionInfo implementation is provided as a binary (either static or dynamically linked) there may be a conflict with compiler settings or std lib implementations used when building your code.

If this is the case, then the error really lies with the provider of the library, as it is generally a bad idea to put anything but fundamental types on a library interface, as the stl standard does not stipulate binary compatibility between implementations[^].

Sadly, even debug and release versions of the same implementation can cause problems on library interfaces.


这篇关于C ++ std :: string赋值失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆