Class Definition: a problem

Hello Guys...
I'm working on classes and can't quite figure out what's wrong with my program:
aparently there's some problem in the definition of my class, but it seems correct to me.

Here's the code:
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
27
28
// A Class for Complex numbers!

#include<iostream>
#include<cmath>
using namespace std;

class compl {
      double re;
      double im;
      
public:
       compl(double x, double y){ re=x; im=y;}
       compl(compl &z){re=z.re; im=z.im}
       
       void disply();
       double get_re(){return re;}
       double get_im(){return im;}
       double sq_mod(){return re*re + im*im;}
       double mod(){return sqrt(sq_mod());}
       double angl(){return atan2(im, re);}
       void coniug(const compl &z){z.re=re; z.im=-im; return;}
       
       compl &operator= (const compl &z);
       compl operator+ (const compl &z)const;
       compl operator-(const compl &z)const;
       compl operator*(const compl &z)const;
       compl operator/(const compl &z)const;
};


my compiler (Dev-C++) gives these messages:

line 7 expected identifier before '~' token
line 7 expected class-name before '{' token
line 7 expected `,' or `;' before '{' token


what do you think is the problem?
compl is a reserved keyword:

http://msdn.microsoft.com/en-us/library/99wd44e4(VS.80).aspx

You need to name your class something else.


Other things I'd recommend changing that aren't causing compiler errors:

- Your copy ctor is incorrect. It should be:

 
classname(const classname& z)  // note the 'const' 


But you don't need to define your own copy ctor for this class anyway. The default one will work fine.

- Also your get, sq_mode, angl, etc funtcions should probably be const as well.
....a keyword....I can't believe it!!!
thank you so much, i was going crazy with this...i couldn't understand!

but now I have another few questions:

1. I didin't mean to put a copy constructor: where did I put it?Which one is it?
all I wanted to put were two overloaded constructors

2. Why the const? is it so the class called by the member funcion won't be modified by the function? This can also be achieved by a correct copy constructor, right?

Thank you SO much!!
PS...that keyword isn't even listed in the C++ handbook i'm using, although that book is pretty old (1998)
1. Line 13 is the one I was referring to. It's almost a copy ctor, but not quote because of the const thing I mentioned before (it would be a copy ctor if 'z' was passed by const reference)

1.5. Also you don't need to write an assignment operator (line 23) either, for the same reason. The default assignment operator will work just fine for this class.


2. Yes @ your first question. No @ your second question. It's const correctness. Basically any member function that doesn't change the state of the object should be const.

See how your +, -, etc operators have that const keyword after them? That means they won't change any data members of the class. Since you get, etc functions don't change anything, they should also be const. Otherwise something like this would be a problem:

1
2
3
const complex foo = whatever;

double bar = foo.get_im();  // ERROR, 'foo' is const, so you can't call a nonconst function 



EDIT: oh yeah... the PS:

I think it might be one of the keywords that were added as an alternative to using symbols that are hard to type on some keyboards.

IE: and became a keyword as an alternative to the & symbol
xor became a keyword as an alternative to ^
and I guess compl became an alternative to ~

Honestly, I didn't know about compl myself until I saw this post
Last edited on
OK!!
Thank you very much!!!
OUch!

My const correctness is either very incorrect, or I completely got rferences wrong, or whatever:

What's wrong in this member opertor definition?

1
2
3
4
5
6
7
8
9
10
11
12
13
...
// public part of my class
public:
comp operator+ (const comp &z)const;
};

// defining the class menber

comp comp::operator+ (const comp &z)const
{
      return comp(re + z.re, im + z.im);
}


my compiler says :
no matching function for call to `comp::comp(comp)'
note: candidates are: comp::comp(comp&)

Did I call the constructor??
OKAY, pretty much the same problem as before, but with an even more basic program: I think I'm doing something wrong in initializing my classes, but I don't know what it is..
Here'e my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Testing testing 123

#include<iostream>
using namespace std;

class thing
{
      int i;
public:
       thing(int x){i=x;}
       int get(){return i;}
};

int main()
{
    int N=10;
    thing thingy[N]
   
    thingy[0](4);
    cout<<thingy[0].get();
}



My compiler doesn't like what I do at line 17:
17 no matching function for call to `thing::thing()'
Any Idea what I'm doing wrong?
thanks
Try a semicolon at the end of that line.

And maybe before the semicolon an = new thingy(x)

-Albatross
Last edited on
ok, yeah, I had caught that semicolon...

so you're telling me to allocate memory for the array when I build it, which makes perfect sense, but what I don't understand now is why does this work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

// Testing testing 123

#include<iostream>
using namespace std;

class thing
{
      int i;
public:
       thing(int x){i=x;}
       int get(){return i;}
};

int main()
{
    int N=10;
    thing thingy(4);
   
    cout<<thingy.get();
}


Crippy
oh, and does the same apply for the previous post made, Apr 15, 2010 at 5:12pm?
Topic archived. No new replies allowed.