operator+ ::a little problem


hi!
i have created a simple class with operator+ to allow adding int to new objects.

folder widmok.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef _WIDMOK_H
#define	_WIDMOK_H
const int rozmiar=1024;
class widmok {
public:
    //widmok();
    widmok(const widmok& orig);
    virtual ~widmok();
        int kanal[rozmiar];
        double x;
        widmok(int wartosc = 0);
        widmok operator+(int);
private:

};

#endif	/* _WIDMOK_H */ 



folder widmok.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include "widmok.h"
#include <iostream>
using namespace std;
//widmok::widmok() {
//}

widmok::widmok(const widmok& orig) {
}

widmok::~widmok() {
}

widmok::widmok(int wartosc){
    for (int i=0;i<rozmiar;i++){
        kanal[i]=wartosc;
    }
}
widmok widmok::operator+(int liczba){
        widmok rezultat(0);
        for (int i=0;i<rozmiar;i++){
        rezultat.kanal[i]=kanal[i]+liczba;  // there is *this
        cout<<"\nhello it's me! operatorek!    liczba: "<<liczba<<" kanal[i]: "<<kanal[i]<<"i: "<<i<<"\n";
    }
        cout<<"1: "<<rezultat.kanal[1]<<"  2: "<<rezultat.kanal[2]<<endl;
    return rezultat;
}


and main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdlib.h>
#include "widmok.h"
#include <iostream>
using namespace std;

/*
 * 
 */
int main(int argc, char** argv) {
    puts(" OK!");
    cout<<"\nargc: "<<argc<<endl;
    puts(*argv);
    const int rozmiarek=1024;
    widmok* a=new widmok(3);

    widmok kobalt(6);
    widmok newObj;
    /////////////////////////////////// ! /////////////////////////////////
    newObj=kobalt+5;
    cout<<kobalt.kanal[40]<<endl;
    puts("And newObj?");//cout<<(kobalt+4).kanal[40];
    cout<<"\nnewObj.kanal[40]: "<<newObj.kanal[40]<<endl;
    return (EXIT_SUCCESS);
}


the problem with operator is, that although it is working when called by using "+" the object "nowy" is still empty after operation. operator+ returns a new object of my class widmok. This object has desired values on each kanal[] in the operator body, but newObj initialized with it is still empty. Is sth wrong with returning here?
That's because you've defined a copy constructor that does nothing.

You should either remove it or define it to do copy the class members. The copy constructor is used in widmok::operator+(int) to copy local object rezultat out.
Hey,

where is the object "nowy" you are talking about?
and what do you expect to see, when cout-ing newObj.kanal[40]? i get 11 and it seems to be right.

and 2 other things: if you write a c++ programm use cNAME instead of NAME.h when including headers and why are you mixing cout and puts?
Thank you very much.
@kbw: yes, of course, how could i forget! ; ) it was copy constructor who destroyed the work operator+ did.
@Mathes: regarding this above it is strange that you get right result. and sorry, i was obviously talking about newObj, i've had to change language of my code(names of variables). and yes, 11 is all i wanted to see.
Last edited on
Topic archived. No new replies allowed.