Classes

Hi all, trying to write a class that deals with "complex" numbers for an assignment.
Anyway, it's going poorly. I think if I can get this first bit I'll be able to finish it, but even this isn't working.

So, given a class shell and a test program as follows. Functions for the constructors have to be created. 4 were given.
Basically, I want to syntax out everything and create and test the functions 1 by 1.

The first constructor should have a function which, given

Complex a1,

in the test program creates a1 to be a complex number (0,0i).
Upon compiling the code right now I get

multiple definition of `Complex::Complex()'
first defined here
multiple definition of `Complex::Complex()'
first defined here
multiple definition of `Complex::Complex(double, double)'
first defined here
multiple definition of `Complex::Complex(double, double)'
first defined here

\Makefile.win [Build Error] [Q1.exe] Error 1

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class Complex
{
   public:

//      CONSTRUCTORS

      Complex();                            // default constructor
      Complex(const double,const double);   // data    constructor

//      ARITHMETIC OPERATIONS

      Complex add(const Complex&)const;          // add 2 Complex objects
      void addEqual(const Complex&);             // += for Complex objects
      Complex subtract(const Complex&)const;     // subtract 2 Complex objects
      void subtractEqual(const Complex&);        // -= for Complex objects
      Complex multiply(const Complex&)const;     // multiply 2 Complex objects
      Complex multiplyPolar(const Complex&)const;// multiply 2 Complex objects
      Complex divide(const Complex&)const;       // divide 2 Complex objects
      Complex dividePolar(const Complex&)const;  // divide 2 Complex objects
      Complex negative()const;                   // unary minus
      Complex conjugate()const;                  // finds Complex conjugate

//      CONVERSION FUNCTIONS

      double r()const;                           // finds radius
      double theta()const;                       // finds polar angle
      Complex pr(const double,const double)const;// polar to rectangular

//      UTILITY FUNCTIONS

      //void print(ostream&);                      // outputs a Complex object

   private:

      double x;
      double y;
};

Complex::Complex()
{
   x=0;
   y=0;
}
Complex::Complex(double X, double Y)
{
   x=X; y=Y;                        
}
/*Complex Complex::add(const Complex&z)const
{
   return(Complex(x+z.x,y+z.y));
}
Complex Complex::multiplyPolar(const Complex&z)const
{
   return(pr(r()*z.r(),theta()+z.theta()));
}
Complex Complex::pr(const double r,const double theta)const
{
   return(Complex(r*cos(theta),r*sin(theta)));
}
void Complex::print(ostream&fout)
{
   fout<<setiosflags(ios::fixed|ios::showpoint)<<setprecision(4)
  	    <<"("<<setw(6)<<x<<","<<setw(6)<<y<<")";
}*/


and the start of the test program

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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>

using namespace std;

#include "complex.cpp"


int main()
{
   ofstream fout("cmplxout.txt");

   //double r4=6,
          //theta4=-M_PI,
          //r7=10,
          //theta7=2*M_PI/3;
   Complex z0,z1(-4, 3),z2(-4,-3),z3( 0, 3),z4,z5,z6(-2.1,7.6),z7,z8,z9,z10,
           z11,z12,z13,z14,z15,z16;

   fout<<"ORIGINAL DATA\n\n"
       <<  " z0 = ";
   //z0.print(fout);.......
   
   return(0);
}


I think if I can get past this first issue I'll be okay. I have no idea what it's on about at the moment,though.

Thanks in advance!!!

The makefile, if it helps. I don't understand anything in it >_<

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
29
30
31
32
33
# Project: Q1
# Makefile created by Dev-C++ 4.9.9.2

CPP  = g++.exe
CC   = gcc.exe
WINDRES = windres.exe
RES  = 
OBJ  = main.o complex.o $(RES)
LINKOBJ  = main.o complex.o $(RES)
LIBS =  -L"C:/Dev-Cpp/lib"  
INCS =  -I"C:/Dev-Cpp/include" 
CXXINCS =  -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include"  -I"C:/Dev-Cpp/include/c++/3.4.2/backward"  -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32"  -I"C:/Dev-Cpp/include/c++/3.4.2"  -I"C:/Dev-Cpp/include" 
BIN  = Q1.exe
CXXFLAGS = $(CXXINCS)  
CFLAGS = $(INCS)  
RM = rm -f

.PHONY: all all-before all-after clean clean-custom

all: all-before Q1.exe all-after


clean: clean-custom
	${RM} $(OBJ) $(BIN)

$(BIN): $(OBJ)
	$(CPP) $(LINKOBJ) -o "Q1.exe" $(LIBS)

main.o: main.cpp
	$(CPP) -c main.cpp -o main.o $(CXXFLAGS)

complex.o: complex.cpp
	$(CPP) -c complex.cpp -o complex.o $(CXXFLAGS)
Last edited on
Solved those two issues for now. May be back with more soon.
Last edited on
New issue:

The constructor and function for printing complex number to console were given,
however, doesn't work for me (see constructor and function below)
1
2
3
4
5
6
7
8
9
void print(ostream&);

...other code...

void Complex::print(ostream&fout)
{
   fout<<setiosflags(ios::fixed|ios::showpoint)<<setprecision(4)
  	    <<"("<<setw(6)<<x<<","<<setw(6)<<y<<")";
}


Constructor errors
variable or field `print' declared void
expected `;' before '(' token

function errors
variable or field `print' declared void
`int Complex::print' is not a static member of `class Complex'
'ostream 'was not declared in this scope
`fout' was not declared in this scope
expected `,' or `;' before '{' token
Last edited on
The function print works fine with me as you declare it here (no errors). Check your code prior to the function.
Maybe you have some syntactical errors ...

I checked it with this 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>

using namespace std;

class Complex
{
   public:

//      CONSTRUCTORS

      Complex();                            // default constructor
      Complex(const double,const double);   // data    constructor

//      ARITHMETIC OPERATIONS

      Complex add(const Complex&)const;          // add 2 Complex objects
      void addEqual(const Complex&);             // += for Complex objects
      Complex subtract(const Complex&)const;     // subtract 2 Complex objects
      void subtractEqual(const Complex&);        // -= for Complex objects
      Complex multiply(const Complex&)const;     // multiply 2 Complex objects
      Complex multiplyPolar(const Complex&)const;// multiply 2 Complex objects
      Complex divide(const Complex&)const;       // divide 2 Complex objects
      Complex dividePolar(const Complex&)const;  // divide 2 Complex objects
      Complex negative()const;                   // unary minus
      Complex conjugate()const;                  // finds Complex conjugate

//      CONVERSION FUNCTIONS

      double r()const;                           // finds radius
      double theta()const;                       // finds polar angle
      Complex pr(const double,const double)const;// polar to rectangular

//      UTILITY FUNCTIONS

      void print(ostream&);                      // outputs a Complex object

   private:

      double x;
      double y;
};

Complex::Complex()
{
   x=0;
   y=0;
}
Complex::Complex(double X, double Y)
{
   x=X; y=Y;                        
}
/*Complex Complex::add(const Complex&z)const
{
   return(Complex(x+z.x,y+z.y));
}
Complex Complex::multiplyPolar(const Complex&z)const
{
   return(pr(r()*z.r(),theta()+z.theta()));
}
Complex Complex::pr(const double r,const double theta)const
{
   return(Complex(r*cos(theta),r*sin(theta)));
}
*/
void Complex::print(ostream&fout)
{
   fout<<setiosflags(ios::fixed|ios::showpoint)<<setprecision(4)
  	    <<"("<<setw(6)<<x<<","<<setw(6)<<y<<")";
}

int main()
{
   ofstream fout("cmplxout.txt");

   //double r4=6,
          //theta4=-M_PI,
          //r7=10,
          //theta7=2*M_PI/3;
   Complex z0,z1(-4, 3),z2(-4,-3),z3( 0, 3),z4,z5,z6(-2.1,7.6),z7,z8,z9,z10,
           z11,z12,z13,z14,z15,z16;

   fout<<"ORIGINAL DATA\n\n"
       <<  " z0 = ";
   //z0.print(fout);.......
   
   return(0);
}
Last edited on
Thanks for the confirmation, after all it was given and it worked for most of my classmates. I'll keep plugging away.

Well, I imitated some of my classmates and instead of doing this as a project in dev c++ I just have two files, one main and one class. Everything works now.
Confusing stuff.
Last edited on
Topic archived. No new replies allowed.