在c++中调用头文件

Calling Header file in C++

本文关键字:文件 调用 c++      更新时间:2023-10-16

我在使用我创建的头文件时遇到问题。不在主方法中创建invoice中定义的类中的对象。

下面是我创建的头文件的内容:

invoice.h

#pragma once
#include <string>
#include<iostream>
#include"stdafx.h";
using namespace std;
class Invoice
{
private:
    string partno, description;
    int quantity, price;
public:
    Invoice(string partno, string description; int quantity, price);
    //void partno(string);
    //void description(string);
    //void quantity(int);
    //void price(int);
    int getinvoiceamount();
    string getPartno() {
        return partno;
    }
    string getDescription(){
        return description;
    }
    int getQuantity(){
    return quantity;
}
    int getPrice(){
        return price;
    }
    void setPartno(int partno)
    {
        Invoice::partno=partno
    }
    void setDescription(int discripyion)
    {
    Invoice::description=description    
    }
    void setQuantity(int quantity);
    {
    Invoice::quantity=quantity
    }
    void setPrice(int price);
    {
    Invoice::price=price
    }
    ~Invoice();
};

下面是我使用头文件的文件内容:

invoice.cpp

#include "stdafx.h"
#include "invoice.h"
#include "stdafx.h"
#include "Account.h"
#include<iostream>
#include<conio.h>
#include <string>
using namespace std;

int invoice::getinvoiceamount()
{
    return (getquantity*getprice)
    if (quantity != 0)
        quantity = 0;
    if (price != 0)
        price = 0;
}

hardware::~hardware()
{
}

让我知道我哪里做错了!

在纠正了大多数明显的错误之后,实现了构造函数,添加了一些注释来澄清成员和函数,结果是:

header:

pragma once
#include <string>
#include<iostream>
#include"stdafx.h";
using namespace std;
class Invoice{
public:
    // constructor implementation: data member initialization added
    Invoice(string part, string descr; int quant, pric)
    : partno(part), description(descr), quantity(quant), price(pric) { }
    // use default destructor 
    // ~Invoice();
    //void partno(string);
    //void description(string);
    //void quantity(int);
    //void price(int);
    // non-modifying member functions: getters
    int getInvoiceAmount();
    string getPartno(){ return partno; }
    string getDescription(){ return description; }
    int getQuantity(){ return quantity; }
    int getPrice(){ return price; }
    // modifying member functions: setters
    void setPartno(int part){ partno=part;}
    void setDescription(int discr){ description=descr; }
    void setQuantity(int quant);{ quantity=quant; }
    void setPrice(int pri){ price=pri; }
private:
    string partno;
    string description;
    int quantity;
    int price;
}; 

cpp

#include "stdafx.h"
#include "invoice.h"
#include "stdafx.h"
#include "Account.h"
#include<iostream>
#include<conio.h>
#include <string>
using namespace std;
// be consistent with the names of variables/ functions
int Invoice::getInvoiceAmount(){
    // check either if some of the variables in question have values that doesn't make sense
    // if (quantity == 0) error("No quantity!");
    // if (price == 0) error("No price!");
    // or the opposite
    if(quantity != 0 && price != 0) return (getquantity * getprice);   
    else error("Quantity or price == 0");   
}
// what is this? Add comments to clarify the purpose of your functions/ variables
// hardware::~hardware()
// {
// }