C + + code?
123456789101112131415161718192021222324252627 |
#include <vector>
#include <string>
#include <iostream>
struct
Person
{
std::string name;
int
age;
std::string bank_ac_no;
Person(
const
std::string& name,
int
years,
const
std::string& ac_no) :
name(name), age(years), bank_ac_no(ac_no) {}
Person() : name(
""
), age(0), bank_ac_no(
""
){}
};
int
main(
int
argc,
char *argv[])
{
struct
Person p1 = {
"A Smith"
, 71,
"5702750"
};
std::vector<Person> people1(0);
people1.push_back(p1);
return
0;
}
|
Run at the time in the push_back that a quote like the following error:
Unhandled exception at 0X50C031CA (Msvcr120d.dll) in test15.exe:0xc0000005:access violation reading location 0x391f9350.
Try, if not push_back custom struct, but push_back a built-in type (such as int,string) will not error.
What the hell is going on? Please advise. Thank you
[Problem added] A new project has been created to copy the above code, so there is no problem. Check the new project and the current project, the configuration is identical.
Answer
It turns out that the other files in my project also define a struct person, and it's good to change the other struct's name.
Std::vector Push_back Error Access violation