I am trying an exercise in a book and can't find my problem.
I want an invoice class with a seperate implementation file and a file to use it. Below are the three files.

// invoice class
//Invoice.h

#include <string>

using namespace std;

class Invoice
{
public:
	Invoice(string,string, int, int);
	void setInvoicePartNumber(string);
	string getInvoicePartNumber();
	void setInvoicePartDescription(string);
	string getInvoicePartDescription();
	void setInvoiceQuantity(int);
	int getInvoiceQuantity();
	void setInvoicePrice(int);
	int getInvoicePrice();
	int getInvoiceAmount();

private:
	string invoicePartNumber;
	string invoicePartDescription;
	int invoiceQuantity;
	int invoicePrice;
	int invoiceAmount;
}


// invoice implementation
// Invoice.cpp

#include <iostream>
#include <string>
using namespace std;

#include "Invoice.h"



Invoice::Invoice(string partNumber, string partDescription, int quantity, int price)
{
	setInvoiceQuantity(quantity);
	setInvoicePartDescription(partDescription);
	setInvoicePartNumber(partNumber);
	setInvoicePrice(price);
}
void Invoice::setInvoicePartNumber(string partNumber)
{
	invoicePartNumber=partNumber;
}
void Invoice::setInvoicePartDescription(string partDescription)
{
	invoicePartDescription=partDescription;
}
void Invoice::setInvoiceQuantity(int quantity)
{
	invoiceQuantity=quantity;
}
void Invoice::setInvoicePrice(int price)
{
	invoicePrice=price;
}

string Invoice::getInvoicePartNumber()
{
	return invoicePartNumber;
}

string Invoice::getInvoicePartDescription()
{
	return invoicePartDescription;
}
int Invoice::getInvoiceQuantity()
{
	return invoiceQuantity;
}
int Invoice::getInvoicePrice()
{
	return invoicePrice;
}
int Invoice::getInvoiceAmount()
{
	invoiceAmount=invoiceQuantity*invoicePrice;
	return invoiceAmount;
}

//using invoice class
//test.cpp

#include <iostream>

#include <string>
#include "Invoice.h"


int main()
{
	Invoice invoice("500i", "screw", 5, 6);

	std::cout<<"Item number " << invoice.getInvoicePartNumber()<< "description: " <<invoice.getInvoicePartDescription()<< " quantity: " <<invoice.getInvoiceQuantity() << "Price: " << invoice.getInvoicePrice() << "for a total of " <<invoice.getInvoiceAmount()<<endl;
	return 0;
}

Any help would be appreciated.

Recommended Answers

All 4 Replies

I would start by breaking up your code block like this, for ease of discussion:

file1.h:

//header contents (class definitions)

file1.cpp:

//implementations

file2.cpp:

//your includes:
#include "file1.h"

int main() {
}

Doing this makes it easier to understand your project's file set up, which may help spot an error easier. You still have plenty of time, edit your original post.

Once you have fixed your OP, post back letting us know you did and let us know more about what your issue is. Specifically, what seems to be giving you trouble. Is it not compiling? Is it not linking? Are you getting odd results when it runs?

Sorry i didn't explain well. I did seperate into three with the comments seperating the files.

It is not compiling.
It does not like the line using namespace std; says missing ; before it.
That is the main problem and i think that might solve the others.

You need a semi-colon after your class definition:

class someClass {
 public:
   /* ... public members ... */
 private:
   /* ... private members ... */
}; //<--- semi-colon required

Without the semi-colon, the compiler will get VERY confused...

That did it. THank you so much.
Leeba

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.