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改一下名字就好了. 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值