Copy Constructors...

Started by
3 comments, last by SL33pY_Kr3W 19 years, 4 months ago
Im having trouble compiling this program I made. The problem lies within the copy constructor for the Krew class. The errors Im getting are: In copy constructor `Krew::Krew(const Krew&)': and passing `const Krew' as `this' argument of `std::string Krew::GetName()' discards Here is the code: //copy constructor #include<iostream> #include<string> class Krew //class def { public: Krew(std::string name = "") { m_pname = new std::string(name); return; } ~Krew() { delete m_pname; } Krew(const Krew& copy) //copy constructor { m_pname = new std::string; *m_pname = copy.GetName();//The problem**** } std::string GetName() //get name { return *m_pname; } private: std::string* m_pname; }; void testCopyConstructor(Krew crit) { std::cout<< crit.GetName()<<std::endl; return; } int main() // main function { Krew aFool("meat"); std::cout<< aFool.GetName() <<std::endl; testCopyConstructor(aFool); std::cout<< aFool.GetName() <<std::endl; system("pause"); return 0; } Any help would be appreciated :)
Advertisement
Use the [source] tag
The short answer:
Change std::string GetName() to std::string GetName() const.

The long answer:
In your copy constructor the reference to the other object is marked as constant. That's normal and right, because it means that you can make copies from Krew objects that are constant. The thing is it's calling GetName(). At present, it doesn't know whether GetName() modifies the object or not so it has to assume that it might. Of course, GetName() doesn't modify the object, so you can tell the compiler that by adding the const keyword after the method's parameters.

Without the const there code like this wouldn't work either:
const Krew someKrew("pie");std::cout << someKrew.GetName();

By the way, it helps if you post code inside source tags, like this:
{source}your code goes in here{/source}

...except replace the {}s with []s.

That puts it in a nice syntax-highlighted scroll box, that looks like this:
//copy constructor#include<iostream>#include<string>class Krew //class def{public:Krew(std::string name = ""){m_pname = new std::string(name);return;}~Krew(){delete m_pname;} Krew(const Krew& copy) //copy constructor{m_pname = new std::string;*m_pname = copy.GetName();//The problem****} std::string GetName() //get name{return *m_pname;}private:std::string* m_pname;};void testCopyConstructor(Krew crit){std::cout<< crit.GetName()<<std::endl;return;}int main() // main function{Krew aFool("meat");std::cout<< aFool.GetName() <<std::endl;testCopyConstructor(aFool);std::cout<< aFool.GetName() <<std::endl;system("pause");return 0;}
-------------"On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question."- Charles Babbage (1791-1871)
SoulSkorpion > Cool answer, props!
Anything posted is personal opinion which does not in anyway reflect or represent my employer. Any code and opinion is expressed “as is” and used at your own risk – it does not constitute a legal relationship of any kind.
Thank you very much! That clears alot up.

This topic is closed to new replies.

Advertisement