超基本的C ++问题 [英] Ultra-basic C++ question

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

问题描述

我有一个名为Manager的类,我想在我的驱动程序中实例化

类。它位于名为Manager.cpp的文件中。经理没有构建器设置

up。


我也有一个驱动程序类,我想创建一个名为

的Manager对象经理在我的主要功能驱动程序类。


我用的代码:


经理经理;

但是我得到诸如经理:未声明的标识符之类的错误。和'语法

错误:在'标识符''经理''之前缺少'';''当我尝试编译时。


我做错了什么?

I have a class called Manager, which I want to instantiate in my driver
class. It is in a file called Manager.cpp. Manager has no constructors set
up.

I also have a driver class and I want to create a Manager object called
manager in my main function of the driver class.

I used the code:

Manager manager;
However i get errors such as "Manager: undeclared identifier" and "syntax
error : missing '';'' before indentifier ''manager''" when I try to compile.

What am I doing wrong?

推荐答案

James写道:
我有一个名为Manager的类,我想在我的驱动程序类中实例化。它位于名为Manager.cpp的文件中。管理器没有设置构建器

我还有一个驱动程序类,我想在驱动程序类的主要功能中创建一个名为
manager的Manager对象。 />
我使用了代码:

经理经理;

但是我收到错误,例如经理:未声明的标识符和'语法
错误:缺少'';''在标识符''经理'''之前当我尝试编译时。

我做错了什么?
I have a class called Manager, which I want to instantiate in my driver
class. It is in a file called Manager.cpp. Manager has no constructors set
up.

I also have a driver class and I want to create a Manager object called
manager in my main function of the driver class.

I used the code:

Manager manager;
However i get errors such as "Manager: undeclared identifier" and "syntax
error : missing '';'' before indentifier ''manager''" when I try to compile.

What am I doing wrong?




您需要将类声明放在头文件中(也许,

Manager.hpp)然后将其包含在需要

的其他.cpp或.hpp文件中。例如:


//文件:Manager.hpp

班级经理

{

public :

void DoSomething();

//其他函数和数据成员

};

//文件:Manager.cpp

#include" Manager.hpp"


void Manager :: DoSomething()

{

// ...

}

//文件:Main.cpp

#include" Manager.hpp" ;


int main()

{

经理经理;

manager.DoSomething() ;

// ...

返回0;

}


你也可以定义如果管理器的功能很小,那么它们在标题中的功能。但是,如果Manager.hpp很大,它可能会减慢编译速度

在整个项目中多次被包含在内。


干杯! --M



You need to put the class declaration in a header file (perhaps,
Manager.hpp) and then include it in other .cpp or .hpp files that need
it. For instance:

// File: Manager.hpp
class Manager
{
public:
void DoSomething();
// other functions and data members
};
// File: Manager.cpp
#include "Manager.hpp"

void Manager::DoSomething()
{
// ...
}
// File: Main.cpp
#include "Manager.hpp"

int main()
{
Manager manager;
manager.DoSomething();
// ...
return 0;
}

You could also define the functions of Manager in the header if they''re
small. It might slow down compilation, however, if Manager.hpp is large
gets included many times throughout the project.

Cheers! --M




詹姆斯写道:

James wrote:
我有一个名为Manager的课程,我是想在我的驱动程序中实例化
类。它位于名为Manager.cpp的文件中。管理器没有设置构建器

我还有一个驱动程序类,我想在驱动程序类的主要功能中创建一个名为
manager的Manager对象。 />
我使用了代码:

经理经理;

但是我收到错误,例如经理:未声明的标识符和'语法
错误:缺少'';''在标识符''经理'''之前当我尝试编译时。

我做错了什么?
I have a class called Manager, which I want to instantiate in my driver
class. It is in a file called Manager.cpp. Manager has no constructors set
up.

I also have a driver class and I want to create a Manager object called
manager in my main function of the driver class.

I used the code:

Manager manager;
However i get errors such as "Manager: undeclared identifier" and "syntax
error : missing '';'' before indentifier ''manager''" when I try to compile.

What am I doing wrong?




in driver.h


#include" Manager.h"


或向我们展示您的代码


/ dan



in driver.h

#include "Manager.h"

or show us your code

/dan


James写道:
我有一个名为Manager的类,我想在我的驱动程序类中实例化。它位于名为Manager.cpp的文件中。管理器没有设置构建器

我还有一个驱动程序类,我想在驱动程序类的主要功能中创建一个名为
manager的Manager对象。 />
我使用了代码:

经理经理;

但是我收到错误,例如经理:未声明的标识符和'语法
错误:缺少'';''在标识符''经理'''之前当我尝试编译时。

我做错了什么?
I have a class called Manager, which I want to instantiate in my driver
class. It is in a file called Manager.cpp. Manager has no constructors set
up.

I also have a driver class and I want to create a Manager object called
manager in my main function of the driver class.

I used the code:

Manager manager;
However i get errors such as "Manager: undeclared identifier" and "syntax
error : missing '';'' before indentifier ''manager''" when I try to compile.

What am I doing wrong?




带有驱动程序代码的文件必须以某种方式定义

''经理'课程可用。通常的方法是将类

定义放在头文件中,如此


//这是''Manager.h''文件:

#ifndef MANAGER_H_INCLUDED //这些被称为''双重包含

#define MANAGER_H_INCLUDED //卫兵'',在其他地方读到它们


班级经理{

...

};


#endif

// ''Manager.h''文件结束


然后将*标题包含在''Manager.cpp''和

的文件中驱动程序代码是:


#include" Manager.h"

...


V



The file with the driver code has to somehow have the definition of the
''Manager'' class available to it. The usual approach is to put the class
definition in a header file, like so

// this is ''Manager.h'' file:
#ifndef MANAGER_H_INCLUDED // those are called ''double inclusion
#define MANAGER_H_INCLUDED // guards'', read about them elsewhere

class Manager {
...
};

#endif
// end of ''Manager.h'' file

then include that header in *both* the ''Manager.cpp'' and the file where
the driver code is:

#include "Manager.h"
...

V


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

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