C++ - Constructors

Discussion in 'Programming & Software Development' started by shihyuan, Oct 15, 2009.

  1. shihyuan

    shihyuan Member

    Joined:
    Apr 21, 2004
    Messages:
    50
    Location:
    Thornbury 3071 VIC
    I am new to C++ and i am having troubles getting it to work.

    So I have a property.h interface file and then i want to implement the functions in a property.cpp file.

    i have included the codes which i am having troubles with.

    ------------------------ some codes from property.cpp ----------------------

    // default constructor
    Property()
    {
    owner = "";
    address = "";
    suburb = "";
    postcode = 0000;
    }
    error: expected unqualified-id before ')' token

    // argument constructor
    Property(string o, string a, string s, int p)
    {
    owner = o;
    address = a;
    suburb = s;
    postcode = p;
    }
    error: expected ')' before '0'

    ------------------------ some codes from property.h ------------------------

    Property();
    Property(string theOwner, string theAddress, string theSuburb, int thepostCode);


    I hope anyone would understand what i am talking about.
     
  2. Menthu_Rae

    Menthu_Rae Member

    Joined:
    Mar 19, 2002
    Messages:
    6,874
    Location:
    Northern Beaches, Sydney
    What happens if you change it to this?

    Code:
    // default constructor
    Property()
    {
    owner = "";
    address = "";
    suburb = "";
    [DEL]postcode = 0000;[/DEL]
    postcode = 0;
    }
     
  3. OP
    OP
    shihyuan

    shihyuan Member

    Joined:
    Apr 21, 2004
    Messages:
    50
    Location:
    Thornbury 3071 VIC
    I just tried that and it didn't solved the problem. Any other suggestions?

     
  4. oupimiquo

    oupimiquo Member

    Joined:
    Sep 20, 2007
    Messages:
    520
    Check your scopes - is the Property() implementation in the scope of the Property class? The "usual" way to do it would be
    Code:
    Property::Property()
    {
      // stuff
    }
    in the CPP file.
     
  5. thommo101

    thommo101 Member

    Joined:
    Jul 24, 2002
    Messages:
    1,047
    Location:
    Perth WA
    I assume owner/address etc are member variables defined in your header file?

    If so the nice way of initialising these is typically in the initialisation list.

    Code:
    // default constructor
    Property::Property()
        :
        owner(""),
        address(""),
        suburb(""),
        postcode(0){}
    
    // argument constructor
    Property::Property(string o, string a, string s, int p)
        :
        owner(o),
        address(a),
        suburb(s),
        postcode(p){}
    Alternatively, defined default parameters in your header:

    Code:
    //----------------HEADER-------
    #include <string>
    
    class Property
    {
      public:
        Property(string o = "", string a = "", string s = "", int p = 0);
        virtual ~Property();
    
        string owner;
        string address;
        string suburb;
        int    postcode;
    };
    
    //------------------ CPP -----------
    Property::Property(string o, string a, string s, int p)
        :
        owner(o),
        address(a),
        suburb(s),
        postcode(p){}
    
    Property::~Property() {}
     
  6. OP
    OP
    shihyuan

    shihyuan Member

    Joined:
    Apr 21, 2004
    Messages:
    50
    Location:
    Thornbury 3071 VIC
    thommo101, I tried out your codes and I encountered new errors.

    [​IMG]

    I am really lost now...
     
  7. oupimiquo

    oupimiquo Member

    Joined:
    Sep 20, 2007
    Messages:
    520
    You've got using namespace std; somewhere? Otherwise you'll need std::string not just string.

    Also, there's something a little wonky - why have you got owner, address, suburb, and postcode declared just above the constructor? That should be in the class definition, not in what appears to be the global namespace.
     
  8. OP
    OP
    shihyuan

    shihyuan Member

    Joined:
    Apr 21, 2004
    Messages:
    50
    Location:
    Thornbury 3071 VIC
    I have included some codes from the interface class file and the implementation file so you can get a better idea.


    ------------------------------property_a.h----------------------------------

    #ifndef __PROPERTY_A_H__
    #define __PROPERTY_A_H__

    class Property
    {
    protected:
    string owner;
    string address;
    string suburb;
    int postcode;

    public:
    Property();
    Property(string theOwner, string theAddress, string theSuburb, int thepostCode);
    virtual ~Property() ;
    void input() ; // Data input for a Property object
    void print() ; // Data output for a Property object

    string getOwner() const; //Note the use of const
    string getAddress() const;
    string getSuburb() const;
    int getPostcode() const;

    void setOwner(string newOwner);
    void setAddress(string newAddress);
    void setSuburb( string newSuburb);
    void setPostcode(int newPostcode);
    };

    #endif



    --------------------------------property_a.cpp------------------------------


    #include "property_a.h"
    #include <iostream>
    #include <string>
    using namespace std;

    string owner;
    string address;
    string suburb;
    int postcode;

    // default constructor
    Property::property() : owner(""), address(""), suburb(""), postcode(0) {}

    // argument constructor
    Property::property(string theOwner, string theAddress, string theSuburb,
    int thepostCode) : owner(theOwner), address(theAddress),
    suburb(theSuburb), postcode(thepostCode) {}

    I get the following errors when compiling

    [​IMG]
     
  9. Mikos

    Mikos Member

    Joined:
    Mar 12, 2004
    Messages:
    2,945
    Location:
    Cydonia
    Tried a standard default constructor along the lines of

    Code:
    Property::Property()
    {
    }
    or even

    Code:
    Property::Property()
    {
      owner = "";
      address = "";
      suburb = "";
      postcode = 0;
    }
    instead of the ones giving errors? Could also be because they are protected rather than public. Though I haven't touched C++ for a long time so not real sure.
     
  10. OP
    OP
    shihyuan

    shihyuan Member

    Joined:
    Apr 21, 2004
    Messages:
    50
    Location:
    Thornbury 3071 VIC
    if i do this

    Property::property() {}

    i get the error: Property::property()
     
  11. underskore

    underskore Member

    Joined:
    Nov 5, 2002
    Messages:
    4,336
    Location:
    3198
    use the set functions instead of accessing the variables directly ?

    edit: wait that would probably try to call a default constructor or something... dont think it will work.
     
  12. BAC :S

    BAC :S Member

    Joined:
    Dec 26, 2001
    Messages:
    1,385
    Location:
    Melbourne
    You need to make sure that the string type is declared in your header file.

    Code:
    #ifndef __PROPERTY_A_H__
    #define __PROPERTY_A_H__
    
    #include <string>
    
    using namespace std;
    
    class Property
    {
    ...
    }
    
    Otherwise you are using an undeclared type. You should get your IDE to sort the errors in the order it finds them.

    EDIT:

    Also what are:
    Code:
    string owner;
    string address;
    string suburb;
    int postcode;
    
    doing in the cpp file?

    EDIT 2:

    I assume you've just started programming but a good thing to look into early on is const correctness, its something that gets overlooked by people starting programming but its a very useful tool for catching errors at compile time and makes you thinking about how fields and methods will be used. I see you've started using it but its good to make sure you really understand it. Also you should pass complex objects by reference:

    Code:
    Property::Property(const string& theOwner, const string& theAddress, const string& theSuburb, int thepostCode)
    
    this saves the arguments given to the constructer being copied to the local argument variables and then into the fields of the class.

    You can also use this when returning complex fields:

    Code:
    const string& getOwner() const;
    
    Anyway, pretty minor things when you are starting off but its good to get your head around them as early as possible, good luck with the programming.



    Cheers,
    Mark.
     
    Last edited: Oct 15, 2009
  13. OP
    OP
    shihyuan

    shihyuan Member

    Joined:
    Apr 21, 2004
    Messages:
    50
    Location:
    Thornbury 3071 VIC
    you are genius, Mark

    Thanks heaps for that.... cant believe I didnt see that....
     

Share This Page

Advertisement: