C++,使用 .h 和 .cpp 文件的类问题,函数'getEstimate'将无法运行

C++, Class issues using .h and .cpp files, function 'getEstimate' will not run

本文关键字:getEstimate 运行 函数 问题 使用 cpp 文件 C++      更新时间:2023-10-16

出于某种原因,即使我认为该类是为使用头文件而创建的,也不会转移任何东西。我正在尝试将一些数据发送到一个类中以进行一些简单的数学运算,然后将结果发回。但是,我已经为您提供了代码背后的基础,我只需要在文件之间传输数据即可工作。

这是代码:

-----------------------------------------主要.cpp----------

#include <iostream>
#include <string>
#include "Estimate.h" //incloude class file
using namespace std;
int main() {//open main
    int labour = 3, travel = 4, copper = 2, plastic = 1, chrome = 2;//initialise integers
    //constructer for class Job - passing through all the variables above.
    Estimate Job(labour, travel, copper, plastic, chrome);
    //CALL FUNCTION to print invoice
    Job.getEstimate();
    system("pause");
    return 0;
}//close main

--------------------------------------------估计.h------------

class Estimate          {//open classs
public:
    int labour, travel, copper, plastic, chrome, subTotal;
    float total, vat;
    Estimate(int, int, int, int, int);
    ~Estimate();
    void getEstimate()  {}
};//close estimate

--------------------------估计.cpp-------------------------------

#include <iostream>
#include <string>
#include "Estimate.h"
int labour, travel, copper, plastic, chrome, subTotal;
float total, vat;
//constructor and destructor
Estimate::Estimate(int labour,int travel, int copper, int plastic, int chrome)  {
    this -> labour  = labour;
    this -> travel  = travel;
    this -> copper  = copper;
    this -> plastic = plastic;
    this -> chrome  = chrome;
}//constructor
Estimate::~Estimate()   {}//destructor
void getEstimate()  {
    std::cout << "################### Estimate ###################";
    system("pause");
}//function

您需要从编译的代码开始。 正如马尔钦所提到的,你需要做#include <iostream>#include <string>

现在,在标头中,您将Estimate::getEstimate定义为空函数{}。在您的实现中,您定义了一个作用域函数getEstimate

将标题中的行更改为:

void getEstimate();

以及您的实现中的行:

void Estimate::getEstimate(){