C++ 从链表数组访问节点数据
我正在做一个相当冗长的任务,目前有一个致命的问题。我无法正确实现对存储在指针中的链表中的数据的节点访问 起初,我得到了一个分割错误,但经过一些研究,如,我能够澄清这一点。现在我已经修复了分段错误,我的代码甚至无法编译 我已经“cout”调试到我知道问题存在于我的C++ 从链表数组访问节点数据,c++,arrays,pointers,linked-list,C++,Arrays,Pointers,Linked List,我正在做一个相当冗长的任务,目前有一个致命的问题。我无法正确实现对存储在指针中的链表中的数据的节点访问 起初,我得到了一个分割错误,但经过一些研究,如,我能够澄清这一点。现在我已经修复了分段错误,我的代码甚至无法编译 我已经“cout”调试到我知道问题存在于我的Cache.h文件中,特别是在我尝试将新节点p设置为某个数组值的起始节点时,在我的addElement函数中。试图清晰地评论 因为我的数组是一个链表数组,所以它本质上是一个指针数组。类型声明为节点*。我觉得p应该是指向指针的指针,因为它不
Cache.h
文件中,特别是在我尝试将新节点p
设置为某个数组值的起始节点时,在我的addElement
函数中。试图清晰地评论
因为我的数组是一个链表数组,所以它本质上是一个指针数组。类型声明为节点*
。我觉得p应该是指向指针的指针,因为它不是任何列表的一部分,需要通过数组访问数据。我已经尝试了大约一百万个指针和指向指针的指针的组合,但是没有任何东西允许我再次编译。我已经完成了所有我知道怎么做的调试,所以我求助于你
我将在下面发布代码<代码>命令。在中,基本上包含我将要使用的文件的名称A.data
是我正在对其数据进行操作的文件(min
for ex)Process.cpp
包含了我的主要功能,以及基本操作,而Cache.h
的存在就是为了拥有这个指针
由于问题肯定存在于Cache.h
中,因此我将首先发布该问题,然后发布所有其他问题。同样,错误位置应该是显而易见的。希望这是我对指针的理解上的一些错误和一个简单的解决方法
谢谢
Cache.h`
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
//CLASS TO STORE SIMPLE "CACHE"
class Cache
{
private:
struct node
{
double val;
string cacheName;
string cacheOp;
node * next;
};
node * cacheArr[26];
public:
//CONSTRUCTOR, SET VALUES OF cacheArr TO NULL
Cache()
{
for (int i = 0; i < 26; i++)
{
cacheArr[i] = new node;
}
}
//ADD AN ELEMENT TO CACHE
void addElement (string file, string op, double value)
{
node** p;
char ch = file[0];
//convert
int alphaNum = (int)(ch - 'A');
cout << "\nALPHA NUM = " << alphaNum << " -- ch = " << ch << endl;
cout << "TEST PRE";
/*
HERE LIES THE PROBLEM
vvvvvvvvvvvvvvvvvvvvv
*/
p* = cacheArr[alphaNum];
cout << "\nTEST";
if (p == NULL)
{
//no values starting with 'ch' have been used
//create new node
p->cacheName = file;
p->cacheOp = op;
p->val = value;
p->next = NULL;
return;
}
else if (p->next == NULL)
{
//list has one value, add to list if so
p = p->next;
p->cacheName = file;
p->cacheOp = op;
p->val = value;
p->next = NULL;
return;
}
//list has 2+ elements
//bring p to last element
while(p!=NULL)
{
p = p->next;
}
` //p is at end of list
p->cacheName = file;
p->cacheOp = op;
p->val = value;
p->next = NULL;
return;
}
//checks to see if file already in cache
bool checkCache(string file, string op)
{
char ch = file[0];
int numAlpha = (int)(ch - 'A' + 1);
node* p = cacheArr[numAlpha];
if(p == NULL)
{
return false;
}
while(p!=NULL)
{
if(p->cacheName == file && p->cacheOp == op)
{
return true;
}
p = p->next;
}
return false;
}
//RETURNS VALUE OF CACHE
double getVal(string file, string op)
{
char ch = file[0];
int numAlpha = (int)(ch - 'A' + 1);
node * p = cacheArr[numAlpha];
while (p!=NULL)
{
if(p->cacheName == file && p->cacheOp == op)
{
return p->val;
}
p = p->next;
}
return 0;
}
};
//END OF CLASS
A.数据`
20
20
30
30
40
40
`
忘了,我得到的错误代码`
[dilleyj@cs1 HW6]$ g++ -Wall -o process Process.cpp
In file included from Process.cpp:6:0:
Cache.h: In member function ‘void Cache::addElement(std::string, std::string, double)’:
Cache.h:59:7: error: expected primary-expression before ‘=’ token
p* = cacheArr[alphaNum];
^
Cache.h:67:8: error: request for member ‘cacheName’ in ‘* p’, which is of pointer type ‘Cache::node*’ (maybe you meant to use ‘->’ ?)
p->cacheName = file;
^
Cache.h:68:8: error: request for member ‘cacheOp’ in ‘* p’, which is of pointer type ‘Cache::node*’ (maybe you meant to use ‘->’ ?)
p->cacheOp = op;
^
Cache.h:69:8: error: request for member ‘val’ in ‘* p’, which is of pointer type ‘Cache::node*’ (maybe you meant to use ‘->’ ?)
p->val = value;
^
Cache.h:70:8: error: request for member ‘next’ in ‘* p’, which is of pointer type ‘Cache::node*’ (maybe you meant to use ‘->’ ?)
p->next = NULL;
^
Cache.h:73:16: error: request for member ‘next’ in ‘* p’, which is of pointer type ‘Cache::node*’ (maybe you meant to use ‘->’ ?)
else if (p->next == NULL)
^
Cache.h:76:14: error: request for member ‘next’ in ‘* p’, which is of pointer type ‘Cache::node*’ (maybe you meant to use ‘->’ ?)
p = p->next;
^
Cache.h:77:9: error: request for member ‘cacheName’ in ‘* p’, which is of pointer type ‘Cache::node*’ (maybe you meant to use ‘->’ ?)
p->cacheName = file;
^
Cache.h:78:9: error: request for member ‘cacheOp’ in ‘* p’, which is of pointer type ‘Cache::node*’ (maybe you meant to use ‘->’ ?)
p->cacheOp = op;
^
Cache.h:79:9: error: request for member ‘val’ in ‘* p’, which is of pointer type ‘Cache::node*’ (maybe you meant to use ‘->’ ?)
p->val = value;
^
Cache.h:80:9: error: request for member ‘next’ in ‘* p’, which is of pointer type ‘Cache::node*’ (maybe you meant to use ‘->’ ?)
p->next = NULL;
^
Cache.h:88:13: error: request for member ‘next’ in ‘* p’, which is of pointer type ‘Cache::node*’ (maybe you meant to use ‘->’ ?)
p = p->next;
^
Cache.h:93:8: error: request for member ‘cacheName’ in ‘* p’, which is of pointer type ‘Cache::node*’ (maybe you meant to use ‘->’ ?)
p->cacheName = file;
^
Cache.h:94:8: error: request for member ‘cacheOp’ in ‘* p’, which is of pointer type ‘Cache::node*’ (maybe you meant to use ‘->’ ?)
p->cacheOp = op;
^
Cache.h:95:8: error: request for member ‘val’ in ‘* p’, which is of pointer type ‘Cache::node*’ (maybe you meant to use ‘->’ ?)
p->val = value;
^
`
至少有两个问题
p*=cacheArr[alphaNum]代码>应该是*p=cacheArr[alphaNum]代码>
然后,在同一个函数中,p->
应该是(*p)->
,因为p
是指向指针的指针。我已经调试过,我提供了一个示例,我已经找到了错误的确切位置,现在我想知道是否有人可以看一眼,看看它是否只是实现错误。如果我知道C++中的任何一条代码,我就可以认识到这个问题是错误的。在我发表这篇文章后不到10秒,你就发表了这篇评论。我觉得我自己已经做了足够多的工作,值得在一分钟的时间里请求一个善良的灵魂快速地看一眼。它不应该是星号p而不是星号p吗?@JeffreyDilley“你在我发表这篇文章后不到10秒就发表了这条评论”当然,这是我经常使用的股票评论。乍一看,不清楚您是否遇到了基本的编译器错误,也不清楚您是否缺乏调试工作,因为这是VLQ问题中常见的问题。您应该学习如何正确解释编译器错误。在这里问这样的问题没有多大价值。@JeffreyDilley请采用统一的格式。有时你不放空格,有时放,甚至两次。新行也是这样。缩进是很奇怪的。像你所关心的那样编写代码会让你成为一个更好的程序员。除非你想让强迫症患者受苦我不再看代码了。。。p*=cacheArr[alphaNum];还是您打算取消对赋值运算符的引用?
20
20
30
30
40
40
[dilleyj@cs1 HW6]$ g++ -Wall -o process Process.cpp
In file included from Process.cpp:6:0:
Cache.h: In member function ‘void Cache::addElement(std::string, std::string, double)’:
Cache.h:59:7: error: expected primary-expression before ‘=’ token
p* = cacheArr[alphaNum];
^
Cache.h:67:8: error: request for member ‘cacheName’ in ‘* p’, which is of pointer type ‘Cache::node*’ (maybe you meant to use ‘->’ ?)
p->cacheName = file;
^
Cache.h:68:8: error: request for member ‘cacheOp’ in ‘* p’, which is of pointer type ‘Cache::node*’ (maybe you meant to use ‘->’ ?)
p->cacheOp = op;
^
Cache.h:69:8: error: request for member ‘val’ in ‘* p’, which is of pointer type ‘Cache::node*’ (maybe you meant to use ‘->’ ?)
p->val = value;
^
Cache.h:70:8: error: request for member ‘next’ in ‘* p’, which is of pointer type ‘Cache::node*’ (maybe you meant to use ‘->’ ?)
p->next = NULL;
^
Cache.h:73:16: error: request for member ‘next’ in ‘* p’, which is of pointer type ‘Cache::node*’ (maybe you meant to use ‘->’ ?)
else if (p->next == NULL)
^
Cache.h:76:14: error: request for member ‘next’ in ‘* p’, which is of pointer type ‘Cache::node*’ (maybe you meant to use ‘->’ ?)
p = p->next;
^
Cache.h:77:9: error: request for member ‘cacheName’ in ‘* p’, which is of pointer type ‘Cache::node*’ (maybe you meant to use ‘->’ ?)
p->cacheName = file;
^
Cache.h:78:9: error: request for member ‘cacheOp’ in ‘* p’, which is of pointer type ‘Cache::node*’ (maybe you meant to use ‘->’ ?)
p->cacheOp = op;
^
Cache.h:79:9: error: request for member ‘val’ in ‘* p’, which is of pointer type ‘Cache::node*’ (maybe you meant to use ‘->’ ?)
p->val = value;
^
Cache.h:80:9: error: request for member ‘next’ in ‘* p’, which is of pointer type ‘Cache::node*’ (maybe you meant to use ‘->’ ?)
p->next = NULL;
^
Cache.h:88:13: error: request for member ‘next’ in ‘* p’, which is of pointer type ‘Cache::node*’ (maybe you meant to use ‘->’ ?)
p = p->next;
^
Cache.h:93:8: error: request for member ‘cacheName’ in ‘* p’, which is of pointer type ‘Cache::node*’ (maybe you meant to use ‘->’ ?)
p->cacheName = file;
^
Cache.h:94:8: error: request for member ‘cacheOp’ in ‘* p’, which is of pointer type ‘Cache::node*’ (maybe you meant to use ‘->’ ?)
p->cacheOp = op;
^
Cache.h:95:8: error: request for member ‘val’ in ‘* p’, which is of pointer type ‘Cache::node*’ (maybe you meant to use ‘->’ ?)
p->val = value;
^
`