By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,124 Members | 953 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,124 IT Pros & Developers. It's quick & easy.

Compling errors

P: 5
I'm working on a program, overloading operators, when I compile the program together it works, however when I separate it into the various files such as the .h and the .cpp I'm getting a lot of error messages. Can you offer any suggestions.

Here's the code:
Expand|Select|Wrap|Line Numbers
  1. //#include "complex.h"//implementation file
  2. #include <iostream>
  3. //#include<math.h>  
  4. using namespace std;
  5.  
  6. class Complex
  7. {
  8.     public:
  9.         Complex(double, double);
  10.         Complex();
  11.         Complex(double);   
  12.         friend ostream& operator <<(ostream& out, const Complex& c);
  13.         friend istream& operator >>(istream& in, Complex& c);
  14.         friend const Complex operator *(const Complex& l, const Complex& r);
  15.         friend const Complex operator -(const Complex& l, const Complex& r);
  16.         friend const Complex operator +(const Complex& l, const Complex& r);
  17.         friend bool operator ==(const Complex& l, const Complex& r);
  18.     private:
  19.          double real, imag;
  20. };
  21.  
  22. // define constructor
  23. Complex::Complex( double r, double i )
  24. {
  25.     real = r; imag = i;
  26. }
  27.  
  28. Complex::Complex()
  29. {
  30.     real = 0;
  31.     imag = 0;
  32. }
  33.  
  34. Complex::Complex(double realPart ) //constructor that takes a single parameter
  35. {
  36.     realPart = 0;
  37.     int i = 1;
  38. }
  39.  
  40. ostream& operator <<(ostream& out, const Complex& c)
  41. {
  42.     out << c.real << "+" << c.imag << "i";
  43.     return out;
  44. }
  45.  
  46. istream& operator >>(istream& in, Complex& c)
  47. {
  48.     char op;
  49.     char i;
  50.     in >> c.real >> op >> c.imag >> i; 
  51.  
  52.     return in;
  53. }
  54.  
  55. // define overloaded + (plus) operator
  56. const Complex operator +(const Complex& l, const Complex& r) 
  57. {
  58.     Complex result;
  59.     result.real = (l.real + r.real);
  60.     result.imag = (l.imag + r.imag);
  61.  
  62.     return result;
  63. }
  64.  
  65. const Complex operator -(const Complex& l, const Complex& r) 
  66. {
  67.       Complex result;
  68.       result.real = (l.real - r.real);
  69.       result.imag = (l.imag - r.imag);
  70.       return result;
  71. }
  72.  
  73. const Complex operator *(const Complex& l, const Complex& r) 
  74. {
  75.  
  76.       int a = (l.real*r.real - l.imag*r.imag);
  77.       int b = (l.imag*r.real + l.real*r.imag);
  78.       Complex result(a,b);
  79.       return result;
  80. }
  81.  
  82. bool operator ==(const Complex& l, const Complex& r)
  83. {
  84.     return ((l.real == r.real) && (l.imag == r.imag));
  85. }
  86.  
  87.  
  88.  
  89. int main()
  90.     Complex a(4,5), b(6,5);
  91.     double realPart = 0;
  92.     Complex i(0,1);
  93.     cout << "First complex number is " << a << endl;
  94.     cout << "Second complex number is " << b << endl << endl;
  95.     cout << "Testing constant i..."<<endl; 
  96.     cout << "The value of our constant i is: " << i << endl <<endl;
  97.     cout << "Enter 2 new complex numbers in the form a+bi(with no spaces):" << endl;
  98.     cin >> a >> b;
  99.     cout << endl;
  100.  
  101.  
  102.     cout << a << " + " << b <<" = "<< a + b<< endl;
  103.     cout << a << " - " << b <<" = "<< a - b<< endl;
  104.     cout << a << " * " << b <<" = "<< a * b<< endl << endl;
  105.     cout << "Testing equality operator..." << endl;
  106.     if (a == b)
  107.         cout << a << " is equal to " << b << endl;
  108.     else
  109.         cout << a << " is not equal to " << b << endl;
  110.     return 0;
  111.  
  112. }
Here are the error messages:


C:\Documents and Settings\Default\Desktop>cl /EHsc pr3d.cpp
pr3.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version
13.10.3077 for 80x86
Copyright (C) Microsoft Corporation 1984-2002. All rights
reserved.

pr3d.cpp
c1xx : fatal error C1083: Cannot open source file:
'pr3d.cpp': No such file or d
irectory
pr3.cpp
c1xx : fatal error C1083: Cannot open source file:
'pr3.cpp': No such file or di
rectory
Generating Code...

C:\Documents and Settings\Default\Desktop>cd exP-3\

C:\Documents and Settings\Default\Desktop\exP-3>cl /EHsc
pr3d.cpp pr3.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version
13.10.3077 for 80x86
Copyright (C) Microsoft Corporation 1984-2002. All rights
reserved.
Mar 14 '07 #1
Share this Question
Share on Google+
2 Replies


DeMan
100+
P: 1,806
Welcome to theScripts,

I am taking the liberty of moving this thread to the C forum, where I'm sure the experts will be more than willing to offer some advice....
Mar 14 '07 #2

Ganon11
Expert 2.5K+
P: 3,652
What is the name of the files you are saving this code in? You may be trying to open a file that doesn't exist.
Mar 14 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.