当我尝试在向量中搜索特定值时,我遇到了分段错误。该向量的类型为Person
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detect);
detector = new FaceDetector.Builder(getApplicationContext())
.setTrackingEnabled(false)
.setProminentFaceOnly(true)
.setMode(FaceDetector.FAST_MODE)
.setMinFaceSize((float) 0.60)
.setLandmarkType(FaceDetector.ALL_CLASSIFICATIONS)
.setClassificationType(FaceDetector.ALL_CLASSIFICATIONS)
.build();
initViews();
}
private void initViews() {
imgTakePicture = (ImageView) findViewById(R.id.imgTakePic);
btnTakePicture = (Button) findViewById(R.id.btnTakePicture);
txtSampleDesc = (TextView) findViewById(R.id.txtSampleDescription);
txtTakenPicDesc = (TextView) findViewById(R.id.textView);
btnTakePicture.setOnClickListener(this);
imgTakePicture.setOnClickListener(this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(TAG, "onActivityResult: this is resyult");
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
launchMediaScanIntent();
try {
processCameraPicture();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Failed to load Image", Toast.LENGTH_SHORT).show();
}
}
}
private void launchMediaScanIntent() {
Log.d(TAG, "launchMediaScanIntent: ");
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(imageUri);
this.sendBroadcast(mediaScanIntent);
}
private void startCamera() {
Log.d(TAG, "startCamera: ");
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Log.d(TAG, "startCamera: 2");
File photo = new File(Environment.getExternalStorageDirectory(), "/videoDIARY/ReferencePic/photo.jpg");
imageUri = Uri.fromFile(photo);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, CAMERA_REQUEST);
}
这是我的搜索功能:
struct Person{
string name;
string address;
string email;
string number;
string SocialSec;
string other;
};
以下是==和<<的运算符重载操作符:
void searchContact(vector<Person> &people) {
string searchTerm;
cout << endl;
cout << "Enter search term: ";
getline(cin, searchTerm);
vector<Person>::iterator it=find(people.begin(), people.end(), searchTerm);
if (it != people.end()){
cout << *it;
}else{
cout << "Element not found\n";
}
}
这就是分段错误的样子:
ostream& operator<<(ostream &stream, const Person &it){
stream << it;
return stream;
}
bool operator==(const Person &lhs, const string &rhs){
return lhs.name == rhs;
}
进行回溯:
Program received signal SIGSEGV, Segmentation fault.
0x00005555555565ae in operator<< (
stream=<error reading variable: Cannot access memory at address 0x7fffff7feff8>,
it=<error reading variable: Cannot access memory at address 0x7fffff7feff0>) at class.cpp:114
114 ostream& operator<<(ostream &stream, const Person &it){
(gdb)
为什么会发生这种情况,我该如何解决这个问题? 是堆栈溢出吗?
编辑:添加运算符&lt;&lt;在原帖中过载以便澄清。
答案 0 :(得分:1)
您的操作员应该打印Person类的基本类型。像这样:
ostream& operator<<(ostream &stream, const Person &it){
stream << "This is the name: " << it.name;
return stream;
}
如果你做流&lt;&lt;它在你的函数内部,它将继续尝试在无限递归调用中打印Person。
答案 1 :(得分:0)
哦,对不起,复制粘贴我有点不对劲。这是我的运营商&lt;&lt;重载:
ostream& operator<<(ostream &stream, const Person &it){
stream << it;
return stream;
}