在另一个类/包含文件中使用类作为变量类型。 [英] Use a class as a variable type in another class / include file.

查看:72
本文介绍了在另一个类/包含文件中使用类作为变量类型。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我对c ++语言很陌生,我仍在努力学习它。我最近发现如何使用包含文件允许我将
拆分为较小的段,而不是在一个大文件中使用类

定义等(yay,major发现......)。


我的问题是:


当我在一个包含文件中定义一个类,然后尝试实例化

在另一个中,我得到编译时错误,说类型无效。

如果我移动实例化的类的类定义

其他类进入主.cpp文件我没有错误。


如何解决这个问题?我想让每个班级单独包含

包含文件,但仍然可以在每个$ / b $ b其他内部实例化它们。


我在Windows XP上使用Microsoft Visual C ++ 6.0。


-Toke

Hello. I am quite new to the c++ language, and am still trying to
learn it. I recently discovered how using include files would allow me
to split up my code into smaller segments, instead of having class
definitions etc. in one big file (yay, major discovery...).

My problem is this:

When I define a class in one include file, and then try to instantiate
it in another, I get compile-time errors saying the type is invalid.
If i move the class definition of the class which instantiates the
other class into the main .cpp-file i get no errors.

How to solve this problem? I would like to have each class in seperate
include files, but still be able to instantiate them inside each
other.

I am using Microsoft Visual C++ 6.0 on Windows XP.

-Toke

推荐答案

" Toke ħiland-J rgensen"?; <至** @ toke.dk>写了...
"Toke H?iland-J?rgensen" <to**@toke.dk> wrote...
你好。我对c ++语言很陌生,我仍在努力学习它。我最近发现使用包含文件可以让我将代码拆分成更小的段,而不是在一个大文件中使用类
定义等(耶,重大发现......)。 />
我的问题是:

当我在一个包含文件中定义一个类,然后尝试在另一个包含文件中实例化它时,我得到编译时错误说类型无效。
如果我将实例化
其他类的类的类定义移动到主.cpp文件中,我没有错误。


尝试将第一类的标题包含在源代码中

与其他类:


#包括myheader.h

如何解决这个问题?我想让每个班级单独包含文件,但仍然能够在每个其他内部实例化它们。
Hello. I am quite new to the c++ language, and am still trying to
learn it. I recently discovered how using include files would allow me
to split up my code into smaller segments, instead of having class
definitions etc. in one big file (yay, major discovery...).

My problem is this:

When I define a class in one include file, and then try to instantiate
it in another, I get compile-time errors saying the type is invalid.
If i move the class definition of the class which instantiates the
other class into the main .cpp-file i get no errors.
Try including the header with the first class into the source code
with the other class:

#include "myheader.h"

How to solve this problem? I would like to have each class in seperate
include files, but still be able to instantiate them inside each
other.




此外,请参阅前向声明,它可能是相关的,但很难

告诉没有代码。


Victor



Also, see "forward declaration", it might be relevant, but hard to
tell without the code.

Victor


" Toke H?iland-J?rgensen" <至** @ toke.dk>在消息中写道

news:59 ************************** @ posting.google.c om ...
"Toke H?iland-J?rgensen" <to**@toke.dk> wrote in message
news:59**************************@posting.google.c om...
你好。我对c ++语言很陌生,我仍在努力学习它。我最近发现使用包含文件可以让我将代码拆分成更小的段,而不是在一个大文件中使用类
定义等(耶,重大发现......)。 />
我的问题是:

当我在一个包含文件中定义一个类,然后尝试在另一个包含文件中实例化它时,我得到编译时错误说类型无效。
如果我将实例化
其他类的类的类定义移动到主.cpp文件中,我没有错误。

如何解决这个问题问题?我希望每个类都单独包含文件,但仍然能够在每个其他内部实例化它们。

我在Windows XP上使用Microsoft Visual C ++ 6.0。

-Toke
Hello. I am quite new to the c++ language, and am still trying to
learn it. I recently discovered how using include files would allow me
to split up my code into smaller segments, instead of having class
definitions etc. in one big file (yay, major discovery...).

My problem is this:

When I define a class in one include file, and then try to instantiate
it in another, I get compile-time errors saying the type is invalid.
If i move the class definition of the class which instantiates the
other class into the main .cpp-file i get no errors.

How to solve this problem? I would like to have each class in seperate
include files, but still be able to instantiate them inside each
other.

I am using Microsoft Visual C++ 6.0 on Windows XP.

-Toke




我会建议这样的事情:


// main.cpp

#include" myclass.h"

int main(){//在这里使用myclass}

//文件结尾


在一个文件中,

// myclass.h

#if!定义MYCLASS_H

#define MYCLASS_H

类CoolClass

{

public:

CoolClass();

void amethod();

//其他东西

};

#endif //文件结尾


最后


// myclass.cpp

#include" myclass.h"

CoolClass :: CoolClass()

{

//在此定义构造函数

}

void CoolClass :: amethod()

{

/ /在这里定义一个方法

}

//文件结束


main.cpp和myclass.cpp将被编译到你的project和myclass.h

放入你的包含路径。


-

Cy
http://home.rochester.rr.com/cyhome/


" Cy Edmunds" < CE ****** @ spamless.rochester.rr.com>在消息新闻中写道:< 6A ******************* @ twister.nyroc.rr.com>。 ..
"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote in message news:<6A*******************@twister.nyroc.rr.com>. ..

我建议像这样:


< snip>

main.cpp和myclass .cpp将被编译到你的项目中,myclass.h将被放入你的include路径中。


这是我能够自己整理的结构,它就像你建议的那样工作正常。我的问题是在

中使用myclass另一个包含文件,例如myotherclass.cpp


构建在你的结构上:// main.cpp
#include" myclass.h"
int main(){// make use myclass这里}
//文件结尾

在一个文件中,
// myclass.h
#if!定义MYCLASS_H #define MYCLASS_H
类CoolClass
{
公开:
CoolClass();
void amethod();
//其他东西
}#
#endif //文件结尾

最后

// myclass.cpp
#include" myclass.h"
CoolClass :: CoolClass()
{
//在此定义构造函数
}
void CoolClass :: amethod()
{
// define amethod
}
//文件结尾

I would suggest something like this:
<snip>
main.cpp and myclass.cpp would be compiled into your project and myclass.h
put into your include path.
This is the structure I have been able to put together myself, and it
works fine, just like you suggested. My problem is using myclass in
another include file, e.g. myotherclass.cpp

building on your structure: // main.cpp
#include "myclass.h"
int main() {// make use of myclass here}
// end of file

in one file,

// myclass.h
#if !defined MYCLASS_H
#define MYCLASS_H
class CoolClass
{
public:
CoolClass();
void amethod();
// other stuff
};
#endif // end of file

and finally

// myclass.cpp
#include "myclass.h"
CoolClass::CoolClass()
{
// define constructor here
}
void CoolClass::amethod()
{
// define amethod here
}
// end of file




另一个类:


// myotherclass .h

#if!定义MYOTHERCLASS_H

#define MYOTHERCLASS_H

class QuiteCoolClass

{

public:

QuiteCoolClass();

void amethod();

// ot她的东西

};

#endif //文件结束


//mymyclass.cpp


#include" myotherclass.h"

QuiteCoolClass :: QuiteCoolClass()

{

//在此定义构造函数

}

void QuiteCoolClass :: amethod()

{

CoolClass coolClassInstance; //这是错误的地方

coolClassInstance.amethod(); //这不起作用

//在这里定义一个方法

}

//文件结尾


我在尝试执行上述操作时遇到编译时错误,而

在main()中实例化CoolClass没有给我任何问题,

即如果我将代码从myotherclass.h和myotherclass.cpp移到

main.ccp,我没有错误。


我希望这能澄清我的问题。 .. :)


-Toke



another class:

//myotherclass.h
#if !defined MYOTHERCLASS_H
#define MYOTHERCLASS_H
class QuiteCoolClass
{
public:
QuiteCoolClass();
void amethod();
// other stuff
};
#endif // end of file

//myotherclass.cpp

#include "myotherclass.h"
QuiteCoolClass::QuiteCoolClass()
{
// define constructor here
}
void QuiteCoolClass::amethod()
{
CoolClass coolClassInstance; //this is where the error is
coolClassInstance.amethod(); //this doesn''t work either
// define amethod here
}
// end of file

I get compile time errors when trying to do the above, whereas
instantiating CoolClass in main() gives me no problems whatsoever,
i.e. if I move the code from myotherclass.h and myotherclass.cpp into
main.ccp, i get no errors.

I hope this clarifies my problem a little... :)

-Toke


这篇关于在另一个类/包含文件中使用类作为变量类型。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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