这里有一些我一直在使用的代码
void Category::categoryPagesSet(char**&words, char**&category, char**&subset,
int **&pages, int** &catPages)
{
int index2=1;
int ind[getCatSize()];
for (int i=0;i<getCatSize();i++)
{
ind[i]=1;
}
for (int i=0;i<getWordSize();i++)
{
for (int j=0;j<getCatSize();j++)
{
if(strcmp(words[i], category[j])==0)
{
for(int j=ind[i];j<ind[i]+pages[i][0];j++)
{
catPages[i][j]=pages[i][index2];
index2++;
}
index2=1;
ind[j]+=pages[i][0];
}
}
}
}
我已经在另一种方法中为catPages分配了内存。
void Category::categoryPagesInit(char**&words, char**&category, char**&subset,
int **&pages, int** &catPages)
{
int howManyPages[getCatSize()];
int cap=0;
int index=0;
char**temp;
for (int i=0; i<getCatSize(); i++) {
howManyPages[i]=0;
}
for (int i=0;i<getWordSize();i++)
{
for (int j=0;j<getCatSize();j++)
{
if (strcmp(words[i], category[j])==0)
{
howManyPages[j]+=pages[i][0];
}
}
for (int k=0;k<getSubSize();k++)
{
if (strcmp(words[i], subset[k])==0)
{
howManyPages[getsubsetIndex(k)]+=pages[i][0];
}
}
}
for (int i=0;i<CategorySize;i++)
{
std::cout<<howManyPages[i]<<std::endl;
}
cap=getCatSize();
for (int i=0;i<cap;i++)
{
if(howManyPages[i]==0)
{delete [] category[i];
CategorySize--;
}
}
temp=new char*[getCatSize()];
catPages=new int* [getCatSize()];
for (int i=0; i<cap; i++) {
if (howManyPages[i]!=0)
{
temp[index]=new char[strlen(category[i])+1];
strcpy(temp[index], category[i]);
std :: cout<<howManyPages[i]<<std::endl;
catPages[index]=new int(howManyPages[i]+1);
catPages[index][0]=0;
catPages[index][0]=howManyPages[i];
std::cout<<catPages[index][0]<<std::endl;
index++;
}
}
delete [] category;
category=temp;
}
catPages[i][j]=pages[i][index2];
不断给我一个错误代码为2的错误访问消息。 我很沮丧,因为我不知道该怎么做。请帮帮我。