Thread: Operator Overloading problem

  1. #1
    Registered User
    Join Date
    Oct 2008
    Location
    Finland
    Posts
    1

    Operator Overloading problem

    Hello. I have a class that holds a set of data. Each piece of data is represented by another class. I wanted to overload the += operator for the dataset class, but it appears gcc mistakes my effort as trying to use the binary + operator. Below is the related function.

    Code:
    CDataFile *CDataFile::operator+=(CData *wave) {
    CNode *node;
    	node=new CNode((void *)wave);
    	WaveList.AddTail(node);
    	return this;
    }
    WaveList is a member object of CDataFile. Below, datafile is an instance of CDataFile and currwave one of CData. This is the statement, with which I try to add new data to the dataset:

    datafile+=currwave;

    However, this gives me the following error.

    In function `int main(int, char**)':
    error: invalid operands of types `CDataFile*' and `CData*' to binary `operator+'
    error: in evaluation of `operator+=(class CDataFile*, class CData*)'
    I'm a novice C++ writer, and I cannot figure out why it fails like this. It works, if I call the function as

    datafile.operator+=(currwave);

    and I am baffled why the actual operator overloading is causing havoc. Help!

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    I don't know why you get the errors to be honest.
    Though, what is the type of datafile? It should be CDataFile* to match the return type of the overloaded operator. But then again I think it is CDataFile because you can call datafile.operator+=(currwave).

    If datafile is the object and not a pointer to the object this should work:
    Code:
    CDataFile& CDataFile::operator+=(CData *wave) {
            CNode *node;
    	node=new CNode((void *)wave);
    	WaveList.AddTail(node);
    	return *this;
    }
    ...
    CData *wave = new CData;
    CDataFile datafile;
    datafile += wave;
    You say that both are instances of objects so I guess you have the returning type wrong. Though wave has to be a pointer not an instance

    The datafile.operator+=(currwave) works because you ignore the returning type. Which should make you think: do you really want an overloaded operator? You should make and Add function and do datafile.Add(currwave). But of course you know best what you want

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The operator += should take (const) and return a reference. So it becomes:
    Code:
    CDataFile& CDataFile::operator+=(const CData& wave)
    {
    	CNode* node;
    	node=new CNode(wave);
    	WaveList.AddTail(node);
    	return *this;
    }
    Also, you should need to cast to void*, since it is implicit.
    But no function should really take void* in C++, unless it is for C interoperability or compatibility. Instead, you should consider templates.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM