使用类成员变量时访问读取错误
我有一个在头文件中声明了私有成员变量的类。在我的构造函数中,我传入一些文件名并使用这些名称创建其他对象。这很好用。然而,当我尝试添加另一个成员变量并在构造函数中初始化它时,我遇到了访问读取冲突。我把代码发给了别人,在他的电脑上运行得很好。知道可能出什么问题吗?
这是有问题的代码:
.h 文件:
class QUERYMANAGER {
INDEXCACHE *cache;
URLTABLE *table;
SNIPPET *snip;
int* iquery[MAX_QUERY_LENGTH];
int* metapointers[MAX_QUERY_LENGTH];
int blockpointers[MAX_QUERY_LENGTH];
int docpositions[MAX_QUERY_LENGTH];
int numberdocs[MAX_QUERY_LENGTH];
int frequencies[MAX_QUERY_LENGTH];
int docarrays[MAX_QUERY_LENGTH][256];
int qsize;
public:
QUERYMANAGER();
QUERYMANAGER(char *indexfname, char *btfname, char *urltablefname, char *snippetfname, char *snippetbtfname);
~QUERYMANAGER();
这是 .cpp 文件:
#include "querymanagernew.h"
#include "snippet.h"
using namespace std;
QUERYMANAGER::QUERYMANAGER(char *indexfname, char *btfname, char *urltablefname, char *snippetfname, char *snippetbtfname){
cache = new INDEXCACHE(indexfname, btfname);
table = new URLTABLE(urltablefname);
snip = new SNIPPET(snippetfname, snippetbtfname);
//this is where the error occurs
qsize = 0;
}
我完全不知道是什么原因造成的 - 有什么想法吗?
谢谢,BSG
I have a class with private member variables declared in a header file. In my constructor, I pass in some filenames and create other objects using those names. This works fine. When I try to add another member variable, however, and initialize it in the constructor, I get an access reading violation. I sent the code to someone else and it works fine on his computer. Any idea what could be wrong?
Here is the offending code:
The .h file:
class QUERYMANAGER {
INDEXCACHE *cache;
URLTABLE *table;
SNIPPET *snip;
int* iquery[MAX_QUERY_LENGTH];
int* metapointers[MAX_QUERY_LENGTH];
int blockpointers[MAX_QUERY_LENGTH];
int docpositions[MAX_QUERY_LENGTH];
int numberdocs[MAX_QUERY_LENGTH];
int frequencies[MAX_QUERY_LENGTH];
int docarrays[MAX_QUERY_LENGTH][256];
int qsize;
public:
QUERYMANAGER();
QUERYMANAGER(char *indexfname, char *btfname, char *urltablefname, char *snippetfname, char *snippetbtfname);
~QUERYMANAGER();
This is the .cpp file:
#include "querymanagernew.h"
#include "snippet.h"
using namespace std;
QUERYMANAGER::QUERYMANAGER(char *indexfname, char *btfname, char *urltablefname, char *snippetfname, char *snippetbtfname){
cache = new INDEXCACHE(indexfname, btfname);
table = new URLTABLE(urltablefname);
snip = new SNIPPET(snippetfname, snippetbtfname);
//this is where the error occurs
qsize = 0;
}
I am totally at a loss as to what is causing this - any ideas?
Thanks, bsg
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
建议,分解数组:
看起来你应该有另一个结构:
QueryManager 现在看起来像:
这可能有助于更好地封装主题。
Suggestion, factor out the arrays:
Looks like you should have another structure:
And the QueryManager now looks like:
This may help encapsulate themes a little better.
您的依赖项可能不正确,并且没有重建必要的文件。尝试“干净”重建。
作为样式注释,请使用初始值设定项列表。
并且您可能不需要创建这些项目指针:
Your dependencies are probably not right, and the necessary files aren't getting rebuilt. Try a "clean" rebuild.
As a note to style, use initializer lists.
and you may not need to make those items pointers:
你建得干净吗?由于访问最后一个成员变量会崩溃,但分配给较早的成员变量却可以正常工作,要么您在使用实例时没有正确构造/分配实例,要么您的对象文件引用了旧版本的标头,而该标头没有'对象中还没有
qsize
,因此没有分配足够的空间。或者类似的东西。Have you built clean? Since accessing the last member variable blows up, but assigning to earlier ones works OK, either you're not constructing/allocating the instance right when you do use it, or you have object files that refer to older versions of the header that didn't have
qsize
in the object yet, and thus aren't allocating enough space. Or something along those lines.正如预期的那样,这在我的机器上运行得很好:
所以错误一定是在您没有显示的代码中。
顺便说一句,除了宏之外,所有大写名称都是 boo。它们使您的代码更难阅读,并使每个习惯更常见编码风格的人感到困惑。
As expected, this runs just fine on my machine:
So the error must be in the code you're not showing.
BTW, all upper-case names are boo except for macros. They're making your code harder to read and confuse everyone used to a more common coding style.