473,387 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

ANSI C++ forbids declaration..

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
5 2728
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Henrik S. Hansen | last post by:
I'm new to C++, and cannot figure out why this won't compile: std::map<std::string, int> tst; tst = 1; int main() { /*...*/ } It gives me: error: ISO C++ forbids declaration of `tst' with...
4
by: Juhan Voolaid | last post by:
Hi I need help here. When i compile my program, i get this error: $ make g++ -c -Wall inf2_functions.cpp -o inf2_functions.o inf2_classes.h:6: error: ISO C++ forbids declaration of ‘vector’...
3
by: gamehack | last post by:
Hi all, Here's the error which I'm getting when trying to compile some code: boxmanager.h:16: error: ISO C++ forbids declaration of 'vector' with no type boxmanager.h:16: error: expected...
1
by: eric | last post by:
hello i'm trying to implement some functionality whereby an algorithm in a base template class relies on a function pointer supplied by a derived template class. the types are only specified by...
7
by: Florian Haag | last post by:
Hello, I'm trying to compile a programme which compiles fine under Linux; I'm trying it with MinGW G++ 3.4.2: Component.h: #ifndef COMPONENT_H_ #define COMPONENT_H_
1
by: pbaldridge | last post by:
I'm trying to code a simple function that will prompt a user if they want to see another set of problems. The error I receive is "ANSI C++ forbids comparison between pointer and integer" int...
8
by: aneuryzma | last post by:
Hello, I'm merging an OpenCV app with an Ogre3d app. I'm on a mac, I'm using xCode. When I add #include "openCVApp.h" I got the following error:
6
by: samsneelam | last post by:
Hi.. This is samuel, while doing a program, i encountered this problem.. Let me give you clarity regarding my prob.. I am having two files .. one is mpcplib.h it contains the follwing...
10
by: tvnaidu | last post by:
I am using Three pthread functions below, I got ISO error, then I declared int variable called val123, then I assigned, but still I am getting error, any idea?. also I included pthread.h. compiling...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.