上课讨厌 [英] class hate

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

问题描述

好吧......这没有任何意义......


在标题中声明了一个类''diskStorage''... diskStorage.h

定义它的构造函数和方法...在diskStorage.cpp中


在另一个类中包含diskStorage头...(server.h)

然后尝试声明一个''diskStorage'类型的变量'...

像这样(在server.h内)


diskStorage local_disk;


这就是我得到的

"错误:没有匹配函数来调用`diskStorage :: diskStorage()"


现在,当我在第二个类头文件中执行此操作时:

diskStorage * local_disk;


然后在第二个类构造函数中:

local_disk = new diskStorage(cylinder,tracks,track_time);


它编译并表现....除了...

调用local_disk-> get_info()返回它应该只在

''服务器''中的内容当我在

服务器类的一部分中调用local_disk-> get_info()而不在其中时,我会调用
。构造函数....它返回垃圾...


我猜第二个案例中的local_disk超出了范围。

但为什么呢它不像我首先声明一个''diskStorage'类型的变量'




我是否需要在头文件中执行此操作

diskStorage local_disk(); //////这不是一个功能!?并且

不是变量


Dunno ...

有什么想法吗?

--- - server.h -----


#include" diskStorage.h"

班级服务器

{

private:

diskStorage local_disk; //应该服务器不知道是什么

diskStorage是什么?

}

---- diskStorage.h -------

类diskStorage {

受保护:

u_long _cylinder_count;

u_long _sector_count;

u_long _track_time;

unsigned char geometry [12];


public:

//!类构造函数

diskStorage(u_long柱面,u_long扇区,u_long track_time);

~diskStorage();


void get_info( );

};

--- diskStorage.cpp ----

#include" diskStorage.h"


diskStorage :: diskStorage(u_long cylinder,u_long sector,u_long

track_time)

{

_cylinder_count =柱面;

_sector_count = sector;

_track_time = track_time;


cout<<" Cylinders:" ;<< _cylinder_count<< endl;

cout<<" Sectors:" << _sector_count<< endl;

}

--- server.cpp ---


server :: server( u_long柱,u_long sector,u_long track_time)

{

diskStorage local_disk(柱面,扇区,track_time); //这不是

工作

local_disk = new diskStorage(cylinder,sector,track_time); //这是

work


}


我确信我可以在标题中声明任何类型的变量,并且

在其他地方初始化它

就像我可以使用任何数据一样类型...但我似乎错了...

或者只是看了很长时间的代码并且遗漏了一些微不足道的东西。

这些当然只是部分代码,但其余的不相关

和相当混乱。


您可能会问的问题:

问:为什么我是否将类声明和定义分开?

A:''因为我希望能够开展一个足够大的项目(有一天)

有意义并且我正在尝试习惯这个想法。


问:为什么我不寻找例子?

答:我做了,但没有发现任何相似之处。


问:我只是想让别人做我的硬件吗?

答:不,我需要先学习才有人要我这样做(在

工作)。


顺便说一句。 gdb跟踪没有打破我告诉它的地方......所以现在我已经完全丢失了b $ b :-)

解决方案

2006年11月21日17:19:05 -0800 comp.lang.c ++,Amchi

< am ********* @ gmail.comwrote,


> diskStorage local_disk;

这就是我得到的
错误:没有匹配功能调用`diskStorage :: diskStorage()"



diskstorage没有不带参数的构造函数(也称为

''默认''构造函数。)对于一个简单的diskstorage变量如你所写,

你必须提供初始化参数。

diskStorage local_disk(cylinder,tracks,track_time);


>我是否需要在头文件中执行此操作
diskStorage local_disk(); //////这不是一个功能!?并且
不是变量



你是对的,这是一个功能。


班级成员是:


diskStorage local_disk;


它在构造函数初始化列表中初始化为


server :: server(u_long柱面,u_long扇区,u_long track_time)

:local_disk(柱面,扇区,track_time)

{

...


Marshall Cline的C ++ FAQ中包含了这个问题。请参阅主题

" [10.6]我的构造函数是否应使用初始化列表或

" assignment"?"在发布之前查看常见问题总是好的。

您可以在以下网址阅读常见问题解答:
http://www.parashift.com/c++-faq-lite/


< blockquote>谢谢。

通过将构造函数更改为空来定义一个

''mutator''方法来解决它。


我感谢你花时间帮忙。

再次感谢。

David Harmon写道:


2006年11月21日17:19:05 -0800 comp.lang.c ++,Amchi

< am ********* @ gmail.comwrote,


diskStorage local_disk;


这就是我得到的

"错误:没有匹配的呼叫功能到`diskStorage :: diskStorage()"



diskstorage没有不带参数的构造函数(也称为

''默认''构造函数。)对于一个简单的diskstorage变量如你所写,

你必须提供初始化参数。

diskStorage local_disk(cylinder,tracks,track_time);


我是否需要在头文件中执行此操作

diskStorage local_disk(); //////这不是一个功能!?并且

不是变量



你是对的,这是一个功能。


类成员是:


diskStorage local_disk;


它在构造函数初始化列表中初始化为


服务器::服务器(u_long柱面,u_long扇区,u_long track_time)

:local_disk(柱面,扇区,track_time)

{

...


Marshall Cline的C ++常见问题解答中包含了这个问题。请参阅主题

" [10.6]我的构造函数是否应使用初始化列表或

" assignment"?"在发布之前查看常见问题总是好的。

您可以在以下网址阅读常见问题解答:
http://www.parashift.com/c++-faq-lite/


On 22 Nov,04:23,Amchi < amer.ter ... @ gmail.comwrote:


谢谢。

通过将构造函数更改为空来解决此问题并定义了一个

''mutator''方法。



我想指出(如果你还不知道)你可以拥有两个(或更多)构造函数的
一个不带参数的,一个

的。所以你可以保留你的构造函数,然后创建一个新的

创建一个带有一些默认值的diskStorage。


PS。下一次,在你回复的下面写下你的回复。

如果你不是,人们会生气。


-

Erik Wikstr?m


Alright .... this makes no sense ...

Declared a class ''diskStorage'' in a header ...diskStorage.h
Defined it''s contructor and methods ... in diskStorage.cpp

Included diskStorage header in a another class ...(server.h)
and then tried to declare a variable of type ''diskStorage'' ...
like this ( inside server.h )

diskStorage local_disk;

this is what I get
"error: no matching function for call to `diskStorage::diskStorage()"

Now, when I do this in the second class header file:
diskStorage* local_disk;

and then in the second class c o n t r u c t o r:
local_disk= new diskStorage(cylinders,tracks,track_time);

it compiles and behaves....except ...
calling local_disk->get_info() returns what it should o n l y in the
''server'' class contructor.

when I call the local_disk->get_info() in a method that is part of
server class and not in it''s constructor.... it returns garbage ...

I''m guessing the local_disk in the second case went out of scope.
But why does it not like me declaring a variable of type ''diskStorage''
in the first place?

Do I need to do this in the header file
diskStorage local_disk(); ////// would this not be a function !? and
not a variable

Dunno ...
Any ideas ?
---- server.h -----

#include "diskStorage.h"

class server
{
private:
diskStorage local_disk; // should server not know what
diskStorage is ?
}
---- diskStorage.h-------
class diskStorage {
protected:
u_long _cylinder_count;
u_long _sector_count;
u_long _track_time;
unsigned char geometry[12];

public:
//! class constructor
diskStorage(u_long cylinders,u_long sectors,u_long track_time);
~diskStorage();

void get_info();
};
--- diskStorage.cpp ----

#include "diskStorage.h"

diskStorage::diskStorage(u_long cylinders, u_long sectors, u_long
track_time)
{
_cylinder_count = cylinders;
_sector_count = sectors;
_track_time = track_time;

cout<<"Cylinders :"<< _cylinder_count<<endl;
cout<<"Sectors :" << _sector_count <<endl;
}
--- server.cpp---

server::server(u_long cylinders, u_long sectors, u_long track_time)
{
diskStorage local_disk(cylinders,sectors,track_time);//this does not
work
local_disk = new diskStorage(cylinders,sectors,track_time);//this does
work

}

I was sure I could declare a variable of any type in a header and
initialize it in elsewhere
Just like I can with any data type...but I seem to be wrong ...
Or just looked at the code for too long and missing something trivial.
These are of course just parts of the code but the rest is not relevant
and quite a mess.

The questions you may ask:
Q: Why am I separating class declarations and definitions?
A: ''Cause I hope to work on a project big enough (some day) where that
makes sense and am trying to get used to the idea.

Q: Why did I not look for examples ?
A: I did, but found nothing similar.

Q: Am I just trying to have someone else do my HW?
A: No, I need to learn b e f o r e someone asks me to do this (at
work).

btw. gdb tracing did not break where I told it to ... so now I''m
completely lost :-)

解决方案

On 21 Nov 2006 17:19:05 -0800 in comp.lang.c++, "Amchi"
<am*********@gmail.comwrote,

>diskStorage local_disk;

this is what I get
"error: no matching function for call to `diskStorage::diskStorage()"

diskstorage has no constructor that takes no arguments (also known as a
''default'' constructor.) For a simple diskstorage variable as written,
you must supply arguments for initialization.

diskStorage local_disk(cylinders,tracks,track_time);

>Do I need to do this in the header file
diskStorage local_disk(); ////// would this not be a function !? and
not a variable

You''re right, that''s a function.

The class member is:

diskStorage local_disk;

It''s initialized in the constructor initialization list as

server::server(u_long cylinders, u_long sectors, u_long track_time)
: local_disk(cylinders,sectors,track_time)
{
...

This issue is covered in Marshall Cline''s C++ FAQ. See the topic
"[10.6] Should my constructors use "initialization lists" or
"assignment"?" It is always good to check the FAQ before posting.
You can read the FAQ at:
http://www.parashift.com/c++-faq-lite/


Thank you.
Worked around it by changing the constructor to be empty and defined a
''mutator'' method.

I appreciate you taking time to help.
Thanks again.
David Harmon wrote:

On 21 Nov 2006 17:19:05 -0800 in comp.lang.c++, "Amchi"
<am*********@gmail.comwrote,

diskStorage local_disk;

this is what I get
"error: no matching function for call to `diskStorage::diskStorage()"


diskstorage has no constructor that takes no arguments (also known as a
''default'' constructor.) For a simple diskstorage variable as written,
you must supply arguments for initialization.

diskStorage local_disk(cylinders,tracks,track_time);

Do I need to do this in the header file
diskStorage local_disk(); ////// would this not be a function !? and
not a variable


You''re right, that''s a function.

The class member is:

diskStorage local_disk;

It''s initialized in the constructor initialization list as

server::server(u_long cylinders, u_long sectors, u_long track_time)
: local_disk(cylinders,sectors,track_time)
{
...

This issue is covered in Marshall Cline''s C++ FAQ. See the topic
"[10.6] Should my constructors use "initialization lists" or
"assignment"?" It is always good to check the FAQ before posting.
You can read the FAQ at:
http://www.parashift.com/c++-faq-lite/


On 22 Nov, 04:23, "Amchi" <amer.ter...@gmail.comwrote:

Thank you.
Worked around it by changing the constructor to be empty and defined a
''mutator'' method.

I''d like to point out (if you did not already know) that you can have
two (or more) constructors, one that takes no arguments, and one that
does. So you could keep your constructor and just create a new one that
creates a diskStorage with some default values.

PS. Next time, write your reply below that which you are replying to.
People get annoyed if you don''t.

--
Erik Wikstr?m


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

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