copy constructor problem

Closed Thread

Join Date: Oct 2003
Posts: 3
Reputation: bluegirl is an unknown quantity at this point 
Solved Threads: 0
bluegirl bluegirl is offline Offline
Newbie Poster

copy constructor problem

 
1
  #1
Oct 13th, 2003
Hi, i need help creating a copy constructor.
i have a class called CElist (and another called CEmployee)
and it contains methods like this one:

bool CElist::getFirst(CEmployee& e)
{
//do some stuff
//sets e to the first employee and returns true
//returns false if it cant
}

my problem is with the copy constructor:

CElist::CElist(const CElist& eList)
{
CEmployee newEmp;
eList.getFirst(newEmp); //this is the error part! --> cannot convert 'this' pointer from 'const class CElist' to 'class CElist &'

this.setFirst(newEmp);
//do other stuff
}

i dont think im changing the eList right? Right now the only solution i can think of is to create a different function that returns the first CEmployee, not a bool...
>< thanks for any help
Quick reply to this message  
Join Date: Oct 2003
Posts: 3
Reputation: bluegirl is an unknown quantity at this point 
Solved Threads: 0
bluegirl bluegirl is offline Offline
Newbie Poster

Re: copy constructor problem

 
0
  #2
Oct 14th, 2003
hmm.. well it works if i dont make the paramter const, so then i guess my question would be, does the parameter for a copy constructor always have to be const? (i know if im just copying values it should be, but....)

sorry, im new at c++ ><
Quick reply to this message  
Join Date: Sep 2003
Posts: 22
Reputation: Mike29936 is an unknown quantity at this point 
Solved Threads: 0
Mike29936 Mike29936 is offline Offline
Newbie Poster

Re: copy constructor problem

 
1
  #3
Oct 14th, 2003
You only make something const(constant) if it's , well, constant. If you're going to change anything about it, or any sorts of funky errors pop up, you're better off not const-ing it.

I personally never use const, though I did take C first, so that might have something to do with it. If it's not absolutely required that you put const there, then don't.

What are you doing in your program, though?
Quick reply to this message  
Join Date: Oct 2003
Posts: 3
Reputation: bluegirl is an unknown quantity at this point 
Solved Threads: 0
bluegirl bluegirl is offline Offline
Newbie Poster

Re: copy constructor problem

 
0
  #4
Oct 15th, 2003
oh its to maintain a list of employees (for one of my assignments...) but its alrite^^ i fixed it --> didnt know that i can access the private data members of the passed instance in the constructor... i htought it would be illegal but guess not^__^
i learned java before, but now im doing c++, and its ...werid hehe
thanks for the reply.
Last edited by bluegirl; Oct 15th, 2003 at 4:36 am.
Quick reply to this message  
Join Date: Oct 2003
Posts: 1
Reputation: robysad is an unknown quantity at this point 
Solved Threads: 0
robysad robysad is offline Offline
Newbie Poster

Re: copy constructor problem

 
1
  #5
Oct 18th, 2003
i thought it's wrong ,whick use const parameter to construct Class CELIST,illegel grammar.
CElist::CElist(const CElist& eList)//org
CElist::CElist(CElist& eList)//try it
Quick reply to this message  
Join Date: Jul 2005
Posts: 76
Reputation: CrazyDieter is an unknown quantity at this point 
Solved Threads: 3
CrazyDieter's Avatar
CrazyDieter CrazyDieter is offline Offline
Junior Poster in Training

Re: copy constructor problem

 
0
  #6
Jul 7th, 2005
For me, the problem is the following :

in your CElist constructor, the parameter is const, so you cannot call methods that are not const on this object.

You have to change the signature of your getFirst method :

bool CElist::getFirst(CEmployee& e) const;

if this method do not modifies modifies the insternal state of the object (because now 'this' is a 'const CElist*'

Originally Posted by bluegirl
Hi, i need help creating a copy constructor.
i have a class called CElist (and another called CEmployee)
and it contains methods like this one:

bool CElist::getFirst(CEmployee& e)
{
//do some stuff
//sets e to the first employee and returns true
//returns false if it cant
}

my problem is with the copy constructor:

CElist::CElist(const CElist& eList)
{
CEmployee newEmp;
eList.getFirst(newEmp); //this is the error part! --> cannot convert 'this' pointer from 'const class CElist' to 'class CElist &'

this.setFirst(newEmp);
//do other stuff
}

i dont think im changing the eList right? Right now the only solution i can think of is to create a different function that returns the first CEmployee, not a bool...
>< thanks for any help
Quick reply to this message  
Join Date: Sep 2004
Posts: 7,540
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: copy constructor problem

 
0
  #7
Jul 7th, 2005
Don't resurrect ancient threads.
I'm here to prove you wrong.
Quick reply to this message  
Closed Thread

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC