指针问题 [英] Pointer Problem

查看:71
本文介绍了指针问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我遇到了一个问题,有一个稍微复杂的部分我正在写的

计划。

我有一个class(让我们称之为ClassA)在其公共区域中有一个

变量(恰好是另一个类,ClassB),如下所示:


class ClassA

{

public:

// ...

ClassB myVar;

// ...

}


我在另一个类(ClassC)中有一个函数(doFunction),它需要

a指向ClassB的指针作为参数。现在,以下编译和工作:


ClassA instance1;


int main()

{

// ...

ClassC instance2;

instance2.doFunction(& instance1.myVar);

/ / ...

}


但是,以下内容会编译,但会在运行时导致访问冲突:


int main()

{

ClassA * ptrInstance1;

ptrInstance1 = new ClassA;

ClassC instance2;

instance2.doFunction(& ptrInstance1-> myVar);

}


这里我的代码有问题吗? doFuntion中的问题是什么?

任何帮助将不胜感激。对不起,可怕的

函数/类名。使用VC ++


谢谢

康奈尔

解决方案

Connell Gauld写道:< blockquote class =post_quotes>我遇到了一个问题,我正在编写一个稍微复杂的部分。
我有一个课程(让我们称之为ClassA)它的公共区域,一个
变量(恰好是另一个类,ClassB)是这样的:

类ClassA
{
公众:
// ......
ClassB myVar;
// ...


全部公开?任何建设者?有参考成员吗?我们可能会看到一个POD,然后我们可能不会。没有足够的信息

肯定知道。

}

我在另一个类(ClassC)中有一个函数(doFunction),它接受
指向ClassB的指针作为参数。现在,以下编译和
工作:

ClassA instance1;

int main()
{
// ...
ClassC instance2;
instance2.doFunction(& instance1.myVar);
// ...


然而,以下内容编译但是在运行时导致访问冲突:

int main()
{A A> pAInstance1;
ptrInstance1 =新的ClassA;
ClassC instance2;
instance2.doFunction(& ptrInstance1-> myVar);
}

我的代码在这里有问题吗? doFuntion中的问题是什么?


我在这里看到的唯一可能有所不同的是,

全局对象''instance1''在初始化之前用0填充

什么的,但当你说


新的ClassA;


对象_may_未被初始化,那个意味着''myVar''的价值是新的给你的记忆。尝试

将其替换为


新的ClassA();


并查看是否存在差异。否则,请确保通过向ClassA添加默认构造函数来初始化

所有成员。

任何帮助将不胜感激。对不起,可怕的
函数/类名。使用VC ++




VC ++与否应该不重要。但是,如果您发现您的代码

如果由另一个编译器编译就可以正常工作,并且如果VC ++编译后不会编译,那么您应该在microsoft中询问。 public.vc.language for

援助,也许这是一个错误。但如果没有,那么你正在使用的编译器没有什么区别。


V


2004年7月22日星期四22:42:02 +0100,Connell Gauld

< co ***** @ freebreakfast.co.uk>写道:


我遇到了一个问题,有一个稍微复杂的部分我正在写的
程序。
我上课了(让我们称之为ClassA)在其公共区域中有一个
变量(恰好是另一个类,ClassB),如下所示:

class ClassA
{
公开:
// ...
ClassB myVar;
// ...

我有一个功能( doFunction)在另一个类(ClassC)中,它将指向ClassB的指针作为参数。现在,以下编译和
工作:

ClassA instance1;

int main()
{
// ...


我猜你错过了


ClassA instance1;


这里。

ClassC instance2;
instance2.doFunction(& instance1.myVar);
// ...
}

以下,但是,编译但在运行时会导致访问冲突:

int main()
{A> ClassA * ptrInstance1;
ptrInstance1 =新的ClassA;
ClassC instance2;
instance2.doFunction(& ptrInstance1-> myVar);
}

这里的代码有问题吗?




doFuntion中存在问题吗?


也许

任何帮助都将不胜感激。




这种奇怪的错误,那些轻微的,看似不重要的变化使得代码工作和不工作之间的差异是一个肯定的标志

你的代码被窃听。不幸的是,它告诉你很少关于错误的地方。您发布的代码没有任何问题。


VC ++有一个出色的调试器。我能提出的最好的建议是,你可以学习如何使用它。或者减少你的程序大小,直到

小到足以在这里发布然后发布整个程序。


john


John Harrison写道:

2004年7月22日星期四22:42:02 +0100,Connell Gauld
< co **** *@freebreakfast.co.uk>写道:


我遇到了一个问题,有一个稍微复杂的部分我正在写的
程序。
我上课了(让我们称之为ClassA)在其公共区域中有一个变量(恰好是另一个类,ClassB),如下所示:

classA Class
{
公开:
// ...
ClassB myVar;
// ...

我有一个功能(另一个类(ClassC)中的doFunction,它将指向ClassB的指针作为参数。现在,以下
编译和工作:

ClassA instance1;

int main()
{
// ...

我猜你错过了

ClassA instance1;




我在猜你在全球范围内错过了几行。


它确实有很大的不同。请阅读我对OP的回复。

此处。

ClassC instance2;
instance2.doFunction(& instance1.myVar);
//
}
[...]




V


Hi,
I have come across a problem with a slightly complicated section to a
program I am writing.
I have a class (let''s call it ClassA) which has, in its public area, a
variable (which happens to be another class, ClassB) like this:

class ClassA
{
public:
// ...
ClassB myVar;
// ...
}

I have a function (doFunction) in yet another class (ClassC) which takes
a pointer to a ClassB as a parameter. Now, the following compiles and works:

ClassA instance1;

int main()
{
//...
ClassC instance2;
instance2.doFunction(&instance1.myVar);
//...
}

The following, however, compiles but causes an Access Violation when run:

int main()
{
ClassA *ptrInstance1;
ptrInstance1=new ClassA;
ClassC instance2;
instance2.doFunction(&ptrInstance1->myVar);
}

Is there something wrong with my code here? Is the problem in doFuntion?
Any help would be greatly appreciated. Sorry for the horrible
function/class names. Using VC++

Thanks
Connell

解决方案

Connell Gauld wrote:

I have come across a problem with a slightly complicated section to a
program I am writing.
I have a class (let''s call it ClassA) which has, in its public area, a
variable (which happens to be another class, ClassB) like this:

class ClassA
{
public:
// ...
ClassB myVar;
// ...
All public? Any constructors? Any reference members? We may be
looking at a POD, then again we may not. Not enough information to
know for sure.
}

I have a function (doFunction) in yet another class (ClassC) which takes
a pointer to a ClassB as a parameter. Now, the following compiles and
works:

ClassA instance1;

int main()
{
//...
ClassC instance2;
instance2.doFunction(&instance1.myVar);
//...
}

The following, however, compiles but causes an Access Violation when run:

int main()
{
ClassA *ptrInstance1;
ptrInstance1=new ClassA;
ClassC instance2;
instance2.doFunction(&ptrInstance1->myVar);
}

Is there something wrong with my code here? Is the problem in doFuntion?
The only thing that I can see here that may make a difference is that
the global object ''instance1'' is filled with 0s before initialising
to whatever, but when you say

new ClassA;

the object _may_ be left uninitialised, and that means that the value
of ''myVar'' is whatever came with the memory ''new'' is giving you. Try
replacing it with

new ClassA();

and see if there is a difference. Otherwise, make sure you initialise
all members to something by adding a default constructor to ClassA.
Any help would be greatly appreciated. Sorry for the horrible
function/class names. Using VC++



VC++ or not shouldn''t matter. If you, however, find that your code
works fine if compiled by another compiler, and doesn''t if compiled
by VC++, then you should ask in microsoft.public.vc.language for
assistance, perhaps it''s a bug. But if not, then there is no
difference what compiler you''re using.

V


On Thu, 22 Jul 2004 22:42:02 +0100, Connell Gauld
<co*****@freebreakfast.co.uk> wrote:

Hi,
I have come across a problem with a slightly complicated section to a
program I am writing.
I have a class (let''s call it ClassA) which has, in its public area, a
variable (which happens to be another class, ClassB) like this:

class ClassA
{
public:
// ...
ClassB myVar;
// ...
}

I have a function (doFunction) in yet another class (ClassC) which takes
a pointer to a ClassB as a parameter. Now, the following compiles and
works:

ClassA instance1;

int main()
{
//...
I''m guesing that you missed out

ClassA instance1;

here.
ClassC instance2;
instance2.doFunction(&instance1.myVar);
//...
}

The following, however, compiles but causes an Access Violation when run:

int main()
{
ClassA *ptrInstance1;
ptrInstance1=new ClassA;
ClassC instance2;
instance2.doFunction(&ptrInstance1->myVar);
}

Is there something wrong with my code here?
No
Is the problem in doFuntion?
Maybe
Any help would be greatly appreciated.



This sort of wierd bug, where slight, seemingly unimportant, changes make
the difference between you code working and not working is a sure sign
that your code is bugged. Unfortunately it tells you very little about
where the bug is. There is nothing wrong with the code you''ve posted.

VC++ has an excellent debugger. The best suggestion I can make is that you
learn how to use it. Alternatively reduce your program in size until it is
small enough to post here and then post the entire program.

john


John Harrison wrote:

On Thu, 22 Jul 2004 22:42:02 +0100, Connell Gauld
<co*****@freebreakfast.co.uk> wrote:

Hi,
I have come across a problem with a slightly complicated section to a
program I am writing.
I have a class (let''s call it ClassA) which has, in its public area,
a variable (which happens to be another class, ClassB) like this:

class ClassA
{
public:
// ...
ClassB myVar;
// ...
}

I have a function (doFunction) in yet another class (ClassC) which
takes a pointer to a ClassB as a parameter. Now, the following
compiles and works:

ClassA instance1;

int main()
{
//...

I''m guesing that you missed out

ClassA instance1;



I''m guessing that you missed it a few lines up, in the global scope.

And it does make a lot of difference. Read my reply to the OP.

here.

ClassC instance2;
instance2.doFunction(&instance1.myVar);
//...
}
[...]



V


这篇关于指针问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆