std::vector push_back报错Access violation

C/C++ code
 
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#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;
}




运行的时候在push_back那一句报如下的错误: 
Unhandled exception at 0x50C031CA (msvcr120d.dll) in Test15.exe: 0xC0000005: Access violation reading location 0x391F9350.

试了一下,如果不是push_back自定义的struct,而是push_back一个内置类型(比如int,string)就不会报错.

到底是怎么回事那? 请指教. 谢谢

[问题补充] 新建了一个项目, 把上面的代码拷贝过去,就没有问题. 检查了一下新项目和现在的项目,配置是一模一样的.

[答案]

原来我项目中的其他文件中也定义了一个struct Person, 把其他的一个struct改一下名字就好了. 

原文地址:https://www.cnblogs.com/time-is-life/p/9542635.html