【发布时间】:2016-01-23 17:24:51
【问题描述】:
这是我实际代码的缩短版本。这也不运行。 线
tempPurchaseAndID[0] = tempPurchase;
导致崩溃。
#include<vector>
#include<iostream>
using namespace std;
int main()
{
string* strTempPurchase = new string("", "");
string* tempOneID = new string("", "");
vector<string> temp;//if category name exists in his purchase
vector<string*/*size of two*/> tempPurchase;
vector<string*/*size of two*/> oneID;// vector for one id
vector<string*>* tempPurchaseAndID{};
tempOneID[0] = "2222";
oneID.push_back(tempOneID);
strTempPurchase[0] ="milk";
strTempPurchase[1] = "3";
tempPurchase.push_back(strTempPurchase);
tempPurchaseAndID[0] = tempPurchase;
tempPurchaseAndID[1] = oneID;
std::cin.get();
return 0;
}
有人可以看到问题并提出解决方案吗? 提前致谢。
附:我需要其他事情的指针,所以我不能删除指针
【问题讨论】:
-
不要到处使用这些指针!这可能会解决您的问题。
-
但我的项目中的其他事情需要它
-
听起来很奇怪你应该需要它们,但通常你不会。您的实际项目可能存在严重的设计缺陷。
-
你没有为
tempPurchaseAndID分配任何内存,但是你试图覆盖它的元素。 -
那我该如何解决呢?
标签: c++ vector stl crash assign