问题描述
我有一个具有以下虚函数的类
I have a class that has the following virtual function
namespace Book
{
class Secure
{
virtual Result setPassword(const std::map<std::string, std::vector<std::string> >& params)=0;
}
}
我还有另一个测试类,其中一种方法从excel获取密码和用户名,并将其设置为map secureParams并设置当前文件的密码。
I have another test class in which a method gets the password and user name from the excel and sets it to the map secureParams and sets the password for the current file.
bool initialize(std::string bookPath, std::string loginPath, std::string userName, std::string password)
{
Book::SharedPtr<Book::Secure> secure;
bool result;
std::map<std::string, std::vector<std::string> > secureParams;
std::vector<std::string> userNames;
std::vector<std::string> passwords;
userNames.push_back(userName);
passwords.push_back(password);
secureParams.insert(std::pair<std::string, std::vector<std::string>>("USERNAME",userNames);
secureParams.insert(std::pair<std::string, std::vector<std::string>>("PASSWORD",passwords);
secure->setPassword(secureParams);
secure->getLoginFile(loginPath.c_str());
result=Book::SecureBook::canLogin(bookPath.c_str(),secure);
return result;
}
运行时代码终止未知文件:故障
测试主体中引发了未知的C ++异常。
The code when run terminates saying Unknown File: FailureUnknown C++ exception thrown in test body.
在XCode中进行调试时,它显示出EXC_BAD_ACCESS(code = 1,address = 0xc0000000)错误行secure-> setPassword(secureParams);
When debugged in XCode it shows a EXC_BAD_ACCESS(code=1,address=0xc0000000) error in the line secure->setPassword(secureParams);
我整天都在尝试调试问题。感谢您的帮助:)
I have been trying to debug the issue all day. Any help is appreciated :)
预先感谢:)
推荐答案
您似乎没有从Book继承的对象::安全,只是抽象的聪明指针ct基类。当取消引用从未设置为指向实际对象的智能指针时,会崩溃。
You don't seem to have an object that inherits from Book::Secure, just a smart pointer to the abstract base class. When you dereference the smart pointer that has never been set to point to an actual object, you get a crash.
说明:
1)您需要一个从Book :: Secure
1) You need an instantiable class that inherits from Book::Secure
namespace Book
{
class Foo : public Secure
{
public:
virtual Result setPassword(const std::map<std::string, std::vector<std::string> >& params) {
cout << "setPassword called" << endl;
}
}
}
2)您需要实例化该类,并在使用智能指针之前使您的智能指针指向它:
2) You need to instantiate the class and make your smart pointer point to it before you use the smart pointer:
secure.reset( new Foo() );
这篇关于将std :: map作为参数传递给虚拟函数调用时,会发生EXC_BAD_ACCESS(code = 1,address = 0x0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!