Thread: passing by refrence problems

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    52

    passing by refrence problems

    I am stuck once again.

    Code:
    // reference  prblm. 2 pg368 c++ primer plus
    #include <iostream>
    using namespace std;
    
    struct candybar
    {
    	char name[20];
    	double weight;
    	int calories;
    };
    
    // prototypes passing by refrence
    candybar & set(candybar & candyref,char * brand="Hershy",double weigh=2.85,int cal=350);
    void show(const candybar & candyref);
    
    
    int main()
    {
    	candybar def
    	{
    		"none",
    		0.0,
    		0
    	};
    	show(set(def));
    	return 0;
    }
    
    candybar & set(candybar & candyref,char * brand="Hershy",double weigh=2.85,int cal=350)
    {
    	candyref.name=*brand;
    	candyref.weight=weigh;
    	candyref.calories=cal;
    	return candyref;
    }
    
    void show(const candybar & candyref)
    {
    	cout << "Candybar\n";
    	cout << "Brand name:"<<candyref.name<<endl;
    	cout << "Weight:"<<candyref.weight<<endl;
    	cout << "Calories:"<<candyref.calories<<endl;
    }
    Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
    ref.cpp:
    Error E2108 ref.cpp 19: Improper use of typedef 'candybar' in function main()
    * cant figure out this prblw

    Error E2379 ref.cpp 19: Statement missing ; in function main()
    * cant find the missing ;

    Error E2451 ref.cpp 25: Undefined symbol 'def' in function main()
    Error E2148 ref.cpp 30: Default argument value redeclared for parameter 'brand'
    Error E2277 ref.cpp 31: Lvalue required in function set(candybar &,char *,double
    ,int)
    Warning W8057 ref.cpp 35: Parameter 'brand' is never used in function set(candyb

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    structs in C++ are just like classes except the members are all public by default. Therefore in main() I would declare an instance of candybar called def like this:


    candybar def;

    without the function body after it. The above line uses the default default constructor provided by the compiler to set aside memory for def. The data members in def are undefined. To give the "default" values you want you have to use the set() function or set up a default constructor for candybar struct/class.

    set(def, "none", 0.0, 0);

    then you can change the default values you put in by doing:

    set(def);

    which will use the default arguments in set(). Call the show function after each call to set() to see if it works.

    ...or use vVv's technique.
    Last edited by elad; 03-22-2002 at 09:30 PM.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    52

    working

    here is a copy of the working code
    thanks for the help
    [code]
    // reference prblm. 2 pg368 c++ primer plus
    #include <iostream>
    using namespace std;

    struct candybar
    {
    char name[20];
    double weight;
    int calories;
    };

    // prototypes passing by refrence
    candybar & set(candybar & candyref,char * brand="Hershy",double weigh=2.85,int cal=350);
    void show(const candybar & candyref);


    int main()
    {
    candybar def;
    show(set(def,"none",0.0,0));
    show(set(def));
    return 0;
    }

    candybar & set(candybar & candyref,char * brand,double weigh,int cal)
    {
    strcpy( candyref.name, brand );
    //candyref.name=brand;
    candyref.weight=weigh;
    candyref.calories=cal;
    return candyref;
    }

    void show(const candybar & candyref)
    {
    cout << "Candybar\n";
    cout << "Brand name:"<<candyref.name<<endl;
    cout << "Weight:"<<candyref.weight<<endl;
    cout << "Calories:"<<candyref.calories<<endl;
    }
    [\code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  2. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  3. Problems passing an array to a function
    By ndenklau in forum C Programming
    Replies: 5
    Last Post: 09-20-2006, 08:14 AM
  4. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM
  5. problems passing parameters between functions
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-21-2001, 11:41 AM