-3

I am new to c++ and trying to compile the simple c++ programm. using vector

#include <iostream>
#include<vector>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#define BUFSIZE 100

using namespace std;

typedef struct AA{
    int a;
    std::string a_str;
}A;
typedef struct BB{
    int b;
    std::string b_str;
    vector<AA> Aobj;
}B;

int main()
{
  B bobj;
  bobj.Aobj[0].a=4;
  bobj.Aobj[0].a_str="DICOM";
  bobj.b_str="LDAP";
  bobj.b_str="DICOM";
  size_t ipos;
  ipos=bobj.Aobj[0].a_str.find("COM");
  if(ipos!=string::npos)
        cout<<"String Found Successfully....";
  else
        cout<<"String Not Found ....";
  return 0;
}

when i compile the program it shows a error message Segmentation fault (core dumped) using Ubuntu OS

Sanjay Kumar
  • 369
  • 6
  • 23

2 Answers2

2

What you need to do is first create an object of type AA and then push it to the vector of B as

AA aa;
aa.a = 4;
aa.a_str = "hello world";
bobj.Aobj.push_back(aa);
chitraketu Pandey
  • 153
  • 1
  • 1
  • 8
1

Add a constructor for B. Also C++ doesn't need struct typedefs like this. Simply

struct B {
    int b;
    std::string b_str;
    std::vector<A> Aobj;

    B() : Aobj(std::vector<A>(1)) {}
};
Shreevardhan
  • 12,233
  • 3
  • 36
  • 50