【问题标题】:Multi-File Class Implementation Cpp多文件类实现Cpp
【发布时间】:2018-12-28 06:04:23
【问题描述】:

我正在尝试实现一个多文件类,但我对“如何做”感到非常困惑。

我已经在主文件中声明了这个类并且它可以工作,但现在我正在尝试让它从主文件中分离出来。

我有这些疑问:

1-) 我的类中有一个构造函数,所以当我在 .cpp 文件中声明它时,我将在构造函数中声明一个构造函数????听起来很混乱..

2-) 如果.cpp文件中的函数使用主文件中声明的全局变量/向量,我需要使用Extern声明它吗??

3-) 我需要#include .cpp 上的函数所需的每个库吗??

我无法说出我遇到的错误,因为每次都是不同的错误,有时我会收到错误:

error: 'DataReading' has not been declared|

我会非常感谢任何帮助:)

首先我将展示在 main 上工作的整个类,注意构造函数(我将用“//code code”替换一些代码以缩短它)

class DataReading
{
private:
    string word;
    string line;
    string temp;
public:
    DataReading(string wd) : word(wd)
    {}
    void ReadLineByLine()
    {
        //code code
    }

    void searchword()
    {
        //code code
    }
    int check_data()
    {
        if ((Names.size() != (DadosDeEntrada[7].Valor+1)) ||(WellTypes.size() != (DadosDeEntrada[7].Valor+1)) || (Positions.size() != (DadosDeEntrada[7].Valor+1))|| (WellsRadius.size() != (DadosDeEntrada[7].Valor+1))|| (Skins.size() != (DadosDeEntrada[7].Valor+1))|| (ControlTypes.size() != (DadosDeEntrada[7].Valor+1))|| (Pressures.size() != (DadosDeEntrada[7].Valor+1)))
        {
            //code code
        }
    }

所以我将展示我的尝试,首先是 .h 文件:

#ifndef DATAREADING_H
#define DATAREADING_H


class DataReading
{
    public:
        DataReading(std::string wd);
        void ReadLineByLine();
        void searchword();
        int check_data();

    private:
        std::string word;
        std::string line;
        std::string temp;
};

#endif // DATAREADING_H

现在 .cpp 文件缩短了:

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <iomanip>
#include <Eigen/Dense>
#define _USE_MATH_DEFINES
#include <math.h>
#include <cmath>

using namespace std;
using namespace Eigen;

extern vector <string> Names;                              // Cria vetores necessários
extern vector <string> WellTypes;
extern vector <string> Positions;
extern vector <string> WellsRadius;
extern vector <string> Skins;
extern vector <string> ControlTypes;
extern vector <string> Pressures;
/*extern struct StructReading                                 //Estrutura para ler os dados do Reservatório
{
    string Variavel;
    double Valor;
}
extern vector <StructReading> DadosDeEntrada;*/

DataReading::DataReading(string wd) 
{
    DataReading(string wd) : word(wd)
    {}
}

void DataReading::ReadLineByLine()
    {
        //code code
    }

void DataReading::searchword()
    {
        //code code
    }

int DataReading::check_data()
    {
        if ((Names.size() != (DadosDeEntrada[7].Valor+1)) ||(WellTypes.size() != (DadosDeEntrada[7].Valor+1)) || (Positions.size() != (DadosDeEntrada[7].Valor+1))|| (WellsRadius.size() != (DadosDeEntrada[7].Valor+1))|| (Skins.size() != (DadosDeEntrada[7].Valor+1))|| (ControlTypes.size() != (DadosDeEntrada[7].Valor+1))|| (Pressures.size() != (DadosDeEntrada[7].Valor+1)))
        {
           //code code
        }
    }

PS:我需要使用 .cpp 文件中的结构,但不知道如何导入。

【问题讨论】:

    标签: c++ class header


    【解决方案1】:

    数据读取.h

    #ifndef DATAREADING_H_INCLUDED
    #define DATAREADING_H_INCLUDED
    
    #include <string>
    
    class DataReading
    {
    private:
        std::string word;
        std::string line;
        std::string temp;
    public:
        DataReading(std::string const & wd);
        void ReadLineByLine();
        void searchword();
        int check_data();
    };
    
    #endif /* DATAREADING_H_INCLUDED */
    

    数据读取.cpp

    #include "datareading.h"
    
    DataReading::DataReading(std::string const & wd)
    : word{ wd }
    {}
    
    void DataReading::ReadLineByLine()
    {
        //code code
    }
    
    void DataReading::searchword()
    {
        //code code
    }
    int DataReading::check_data()
    {
        //code code
    }
    

    main.cpp

    #include "datareading.h"
    
    int main()
    {
        DataReading dr{"foo"};
    }
    

    不过,您不应该使用全局变量。

    "PS:我需要使用 .cpp 文件中的结构,但不知道如何导入。" 在自己的头文件中声明该结构,并在您使用该结构的任何地方包含该结构。

    【讨论】:

    • 非常感谢,我不知道该如何表达我的感激之情!我有一些问题,你为什么把“()”改成“{}”我可以这样写吗? DataReading::DataReading(std::string const &amp; wd) : word(wd) {}
    • 用大括号编写初始化是我从 C+11 开始养成的习惯。在网上搜索uniform initialization c++应该会找到很多相关信息。关于全局变量:在您的问题中,您说这些是在“主文件”中声明的,现在您说它们是在main() 中声明的。你不应该使用全局变量。而是使用局部变量并将(const)引用传递给使用它们的函数/类。
    • 抱歉错误,它们不在 Main 中!谢谢你的建议,我会试试的。我注意到您只是在 .cpp 文件中包含了“datareading.h”,但没有必要将其他包含为 和 .h 文件中的 using namespace std; 行代码??
    • &lt;string&gt; 已经包含在 datareading.h 中,而 datareading.cpp 中包含了 &lt;string&gt; 的内容,当预处理器完成时,datareading.cpp 中也存在 datareading.cpp 的内容。您必须包含哪些文件取决于您的 //code code 是什么。你不应该在头文件中使用using namespace
    • 所以我需要在datareading.cpp中使用“包含”和“命名空间”?
    猜你喜欢
    • 2010-12-21
    • 1970-01-01
    • 2011-07-19
    • 1970-01-01
    • 2011-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多