Link to home
Start Free TrialLog in
Avatar of DJ_AM_Juicebox
DJ_AM_Juicebox

asked on

easy java question, constructor not defined

Hi everyone,

easy question coming from c++... my java compiler (net beans) says I have an error using the constructor of CPerson in the last line of main():

    public static void main(String[] args)
    {
        // My linked list container.
        CLinkedList list;

        // Add a node.
        list.AddNode(
            CPerson("Joe", "111-222-3333", "some street"));
    }

It looks like it's saying that constructor method is not defined, but declaring a CPerson like this:

     CPerson person;

is ok. This is how I defined CPerson though:

    public class CPerson {
   
        /** Creates a new instance of CPerson */
        public CPerson()
        {
        }
        public CPerson(String strName,
                       String strPhoneNumber,
                       String strAddress)
        {
            m_strName = strName;
            m_strPhoneNumber = strPhoneNumber;
            m_strAddress = strAddress;
        }
    }

I must be something really simple. Why can't I use the constructor like that?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of suprapto45
suprapto45
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Then,

You also need to define the getter and setter (I think that this is also present in C++) so you should know about it :)

David
Avatar of CEHJ
>>list.AddNode(CPerson("Joe", "111-222-3333", "some street"));


should be

list.AddNode(new CPerson("Joe", "111-222-3333", "some street"));
> should be
> list.AddNode(new CPerson("Joe", "111-222-3333", "some street"));


thats already been pointed out
Missed it
Sorry suprapto45
you are meant to actually *read* all previous comments before posting :)
Avatar of DJ_AM_Juicebox
DJ_AM_Juicebox

ASKER

sorry I just posted a code snippet, I do have all the member variables defined, it was just that I was not using the new operator.

Why do we need to use the new operator in java for this example? Is it not possible to create class objects on the stack like that?

Thanks
>>Is it not possible to create class objects on the stack like that?

No, Java objects are created using new on the heap. There are static classes though and classes with static methods

http://mindprod.com/jgloss/static.html
Just be able to get online again.

Glad I could help :).

>>"Sorry suprapto45"
Not a problem at all. We all are just trying to help here.

David