By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,384 Members | 2,035 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,384 IT Pros & Developers. It's quick & easy.

ANSI C++ forbids declaration..

P: n/a
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?
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On 8 Jun 2004 03:37:38 -0700, j0******@engineer.com (j0mbolar) wrote:
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::operator=(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::operator=(const char* string) {
char* newString = new char[std::strlen(string) + 1];
std::strcpy(newString, string);
delete[] m_string;
m_string = newString;
return *this;
}

Even better, just use std::string from the <string> header.
}

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
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #2

P: n/a
In message <2d*************************@posting.google.com> , j0mbolar
<j0******@engineer.com> writes

[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...

--
Richard Herring
Jul 22 '05 #3

P: n/a
j0******@engineer.com (j0mbolar) wrote:
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);
Jul 22 '05 #4

P: n/a
j0mbolar wrote:
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
Jul 22 '05 #5

P: n/a
j0******@engineer.com (j0mbolar) wrote in message news:<2d*************************@posting.google.c om>...
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
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.