在c++中调用头文件
Calling Header file in C++
我在使用我创建的头文件时遇到问题。不在主方法中创建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()
// {
// }
相关文章:
- 编译多个C++文件.调用二进制文件以运行代码
- 无法从 SO 文件调用 SO 文件的函数 - C++生成文件
- 如何从另一个文件调用类构造函数?
- 调用函数一次用于动态链接库,一次从可执行文件调用函数
- 当我从头文件和实现文件调用我的函数到我的主文件时,我没有得到任何输出
- 从 Android 中的 C 文件调用C++函数
- 从外部文件C++调用函数
- 如何从多个 cpp 文件调用函数
- 从用户定义的头文件调用函数时出现未定义的引用错误,其实现位于.cpp文件中
- cmake:生成批处理文件调用cl.exe
- 在文件中生成随机数,然后从文件调用它们,然后将它们打印出来
- main.cpp从 .h 文件调用公共虚拟方法
- 如何从 python (py) 文件调用 C++ 函数
- 如何从 cpp 文件调用 setRequestOrientation()
- 调试Visual Studio中可执行文件调用的C 文件
- 可以使用不同的头文件调用相同的函数吗?
- 从 Android 中的原生 c++ 文件调用本机 opencv
- 使用对象文件调用Winapi功能
- 从头文件调用字符串getter函数
- 方法从头文件调用