dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: Passing an object through functions

  1. #1
    Join Date
    Sep 2016
    Posts
    16

    Passing an object through functions

    I have a object called Shopper that i want to pass through an other function, but i didn't find how todo it.
    I called the function HardwareStore::inputShopper() to input the name of the new shopper, and I am trying to pass it to an other function from the same cpp file. just to check if it's working.

    The Error messages i get is this one:
    Code:
    HardwareStore.cpp:24:16: error: redefinition of 'newShopper' with a different type : 'HardwareStore' vs 'Shopper::Shopper' HardwareStore(newShopper);
    
    HardwareStore.cpp:23:19: error: previous definition is here 
    Shopper::Shopper newShopper(name,lname);
    
    1error generated.
    MainProgram.cpp
    Code:
    HardwareStore hws(10);
    hws.inputShopper();
    HardwareStore.cpp
    Code:
    HardwareStore::HardwareStore(int numRegisters):numRegisters(numRegisters)
    {
    	for(int i=0; i<numRegisters; i++)
    		registerQueue.push_back(queue<Shopper>());
    		
    		cout<<"This Worked"<<endl;
    }
    void HardwareStore::inputShopper(){
    	string fname, lname;
    	
    	cout<<"First name : ";
    		getline(cin, fname);
    	cout<<"Last Name  : ";
    		getline(cin, lname);
    	Shopper::Shopper newShopper(fname,lname);
    	HardwareStore(newShopper);	
    }
    void HardwareStore::addShopperToLine(const Shopper& shopper){
    	
    		cout<<"if it displays, it means it s Working";
    }
    Shopper.cpp
    Code:
    Shopper::Shopper(const string& firstName, const string& lastName):firstName(firstName),lastName(lastName){
    		ShoppingCart newChoppingList;
    }
    
    string Shopper::getFname(){
    	return firstName;
    }
    string Shopper::getLname(){
    	return lastName;
    }

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,978

    Re: Passing an object through functions

    Are you using namespaces? If not consider
    Code:
    void HardwareStore::inputShopper() {
    	string fname, lname;
    	
    	cout<<"First name : ";
    	getline(cin, fname);
    	cout<<"Last Name  : ";
    	getline(cin, lname);
    
    	Shopper newShopper(fname, lname);
    	AddShopperToLine(newShopper);	
    }
    However, this has a potential problem. addShopperToLine() has the parameter shopper passed by reference. So in the above example, newshopper is passed as the argument, but the variable newshopper doesn't exist when the class function InputShopper() terminates. So addShopperToLine() can't assume that the lifetime of the parameter shopper exceeds the execution of the function. Whether this will be a problem depends, of course, on what usage is made of shopper within the function.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.4.5)

  3. #3
    Join Date
    Sep 2016
    Posts
    16

    Re: Passing an object through functions

    using namesake std; was implemented of course.
    The newShopper will be pushed into a list of shopper in the function addShopperToLine(..) later;
    When i execute the code as it is (After updating the stupid mistake i made on calling that function "AddShopperToLine(newShopper);" by Copying and pasting the code) , I get this undefined symbols.. Displayed
    Code:
    Undefined symbols for architecture x86_64:
      "Shopper::Shopper(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from:
          HardwareStore::inputShopper() in HardwareStore-43ae66.o
    ld: symbol(s) not found for architecture x86_64
    clang: error: linker command failed with exit code 1 (use -v to see invocation)

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,978

    Re: Passing an object through functions

    Can you post your full code please.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.4.5)

  5. #5
    Join Date
    Sep 2016
    Posts
    16

    Re: Passing an object through functions

    Here is the 5 files i am working on :
    HardwareStore.h
    Code:
    #ifndef HARDWARESTORE_H
    #define HARDWARESTORE_H
    
    #include "Shopper.h"
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <list>
    #include <queue>
    
    using namespace std;
    
    
    class HardwareStore{
    
    	public:
    	
    		HardwareStore(int numRegisters);//Creates a HardwareStore with the specified number of 
    									//registers. (i.e. the number of queues).
    		
    		void inputShopper();
    		void addShopperToLine(const Shopper& shopper);// Adds a shopper to the line that currently	
    											// has the least number of people waiting on it.
    		void processShopper(); //Processes the shopper at the front of the longest line.
    		void checkoutAllShoppers(); //Processes all Shoppers still on line for all of the lines.
    		double totalRevenue();//Returns the amount of money the HardwareStore has taken in so far. 
    						//(At the end of the program, it will represent the total revenue of the store.)
    	private:
    		list<queue<Shopper> > registerQueue;
    
    		int numRegisters;
    		
    };
    
    #endif
    HardwareStore.cpp
    Code:
    #include "HardwareStore.h"
    #include "Shopper.h"
    
    HardwareStore::HardwareStore(int numRegisters):numRegisters(numRegisters)
    {
    	for(int i=0; i<numRegisters; i++)
    		registerQueue.push_back(queue<Shopper>());
    		
    	cout<<"+++++ HardwareStore() Working+++++"<<endl;
    }
    void HardwareStore::inputShopper(){
    	string fname, lname;
    	
    	cout<<"First name : ";
    		getline(cin, fname);
    	cout<<"Last Name  : ";
    		getline(cin, lname);
    	Shopper newShopper(fname, lname);
    	addShopperToLine(newShopper);
    	
    
    }
    void HardwareStore::addShopperToLine(const Shopper& shopper){
    	
    		cout<<"+++++addShopperToLine() Working+++++"<<endl;
    	 //registerQueue contains all the clients of that queue.
    		//the program will add a client to the smallest queue (Register that has the least number of client)
    		
    }
    
    void HardwareStore::processShopper(){
    }
    
    void HardwareStore::checkoutAllShoppers(){}
    
    double HardwareStore::totalRevenue(){
    	return 0;
    }
    Shopper.h
    Code:
    #ifndef SHOPPER_H
    #define SHOPPER_H
    
    #include "Item.h"
    #include "ShoppingCart.h"
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <list>
    using namespace std;
    
    class Shopper{
    	public:
    		
    		Shopper(const string& firstName, const string& lastName); //This creates 
    									//a shopper with this name and with an empty ShoppingCart.
    		void addItemToCart(const Item& item, int numItems); //This adds numItems items 
    							//of the same type to the shopping cart. If no second parameter
    							// is specified, only one item should be added.
    														
    		int amountOwed();/*This is the amount of money in cents that Shopper needs to pay the cashier in the
    						hardware store. It is the total cost of the items in the cart plus an additional 
    						8.875% tax. The price should be rounded up to the nearest cent. 
    						(reminder: the ceil function in the <cmath> header file, rounds up a double to 
    						the nearest integer).*/
    		friend ostream& operator<<(ostream& os, const Shopper& shopper); //Prints the representation for a shopper.
    		
    		string getFname()const;
    		string getLname()const;
    
    	private:
    		string firstName;
    		string lastName;
    		ShoppingCart cart;
    		int numItems;
    		
    };
    
    #endif
    Shopper.cpp
    Code:
    #include "Shopper.h"
    #include <list>
    #include <iostream>
    #include <string>
    
    Shopper::Shopper(const string& firstName, const string& lastName):firstName(firstName),lastName(lastName){
    	
    		ShoppingCart newChoppingList;
    }
    
    void Shopper::addItemToCart(const Item& item, int numItems){
    	
    	
    }
    
    string Shopper::getFname()const{
    	return firstName;
    }
    string Shopper::getLname()const{
    	return lastName;
    }
    ostream& operator<<(ostream& os, const Shopper& shopper){
    	return os;
    }
    StoreSimulator.cpp
    Code:
    #include "HardwareStore.h"
    #include "Shopper.h"
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <queue>
    
    using namespace std;
    
    int main(){
    	
    	int option; // store's simulator operation options
    	char ch;
    	int numRegisters = 10;
    	
    	HardwareStore hws(10);
    	
    	do{
    		cout<<"\t\t********** Store Simulator **********"<<endl;
    		cout<<"\t-1- New Operation."<<endl;//New shopper for a new shopping cart
    		cout<<"\t-2- Total revenue."<<endl;//Total revenue for the store
    		cout<<"\t-3- Amount owed by Shopper."<<endl;//Amount of money owed to the stor by client
    		cout<<"\t-4- Quit"<<endl;
    		cout<<"\t\tOption :> ";
    			cin>>option;
    
    		switch (option){
    			case 1:
    				cin.ignore();
    				cout<<"\t\t---- New Operation ----"<<endl;
    					
    					hws.inputShopper();
    				break;
    			case 2:
    				cout<<"\t\t---- Total revenue ----"<<endl;
    				break;
    			case 3: 
    				cout<<"\t\t---- Amount owed by Shopper ----"<<endl;
    				break;
    			case 4: 
    				cout<<"\t\t SEE YOU SOON "<<endl;
    				exit(1);
    				break;
    			default:
    				cout<<"\tSelect a correct option"<<endl;
    				break;
    
    		}
    		cout<<"Would you like to continue? (Y/n) ";
    			cin>>ch;
    			
    			
    	}while(ch=='y' || ch=='Y');
    	return 0;
    }

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,978

    Re: Passing an object through functions

    As you haven't posted all the bits (no item and shopping cart), I can't test as posted. However the following compiles OK with VS2015
    Code:
    #include <list>
    #include <queue>
    #include <string>
    #include <iostream>
    using namespace std;
    
    class Shopper {
    public:
    
    	Shopper(const string& firstName, const string& lastName); //This creates 
    															  //a shopper with this name and with an empty ShoppingCart.
    	//void addItemToCart(const Item& item, int numItems); //This adds numItems items 
    														//of the same type to the shopping cart. If no second parameter
    														// is specified, only one item should be added.
    
    	int amountOwed();/*This is the amount of money in cents that Shopper needs to pay the cashier in the
    					 hardware store. It is the total cost of the items in the cart plus an additional
    					 8.875% tax. The price should be rounded up to the nearest cent.
    					 (reminder: the ceil function in the <cmath> header file, rounds up a double to
    					 the nearest integer).*/
    
    	friend ostream& operator<<(ostream& os, const Shopper& shopper); //Prints the representation for a shopper.
    
    	string getFname()const;
    	string getLname()const;
    
    private:
    	string firstName;
    	string lastName;
    	//ShoppingCart cart;
    	int numItems;
    
    };
    
    class HardwareStore {
    
    public:
    
    	HardwareStore(int numRegisters);//Creates a HardwareStore with the specified number of 
    									//registers. (i.e. the number of queues).
    
    	void inputShopper();
    	void addShopperToLine(const Shopper& shopper);// Adds a shopper to the line that currently	
    												  // has the least number of people waiting on it.
    	void processShopper(); //Processes the shopper at the front of the longest line.
    	void checkoutAllShoppers(); //Processes all Shoppers still on line for all of the lines.
    	double totalRevenue();//Returns the amount of money the HardwareStore has taken in so far. 
    						  //(At the end of the program, it will represent the total revenue of the store.)
    private:
    	list<queue<Shopper> > registerQueue;
    
    	int numRegisters;
    
    };
    
    Shopper::Shopper(const string& firstName, const string& lastName) :firstName(firstName), lastName(lastName) {
    
    	//ShoppingCart newChoppingList;
    }
    
    /*void Shopper::addItemToCart(const Item& item, int numItems) {
    
    
    }*/
    
    string Shopper::getFname()const {
    	return firstName;
    }
    string Shopper::getLname()const {
    	return lastName;
    }
    ostream& operator<<(ostream& os, const Shopper& shopper) {
    	return os;
    }
    
    HardwareStore::HardwareStore(int numRegisters) :numRegisters(numRegisters)
    {
    	for (int i = 0; i<numRegisters; i++)
    		registerQueue.push_back(queue<Shopper>());
    
    	cout << "+++++ HardwareStore() Working+++++" << endl;
    }
    
    void HardwareStore::inputShopper() {
    	string fname, lname;
    
    	cout << "First name : ";
    	getline(cin, fname);
    	cout << "Last Name  : ";
    	getline(cin, lname);
    	Shopper newShopper(fname, lname);
    	addShopperToLine(newShopper);
    }
    
    void HardwareStore::addShopperToLine(const Shopper& shopper) {
    
    	cout << "+++++addShopperToLine() Working+++++" << endl;
    	//registerQueue contains all the clients of that queue.
    	//the program will add a client to the smallest queue (Register that has the least number of client)
    
    }
    
    void HardwareStore::processShopper() {
    }
    
    void HardwareStore::checkoutAllShoppers() {}
    
    double HardwareStore::totalRevenue() {
    	return 0;
    }
    
    int main()
    {
    	HardwareStore hws(10);
    }
    Note that its not a good idea to have using statements in header files. Usually reference to namespaces in header files includes the namespace with using statements being in the .cpp files. eg
    Code:
    ifndef HARDWARESTORE_H
    #define HARDWARESTORE_H
    
    #include "Shopper.h"
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <list>
    #include <queue>
    
    //using namespace std;
    
    
    class HardwareStore{
    
    	public:
    	
    		HardwareStore(int numRegisters);//Creates a HardwareStore with the specified number of 
    									//registers. (i.e. the number of queues).
    		
    		void inputShopper();
    		void addShopperToLine(const Shopper& shopper);// Adds a shopper to the line that currently	
    											// has the least number of people waiting on it.
    		void processShopper(); //Processes the shopper at the front of the longest line.
    		void checkoutAllShoppers(); //Processes all Shoppers still on line for all of the lines.
    		double totalRevenue();//Returns the amount of money the HardwareStore has taken in so far. 
    						//(At the end of the program, it will represent the total revenue of the store.)
    	private:
    		std::list<std::queue<Shopper> > registerQueue;
    
    		int numRegisters;
    		
    };
    
    #endif
    Code:
    #include "HardwareStore.h"
    #include "Shopper.h"
    
    using namespace std;
    
    HardwareStore::HardwareStore(int numRegisters):numRegisters(numRegisters)
    {
    	for(int i=0; i<numRegisters; i++)
    		registerQueue.push_back(queue<Shopper>());
    		
    	cout<<"+++++ HardwareStore() Working+++++"<<endl;
    }
    void HardwareStore::inputShopper(){
    	string fname, lname;
    	
    	cout<<"First name : ";
    		getline(cin, fname);
    	cout<<"Last Name  : ";
    		getline(cin, lname);
    	Shopper newShopper(fname, lname);
    	addShopperToLine(newShopper);
    	
    
    }
    void HardwareStore::addShopperToLine(const Shopper& shopper){
    	
    		cout<<"+++++addShopperToLine() Working+++++"<<endl;
    	 //registerQueue contains all the clients of that queue.
    		//the program will add a client to the smallest queue (Register that has the least number of client)
    		
    }
    
    void HardwareStore::processShopper(){
    }
    
    void HardwareStore::checkoutAllShoppers(){}
    
    double HardwareStore::totalRevenue(){
    	return 0;
    }
    Code:
    #ifndef SHOPPER_H
    #define SHOPPER_H
    
    #include "Item.h"
    #include "ShoppingCart.h"
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <list>
    //using namespace std;
    
    class Shopper{
    	public:
    		
    		Shopper(const std::string& firstName, const std::string& lastName); //This creates 
    									//a shopper with this name and with an empty ShoppingCart.
    		void addItemToCart(const Item& item, int numItems); //This adds numItems items 
    							//of the same type to the shopping cart. If no second parameter
    							// is specified, only one item should be added.
    														
    		int amountOwed();/*This is the amount of money in cents that Shopper needs to pay the cashier in the
    						hardware store. It is the total cost of the items in the cart plus an additional 
    						8.875% tax. The price should be rounded up to the nearest cent. 
    						(reminder: the ceil function in the <cmath> header file, rounds up a double to 
    						the nearest integer).*/
    		friend std::ostream& operator<<(std::ostream& os, const Shopper& shopper); //Prints the representation for a shopper.
    		
    		std::string getFname()const;
    		std::string getLname()const;
    
    	private:
    		std::string firstName;
    		std::string lastName;
    		ShoppingCart cart;
    		int numItems;
    		
    };
    
    #endif
    Code:
    #include "Shopper.h"
    #include <list>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    Shopper::Shopper(const string& firstName, const string& lastName):firstName(firstName),lastName(lastName){
    	
    		ShoppingCart newChoppingList;
    }
    
    void Shopper::addItemToCart(const Item& item, int numItems){
    	
    	
    }
    
    string Shopper::getFname()const{
    	return firstName;
    }
    string Shopper::getLname()const{
    	return lastName;
    }
    ostream& operator<<(ostream& os, const Shopper& shopper){
    	return os;
    }
    and the same for the other .h and .cpp files.
    Last edited by 2kaud; November 11th, 2016 at 05:08 PM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.4.5)

  7. #7
    Join Date
    Sep 2016
    Posts
    16

    Re: Passing an object through functions

    it does compile. all the .cpp files compile. but every time i launch it, it fails
    I always get this message

    Undefined symbols for architecture x86_64:
    "Shopper::Shopper(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from:
    HardwareStore::inputShopper() in HardwareStore-dc2fac.o
    ld: symbol(s) not found for architecture x86_64
    clang: error: linker command failed with exit code 1 (use -v to see invocation)

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,978

    Re: Passing an object through functions

    The line it is complaining about is
    Code:
    Shopper newShopper(fname, lname);
    the linker doesn't seem to be able to find the code for this constructor of Shopper. Where is
    Code:
    Shopper::Shopper(const string& firstName, const string& lastName)
    {....}
    if you post all the code I'll have a look at it. Attach it as a zip file.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.4.5)

  9. #9
    Join Date
    Sep 2016
    Posts
    16

    Re: Passing an object through functions

    That is the entire code i wrote.

  10. #10
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,978

    Re: Passing an object through functions

    Code:
    #include "Item.h"
    #include "ShoppingCart.h"
    As I said in post #6, these haven't been posted - and no corresponding .cpp either. That's why I did the code in post#6 and asked for all of the code again in post #8. Have you tried compiling the code in post #6?

    I suspect the problem is how the project for clang has been set up - but I don't use clang, only VS2015 so can't help with using clang.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.4.5)

  11. #11
    Join Date
    Sep 2016
    Posts
    16

    Re: Passing an object through functions

    Oh, sorry, yes. I didn't connect them to the rest of the programs
    Item.h
    Code:
    #ifndef ITEM_H
    #define ITEM_H
    
    #include <iostream>
    #include <fstream>
    #include <list>
    
    using namespace std;
    
    class Item{
    	public:
    		Item(const string& name, const double price); //A constructor that takes in the name and price as input.
    		Item(); //An empty constructor. (This is so the vector gets initialized properly)
    		
    		string getName()const;  //This returns the name of the item
    		double getPrice()const; //This returns the price of the item.
    		void updatingLists(); //Get the items and prices from the files and updates them in two lists()
    		
    		friend ostream& operator<< (ostream& os, const Item& item);//This prints out the representation of an item.
    	
    	private:
    		string name;
    		double price;
    		list<string>listItems;
    		list<double>priceItems;
    
    };
    #endif
    Item.cpp
    Code:
    #include "Item.h"
    
    Item::Item(const string& NameItem, const double priceItem){
    	/*Get the item and prices from the files and copy them in a list*/
    	
    	price = priceItem;
    	name = NameItem;
    }
    /*This function open the files Inventary.txt then put the item in a list, 
    and the prices of those items in an other list*/
    void Item::updatingLists(){
    	
    	string word,prod;
    	double conver;
    	int temp=0;
    	
    	ifstream myflux("Inventary.txt");
    	if(myflux){
    		while(myflux>>word){
    			if(temp==0){
    					listItems.push_back(word);
    					temp++;
    				}
    			else{
    				conver = atof(word.c_str());
    				priceItems.push_back(conver);
    				temp=0;
    			}
    				
    		}
    	}
    	myflux.close();
    		list<string>::const_iterator it1=listItems.begin();
    		list<double>::const_iterator it2=priceItems.begin();
    		
    		while(it1!=listItems.end()){
    			cout<<*it1<<"\t"<<*it2<<endl;
    			*it1++;
    			*it2++;
    		}
    		
    		cout<<endl;
    }
    Item::Item(){
    	name = "Empty";
    	price = 0;
    }
    
    string Item::getName()const{
    	return name;
    }
    
    double Item::getPrice()const{
    	return price;
    }
    
    ostream& operator<< (std::ostream& os,  const Item& item){
    	os<<" - ";
    		os<<item.getName()<<" --> "<<item.Item::getPrice();
    	os<<" - ";
    	os<<endl;
    	
    	return os;
    }
    ShoppingCart.h
    Code:
    #ifndef SHOPPER_H
    #define SHOPPER_H
    
    #include "Item.h"
    #include "ShoppingCart.h"
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <list>
    using namespace std;
    
    class Shopper{
    	public:
    		
    		Shopper(const string& firstName, const string& lastName); //This creates 
    									//a shopper with this name and with an empty ShoppingCart.
    		void addItemToCart(const Item& item, int numItems); //This adds numItems items 
    							//of the same type to the shopping cart. If no second parameter
    							// is specified, only one item should be added.
    														
    		int amountOwed();/*This is the amount of money in cents that Shopper needs to pay the cashier in the
    						hardware store. It is the total cost of the items in the cart plus an additional 
    						8.875% tax. The price should be rounded up to the nearest cent. 
    						(reminder: the ceil function in the <cmath> header file, rounds up a double to 
    						the nearest integer).*/
    		friend ostream& operator<<(ostream& os, const Shopper& shopper); //Prints the representation for a shopper.
    	
    		string getFname()const;
    		string getLname()const;
    
    	private:
    		string firstName;
    		string lastName;
    		ShoppingCart cart;
    		int numItems;
    		
    };
    
    #endif
    ShoppingCart.cpp
    Code:
    #include "ShoppingCart.h"
    
    ShoppingCart::ShoppingCart():myItems(){
    	//cout<<"Just Working"<<endl;
    }
    
    void ShoppingCart::addItem(const Item& item){
    	/*Create a new Item in the ShoppingCart*/
    	myItems.push_back(item);
    
    } 
    		
    double ShoppingCart::grandTotal(){
    		double total=0.0;
    		
    		for(list<Item>::const_iterator it=myItems.begin(); it != myItems.end(); it++){
    			 total+=it->getPrice();
    		}
    		
    	return total;
    }
    
    int ShoppingCart::numItems(){
    	return myItems.size();
    }
    
    ostream& operator<<(ostream& os, ShoppingCart& sc){
    	
    		return os;
    }

  12. #12
    Join Date
    Sep 2016
    Posts
    16

    Re: Passing an object through functions

    i know what was the problem.
    I wasn't compiling correctly.

    I was doing:
    Code:
    :> g++ -c Shopper.cpp
    :> g++ -c HardwareStore.cpp
    :> g++ -c App.cpp
    :> g++ -o App App.cpp HardwareStore.cpp
    :> g++ -o App App.cpp StoreSimulator.cpp
    Until now, everything is working.
    but i was compiling this
    Code:
    g++ -o StoreSimulator StoreSimulator.cpp HardwareStore.cpp
    Instead of this
    Code:
    g++ -o StoreSimulator Item.o ShoppingCart.o Shopper.o HardwareStore.o StoreSimulator.o

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)