用C ++调用头文件

时间:2015-09-09 11:52:17

标签: c++ header-files precompiled-headers

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

这是我的头文件的内容:

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()
{
}

请告诉我我在哪里做错了!

1 个答案:

答案 0 :(得分:1)

在纠正了大多数明显错误后,实现构造函数,添加一些注释以澄清成员和函数会导致:

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()
// {
// }