Sign in to follow this  
SL33pY_Kr3W

Copy Constructors...

Recommended Posts

SL33pY_Kr3W    103
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 :)

Share this post


Link to post
Share on other sites
SoulSkorpion    238
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;
}







Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this