关于C ++包括另一个类 [英] Regarding C++ Include another class

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

问题描述

我有2个档案:

  File1.cpp 
File2.cpp


$ b $ p

File1是我的主类有主方法,File2.cpp有一个类调用ClassTwo和我想创建一个ClassTwo的对象我的File1.cpp



我以这种方式将它们编译在一起

  g ++ -o myfile File1.cpp File2.cpp 

但是当我尝试通过



//创建类两个对象

  ClassTwo ctwo; 

不起作用。



错误是



未在此范围内声明ClassTwo。



这是我的main.cpp

  #include< iostream> 
#include< string>
using namespace std;

int main()
{
//这里编译错误 - undeclare ClassTwo在范围内。
ClassTwo ctwo;

//一些代码
}

.cpp

  #include< iostream> 
#include< string>

using namespace std;

class Classwo
{
private:
string myType;
public:
void setType(string);
string getType();
};


void ClassTwo :: setType(string sType)
{
myType = sType;
}

void ClassTwo :: getType(float fVal)
{
return myType;
}

响应将File2.cpp分割成另一个.h文件,我声明一个类,我如何将它拆分成另一个.h文件,因为我需要维护公共和私有的变量(私人)和函数(公共)和如何获得ClassTwo ctwo到我的File1.cpp在main方法

解决方案


您的代码有什么基本问题? p>

您的代码需要分离到接口(.h)和实现(.cpp)。

编译器需要查看类型的组成当你写如下

  ClassTwo obj; 

这是因为编译器需要为类型 ClassTwo的对象预留足够的内存这样做需要看到 ClassTwo 的定义。在C ++中最常用的方法是将代码拆分为头文件和源文件。

类定义放在头文件中,而类的实现则放在源文件中。这样,可以轻松地将头文件包含到其他源文件中,这些源文件需要查看类的定义,谁是他们创建的对象。

为什么我不能简单地将所有的代码放在cpp文件中并将它们包含在其他文件中?




您不能简单地将所有代码放在源文件中,然后将该源文件包含在其他文件中。++标准命令,您可以声明一个实体您可以只定义一次( 一种定义规则(ODR) )。包含源文件会违反ODR,因为实体的副本在每个


如何解决此特定问题


您的代码应按如下组织:



// File1.h

 定义ClassOne 



// File2.h

  #include< iostream> 
#include< string>


类ClassTwo
{
private:
string myType;
public:
void setType(string);
std :: string getType();
};

// File1.cpp

  #includeFile1.h

执行ClassOne

// File2.cpp

  #include File2.h

void ClassTwo :: setType(std :: string sType)
{
myType = sType;
}

void ClassTwo :: getType(float fVal)
{
return myType;
}

// main.cpp

  #include< iostream> 
#include< string>
#includefile1.h
#includefile2.h
using namespace std;

int main()
{

ClassOne cone;
ClassTwo ctwo;

//一些代码
}




有没有其他方法,而不是包含头文件?


如果您的代码只需要创建指针而不是实际对象,您可以使用 转发声明 ,但请注意,使用转发声明添加 此类型可以如何使用的一些限制 ,因为编译器将此类型视为不完整类型


I have 2 files:

File1.cpp
File2.cpp

File1 is my main class which has the main method, File2.cpp has a class call ClassTwo and I want to create an object of ClassTwo in my File1.cpp

I compile them together by this way

g++ -o myfile File1.cpp File2.cpp

But when I try to create by

//create class two object

ClassTwo ctwo;

It doesn't work.

Error was

ClassTwo was not declared in this scope.

This is my main.cpp

#include <iostream>
#include <string>
using namespace std;

int main()
{
//here compile error - undeclare ClassTwo in scope.
ClassTwo ctwo;

//some codes
}

Here is my File2.cpp

#include <iostream>
#include <string>

using namespace std;

class ClassTwo
{
private:
string myType;
public:
void setType(string);
string getType();
};


void ClassTwo::setType(string sType)
{
myType = sType;
}

void ClassTwo::getType(float fVal)
{
return myType;
}

Got respond of splitting my File2.cpp into another .h file but if i am declaring a class, how do i split it into another .h file as i need to maintain the public and private of the variable(private) and functions(public) and how do i get ClassTwo ctwo to my File1.cpp at main method

解决方案

What is the basic problem in your code?

Your code needs to be separated out in to interfaces(.h) and Implementations(.cpp).
The compiler needs to see the composition of a type when you write something like

ClassTwo obj;

This is because the compiler needs to reserve enough memory for object of type ClassTwo to do so it needs to see the definition of ClassTwo. The most common way to do this in C++ is to split your code in to header files and source files.
The class definitions go in the header file while the implementation of the class goes in to source files. This way one can easily include header files in to other source files which need to see the definition of class who's object they create.

Why can't I simply put all code in cpp files and include them in other files?

You cannot simple put all the code in source file and then include that source file in other files.C++ standard mandates that you can declare a entity as many times as you need but you can define it only once(One Definition Rule(ODR)). Including the source file would violate the ODR because a copy of the entity is created in every translation unit where the file is included.

How to solve this particular problem?

Your code should be organized as follows:

//File1.h

Define ClassOne 

//File2.h

#include <iostream>
#include <string>


class ClassTwo
{
private:
   string myType;
public:
   void setType(string);
   std::string getType();
}; 

//File1.cpp

#include"File1.h"

Implementation of ClassOne 

//File2.cpp

#include"File2.h"

void ClassTwo::setType(std::string sType)
{
    myType = sType;
}

void ClassTwo::getType(float fVal)
{
    return myType;
} 

//main.cpp

#include <iostream>
#include <string>
#include "file1.h"
#include "file2.h"
using namespace std;

int main()
{

    ClassOne cone;
    ClassTwo ctwo;

    //some codes
}

Is there any alternative means rather than including header files?

If your code only needs to create pointers and not actual objects you might as well use Forward Declarations but note that using forward declarations adds some restrictions on how that type can be used because compiler sees that type as an Incomplete type.

这篇关于关于C ++包括另一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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