ambiguous operator=

I have an assignment that is basically "implement your own c-string class". The overloaded assignment operator is declared as:
ourString & ourString::operator=(const ourString & orig);

With that function one object copies to another fine, but I need to be able to pass it a string literal. If I create
ourString & ourString::operator=(string orig)
the compiler complains that calling it with a string literal is ambiguous.

If I comment out the string version it will compile but then there are runtime errors. What am I missing?
there may be some bug in your ourString constructors. ( what is the runtime error about ? )
If you want to be sure to have an operator= which takes a string literal, overload it as const char*
Some kind of heap corruption error. I thought it was happening because when a string was assigned to an object its other members (capacity, length) were getting set to garbage values, but I just noticed I was trying to send a char to an array index in one of the tests...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
ourString & ourString::operator=(const ourString & orig)
{
	m_capacity = orig.m_capacity;
	m_length = orig.m_length;
	delete [] m_cstrPtr;
	m_cstrPtr = new char[m_capacity];
	strcpy(m_cstrPtr, orig.m_cstrPtr);
	return *this;
}

ourString & ourString::operator=(const char * orig)
{
	m_capacity = strlen(orig) + 1;
	m_length = strlen(orig);
	delete [] m_cstrPtr;
	m_cstrPtr = new char[m_capacity];
	strcpy(m_cstrPtr, orig);
	return *this;
}


Is something like this what you're talking about? Now Intellisense says, "no instance of overloaded function 'ourString::operator=' matches specified type".
Last edited on
Post all of your constructors, all of your assignment operators, and the line of code that is failing.
Topic archived. No new replies allowed.