Copy Constructors...
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 :)
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:
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:
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;}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement