ANSI C++ forbids declaration..

J

j0mbolar

operator = (const char *string) {
if(m_string) {
free(m_string);
m_string = 0;
}
if(string) {
m_string = strdup(string);
}
}

generates the error:
"ANSI C++ forbids declaration `operator =' with no type

how should the return type be specified?
 
T

tom_usenet

operator = (const char *string) {
if(m_string) {
free(m_string);
m_string = 0;
}
if(string) {
m_string = strdup(string);
}

What if you run out of memory? Instead:

MyString& MyString::eek:perator=(const char* string) {
char* newString = strdup(string);
if (!newString)
throw std::bad_alloc(); //out of memory
free(m_string); //remember free(NULL) is a no-op
m_string = newString;
return *this;
}

However, bear in mind that strdup is a non-standard function, so
you'll need to provide your own definition for it if you want to port
your code. Here's a more C++ version:

MyString& MyString::eek:perator=(const char* string) {
char* newString = new char[std::strlen(string) + 1];
std::strcpy(newString, string);
delete[] m_string;
m_string = newString;
return *this;
}

}

generates the error:
"ANSI C++ forbids declaration `operator =' with no type

how should the return type be specified?

Generally you return *this, so your return type will be TheClass&.

Tom
 
R

Richard Herring

In message <[email protected]>, j0mbolar

[inside a class declaration, presumably]
operator = (const char *string) {
if(m_string) {
free(m_string);
m_string = 0;
}
if(string) {
m_string = strdup(string);
}
}

generates the error:
"ANSI C++ forbids declaration `operator =' with no type

how should the return type be specified?

Usually, like this:
class MyClass
{
public:
MyClass & operator=(const char * string)
{
// ...
return *this;
}
};

I won't ask why you're using hand-allocated char arrays instead of
std::vector<char> or std::string...
 
O

Old Wolf

operator = (const char *string) {
if(m_string) {
free(m_string);
m_string = 0;
}
if(string) {
m_string = strdup(string);
}
}

generates the error:
"ANSI C++ forbids declaration `operator =' with no type

how should the return type be specified?

Usually it returns a reference to the object, so you can chain:
obj1 = obj2 = "foo";
which means the same as
obj2 = "foo"; obj1 = obj2;

Also, "strdup" is a non-standard function. You should write:
char *ptr = (char *)malloc(strlen(string)+1);
if (!ptr) { ........ } // do something sensible here
strcpy(ptr, string);
m_string = ptr;

although it's a mystery to me why you would prefer this to:
m_string = new char[strlen(string)+1];
strcpy(m_string, string);
 
K

Kai-Uwe Bux

j0mbolar said:
operator = (const char *string) {
if(m_string) {
free(m_string);
m_string = 0;
}
if(string) {
m_string = strdup(string);
}
}

generates the error:
"ANSI C++ forbids declaration `operator =' with no type

how should the return type be specified?

Asside from the return type, you should beware that your class
might self destruct in certain circumstances:

#include <stdlib.h>
#include <iostream>

class C {
public:

char * m_string;

C ( void ) :
m_string( 0 )
{}

const C& operator = (const char *string) {
if(m_string) {
free(m_string);
m_string = 0;
}
if(string) {
m_string = strdup(string);
}
return( *this );
}

};

int main ( void ) {
C c;
c = "Hello world!";
std::cout << c.m_string << std::endl;
c = c.m_string;
std::cout << c.m_string << std::endl;
}


This greets the world only once.

You might find std::string more usefull. BTW, I found that
std::string usually performs *better* than my onw hand coded classes
ontaining char*. The reason is, that with char* you have to do your
own memory management. The magic hidden inside the STL shipped with
the compiler usually outperforms my own code.


Best

Kai-Uwe
 
M

Michiel Salters

operator = (const char *string) {
if(m_string) {
free(m_string);
m_string = 0;
}
if(string) {
m_string = strdup(string);
}
}

generates the error:
"ANSI C++ forbids declaration `operator =' with no type

how should the return type be specified?

For the given implementaion: void, like any other function
that you don't want to return anything.

That would make it legal, but not moral. See the other posts why.

Regards,
Michiel Salters
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,114
Latest member
GlucoPremiumReview
Top