Another c++ class problem,

This is a discussion on Another c++ class problem, within the C++ Programming forums, part of the General Programming Boards category; Hello, i posted here a few days ago about a class access issue and have since gotten a new hw ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    8

    Another c++ class problem,

    Hello, i posted here a few days ago about a class access issue and have since gotten a new hw to do and did it but i cant get it to run. here is the header and the implementation file so you dont have to ask for it since its hard to explain what my specific problem is.

    here is my header file, Invoice.h
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class Invoice
    {
    	private:
    		string PartNumber;
    		string PartDescription;
    		int Quantity;
    		int PricePerItem;
    
    	public:
    		void setPartNumber(string);
    		string getPartNumber();
    		void setPartDescription(string);
    		string getPartDescription();
    		void setQuantity(int);
    		int getQuantity();
    		void setPricePerItem(int);
    		int getPricePerItem();
    		int getInvoiceAmount();
    		Invoice(string, string, int, int);
    };
    and my Invoice.cpp
    Code:
    #include "stdafx.h"
    #include "Invoice.h"
    #include <string>
    
    using namespace std;
    
    Invoice::Invoice(string num, string description, int quantity, int cost)
    {
    	if(quantity < 0)
    	{
    		cout << "Quantity cannot be negative!" << endl;
    		cout << "Enter new quantity:";
    		cin >> quantity;
    	}
    	if(cost < 0)
    	{
    		cout << "PricePerItem cannot be negative!" << endl;
    		cout << "Enter new price:";
    		cin >> cost;
    	}
    	
    	Invoice::setPartNumber(num);
    	Invoice::setPartDescription(description);
    	Invoice::setQuantity(quantity);
    	Invoice::setPricePerItem(cost);
    }
    
    void Invoice::setPartNumber(string num)
    {
    	Invoice::PartNumber = num;
    }
    
    string Invoice::getPartNumber()
    {
    	return PartNumber;
    }
    
    void Invoice::setPartDescription(string description)
    {
    	PartDescription = description;
    }
    
    string Invoice::getPartDescription()
    {
    	return PartDescription;
    }
    
    void Invoice::setQuantity(int quantity)
    {
    	Quantity = quantity;
    }
    
    int Invoice::getQuantity()
    {
    	return Quantity;
    }
    
    void Invoice::setPricePerItem(int cost)
    {
    	PricePerItem = cost;
    }
    
    int Invoice::getPricePerItem()
    {
    	return PricePerItem;
    }
    
    int getInvoiceAmount()
    {
    	
    	int num = Invoice::getQuantity();
    	int price = Invoice::getPricePerItem();
    	int Invoice_Amount = num * price;
    	return Invoice_Amount; 
    }
    the last part that is bolded is where the compiler keeps telling me the identifier for getQuantity() and getPricePerItem() cannot be found but i have declared them and all that stuff just before it. Can anyone help me out please? i did this exact same thing last time and it worked in a different but similar project and i dont see anything different here except that i am using more than one variables in my constructor than last time...not really sure if thats the problem...feeling kinda lost :/

    thanks in advance.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,739
    You have getInvoiceAmount() listed as a member function in the header file, but in the implementation file, you wrote a global "free" function called getInvoiceAmount().

    Simply use the class name and scope operator to insert the definition of the member function to the class.
    Code:
    int Invoice::getInvoiceAmount()
    {
    	
    	int num = Invoice::getQuantity();
    	int price = Invoice::getPricePerItem();
    	int Invoice_Amount = num * price;
    	return Invoice_Amount; 
    }
    edit - It doesn't make sense in C++ to call a member function of some object in a free function without using the dot operator. So, you get an error about what you're trying to call.
    Last edited by whiteflags; 09-13-2011 at 09:15 PM.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    Thank you so much! I didnt realize it was needed for the member function since it was optional(i think it is rather) on the data member set and get functions.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,785
    The "Invoice::" part is necessary when declaring your function (so that the compiler understands it's part of your class).
    However, when you call member functions inside your class (from the same class, of course), then the "Invoice::" part is unnecessary, since you are in said class. Make sense?

    Eg, the function is in the class Invoice, so to call a member function inside Invoice, we don't need to prefix "Invoice::" since the compiler will automatically look for the function inside the class. If it was a free function, then the compiler wouldn't be able to know, hence you might need it (depends on circumstances).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    hmm but since i am in the class implementation, wouldnt everything in there be in the class hence not needing the class name and the binary scope operator?
    well i mean its in the .h file hence it should automatically identify it?

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,785
    You are not in the "class implementation."
    As far as the compiler is concerned, you are just writing another source file. The fact that you include a header at the top changes nothing since all include does is copy and paste the contents in the source file.

    Now, if you write everything inline, eg:
    Code:
    class foo
    {
        void bar() { /* ... */ }
        // ...
    };
    There would be no need to prefix with "foo::" since you are in the implementation already.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. class problem
    By like_no_other in forum C++ Programming
    Replies: 8
    Last Post: 04-27-2009, 10:48 AM
  2. Problem with class/derived class
    By ammochck21 in forum C++ Programming
    Replies: 2
    Last Post: 04-07-2007, 12:40 AM
  3. Replies: 27
    Last Post: 10-11-2006, 04:27 AM
  4. Another class problem
    By thecneu in forum C++ Programming
    Replies: 6
    Last Post: 05-26-2005, 12:57 PM
  5. class problem
    By M204Programmer in forum C++ Programming
    Replies: 12
    Last Post: 03-20-2003, 09:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21