473,699 Members | 2,801 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2745
On 8 Jun 2004 03:37:38 -0700, j0******@engine er.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::opera tor=(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::opera tor=(const char* string) {
char* newString = new char[std::strlen(str ing) + 1];
std::strcpy(new String, 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************ *************@p osting.google.c om>, j0mbolar
<j0******@engin eer.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<cha r> or std::string...

--
Richard Herring
Jul 22 '05 #3
j0******@engine er.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******@engine er.com (j0mbolar) wrote in message news:<2d******* *************** ***@posting.goo gle.com>...
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
2602
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 no type
4
35024
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’ with no type inf2_classes.h:6: error: expected ‘;’ before ‘<’ token make: *** Error 1
3
50486
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 ';' before '<' token boxmanager.h:17: error: ISO C++ forbids declaration of 'vector' with no type
1
7335
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 the client (caller) of the derived class. i got it working under visual studio 2003/2005 but gcc 4.1.0 compilation fails. here's the smallest subset of the code that shows the error:
7
9426
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
5466
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 doAgain() { bool check; char choice; cout<<"Do you want to do another set of problems now?\n" <<"Enter y(yes) or n(no): "; cin>>choice; check=(choice=="y");
8
7868
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
14720
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 declerations.... #include <queue> #include <vector> #include <string> class database { queue<delayTP> delayThrouput;
10
4331
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 in Linux with GCC. pthread_cond_signal(&(receiverConf->receive_q_cond)); pthread_cond_destroy(&(receiverConf->receive_q_cond)); pthread_mutex_destroy(&(receiverConf->receive_q_lock)); Main.cpp:545: ISO C++ forbids declaration of ` ...
0
8686
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8615
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9173
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8911
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8882
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6533
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4375
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3057
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2009
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.