Jump to content
  • Advertisement
Sign in to follow this  
SL33pY_Kr3W

Copy Constructors...

This topic is 4956 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
Guest Anonymous Poster
Use the [source] tag

Share this post


Link to post
Share on other sites
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!