You are on page 1of 7

i 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 first defined here multiple definition first defined here multiple definition first defined here multiple definition first defined here of `Complex::Complex()' of `Complex::Complex()' of `Complex::Complex(double, double)' of `Complex::Complex(double, double)'

\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

class Complex { public: // CONSTRUCTORS Complex(); Complex(const double,const double); // ARITHMETIC OPERATIONS Complex add(const Complex&)const; // void addEqual(const Complex&); // Complex subtract(const Complex&)const; // void subtractEqual(const Complex&); // Complex multiply(const Complex&)const; // Complex multiplyPolar(const Complex&)const;// Complex divide(const Complex&)const; // Complex dividePolar(const Complex&)const; // Complex negative()const; // Complex conjugate()const; // // 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 add 2 Complex objects += for Complex objects subtract 2 Complex objects -= for Complex objects multiply 2 Complex objects multiply 2 Complex objects divide 2 Complex objects divide 2 Complex objects unary minus finds Complex conjugate // default constructor // data constructor

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

//void print(ostream&); private: double x; double y; };

// outputs a Complex object

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

#include #include #include #include

<iostream> <iomanip> <fstream> <cmath>

using namespace std; #include "complex.cpp" int main() { ofstream fout("cmplxout.txt"); //double r4=6, //theta4=-M_PI, //r7=10,

18 19 20 21 22 23 24 25 26 27 }

//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:/DevCpp/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 Jul 11, 2008 at 3:20am

Boman (18)

Jul 11, 2008 at 3:50am

Solved those two issues for now. May be back with more soon.
Last edited on Jul 11, 2008 at 3:50am Boman (18) Jul 11, 2008 at 3:55am

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 Jul 11, 2008 at 3:58am Mitsakos (343) Jul 11, 2008 at 4:13am

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

#include #include #include #include

<iostream> <iomanip> <fstream> <cmath>

using namespace std; class Complex { public:

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

//

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

//

ARITHMETIC OPERATIONS Complex add(const Complex&)const; // void addEqual(const Complex&); // Complex subtract(const Complex&)const; // void subtractEqual(const Complex&); // Complex multiply(const Complex&)const; // Complex multiplyPolar(const Complex&)const;// Complex divide(const Complex&)const; // Complex dividePolar(const Complex&)const; // Complex negative()const; // Complex conjugate()const; // add 2 Complex objects += for Complex objects subtract 2 Complex objects -= for Complex objects multiply 2 Complex objects multiply 2 Complex objects divide 2 Complex objects divide 2 Complex objects unary minus 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&); private: double x; double y; // outputs a Complex object

}; 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)));

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

} */ 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 Jul 11, 2008 at 4:14am

Boman (18)

Jul 11, 2008 at 4:25am

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 Jul 11, 2008 at 4:32am

Topic archived. No new replies allowed. C++

Information Documentation Reference Articles Forum

Forum

Beginners Windows Programming UNIX/Linux Programming General C++ Programming Lounge Jobs

Home page | Privacy policy | old layout cplusplus.com, 2000-2012 - All rights reserved - v3.0 Spotted an error? contact us

You might also like