Help with " [Linker error] undefined reference to `complx::complx(double, double)' "

Getting this error, don't know how to fix it. Below are the 3 files I included in the project.
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
// Complx.cpp
#include "complx.h"
complx::~complx() {
complx complx::operator+ (const complx& c) const
}
ostream &operator<< ( ostream &out_file, complx &number ){
        out_file<<number.real<<" i"<<number.imag<<endl;
        return out_file
}
 
 
extern ifstream &operator >> ( ifstream &in_file, complx &number ){
     double re, is;
     char ch;
     if (in_file >> ch && ch == '('&& in_file >> re >> ch && ch == ','
          && in_file >> is >> ch && ch == ')')
              number.Set(re,is);
     else cerr << "Finish the input"<<endl;
     return in_file;
}
 
extern istream &operator >> ( istream &in_file, complx &number ){
       in_file>> &number.real >> &number.imag;
       return in_file;
}
 
// define constructor
complx::complx( double r, double i ){
      real = r; imag = i;
}
 
int operator== (const complx& a, const complx& b){
    if( a.real == b.real && a.imag==b.imag)
    return 1;
    else
    return 0;
}

__________________________________________________________________
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
#include <iostream>
#include <fstream>
using namespace std;
class complx
{
      double real,
             imag;
public:
      complx( double real = 0., double imag = 0.); // constructor
      ~complx();                      // destrucutor
      friend int operator== (const complx&, const complx&);
 
       //Sets private data members.
 
     void Set(double new_real, double new_imaginary) {
     real = new_real;
     imag = new_imaginary;
     }
 
     //Returns the real part of the complex number.
          double Real() {
          return real;
     }
 
     //Returns the imaginary part of the complex number.
     double imaginary() {
     return imag;
     }
};
 
extern ifstream &operator >> ( ifstream &in_file, complx &number );
extern istream &operator >> ( istream &in_file, complx &number );
extern ostream &operator << ( ostream &out_file, complx &number );

_____________________________________________________________________
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//call_complx.cpp
#include "complx.h"
int main()
{
        int i=0;
          complx c1, c2(3,5),in[5];
          cout<< "Please input a complex number in the format of (a,b) - " << endl;
         while ((cin  >> in[i])&& (i<4)){
              cout << in[i] << endl;
              i++;
        }      
        if (c1 == in[0] )        
          cout << c1 << " equals to " << in[0] << endl;
        else
           cout << c1 << " not equal to " << in[0]<< endl;
 
        if (in[1] == in[2] )          
          cout << in[1] << " equals to " << in[2] << endl;
        else
           cout << in[1] << " not equal to " << in[2]<< endl;        
       
        char c; cin >> c;
          return 0;   //successful termination
}
Last edited on
Are you sure you are compiling and linking both cpp files?
OK the linker error is fixed but i'm getting an error now like "8 C:\Dev-Cpp\Exercise 4\complx.h `double complx::real' is private "
Why do you have the following line (complx.cpp line 4) inside the implementation of your destructor?
 
complx complx::operator+ (const complx& c) const


It looks to be an incomplete function definition in the wrong place. That should be causing compile errors. I can't see that causing the linker error you're getting, but I've seen stranger things happen.
You need to make your operator>> and operator<< functions friend functions in your complx class.
Line 7 of complx.cpp
out_file<<number.real<<" i"<<number.imag<<endl;
You are trying to access private data. Either use accessors: out_file << number.Real() << " i" << number.imaginary() << endl; or declare operator<< as friend.
Last edited on
Topic archived. No new replies allowed.