I am having so much trouble with linking files in eclipse. I keep getting undefined reference errors, even though I added each file path to the Paths and Symbols setting in Project. I also included the header files. I have 1 Application file along with 3 implementation files and 2 header files.
Here is 1 implementation file:
#include <iostream>
#include <fstream>
#include "complex2.h"
#include "complexType.h"
using namespace std;
complexDB::complexDB(int lineCount)
{
length = lineCount;
c = new complexType[lineCount];
num_complex = 0;
}
void complexDB::set_Index(int i, const complexType& cT)
{
c[i] = cT;
}
void complexDB::set_numComplex(int n)
{
num_complex = n;
}
void complexDB::insert(const complexType& insertItem)
{
if (num_complex < length)
{
int oldi = num_complex;
c[oldi] = insertItem; // inserts item at the last index of array
num_complex++;
}
}
void complexDB::deletee(const complexType& deleteItem)
{
}
void complexDB::list()
{
cout << "in list:" << endl;
cout << length << "\t" << num_complex << endl;
for (int i = 0; i < num_complex; i++)
{
cout << c[i];
}
}
void complexDB::save()
{
ofstream fout;
fout.open("126complex.txt");
if (fout.is_open())
{
cout << "is open" << endl;
}
for (int i = 0; i < num_complex; i++)
{
fout << c[i];
}
}
complexDB::~complexDB()
{
delete[] c;
}
Here is another implementation file:
#include <fstream>
#include <sstream>
#include "complexType.h"
//#include "complex2.cpp"
using namespace std;
#define CA_MAX_SIZE 10
#define ComplexFileName "126.txt"
void CreateComplexFile()
{
complexType ca[CA_MAX_SIZE];
ofstream fout;
fout.open(ComplexFileName);
for (int i = 0; i < CA_MAX_SIZE; i++)
{
ca[i].setComplex(i, i + 1);
fout << ca[i];
}
fout.close();
}
void ReadComplexFile()
{
complexType ca[CA_MAX_SIZE];
ifstream fin;
fin.open(ComplexFileName);
for (int i = CA_MAX_SIZE - 1; i >= 0; i--)
{
fin >> ca[i];
}
fin.close();
}
void ReadComplexFileEOF()
{
complexType ca[CA_MAX_SIZE];
ifstream fin;
fin.open(ComplexFileName);
int i = 0;
while (!fin.eof())
{
fin >> ca[i++];
}
fin.close();
}
void ReadComplexFileTwice()
{
complexType ca[CA_MAX_SIZE];
ifstream fin;
fin.open(ComplexFileName);
for (int i = CA_MAX_SIZE - 1; i >= 0; i--)
{
fin >> ca[i];
}
fin.clear();
fin.seekg(0, ios::beg);
int i = 0;
while (!fin.eof())
{
fin >> ca[i++];
}
fin.close();
}
void ImportComplexFile(string fname)
{
ifstream fin;
double real, im;
char plusorminus, ichar;
string oneline;
fin.open(fname.c_str());
while (!fin.eof())
{
getline(fin, oneline);
stringstream(oneline) >> real >> plusorminus >> im >> ichar;
}
fin.close();
}
void ImportComplexFile2(string fname)
{
ifstream fin;
fin.open(fname.c_str());
double real, im;
char plusorminus, ichar;
complexType c;
string oneline;
while (!fin.eof())
{
getline(fin, oneline);
real = 0;
im = 0;
plusorminus = '\0';
ichar = '\0';
stringstream(oneline) >> real >> plusorminus >> im >> ichar;
switch (plusorminus)
{
case '-':
im = -im;
break;
case 'i':
im = real;
real = 0;
break;
case '\0':
im = 0;
break;
}
c.setComplex(real, im);
cout << c << endl;
}
fin.close();
}
For both files, anything with complexType gives the error:
undefined reference to `complexType::complexType(double, double)'
Also for the main file I get this error:
cannot find -lC:\Users\Altemush\Documents\SJSU_Dev\projects_mingw\complex2\src
I suppose I'm not linking the files together correctly, een though I thought I did. Please, can anyone help me?
Here is complexType.h:
#ifndef H_complexNumber
#define H_complexNumber
#include <iostream>
using namespace std;
class complexType
{
friend ostream& operator<< (ostream&, const complexType&);
friend istream& operator>> (istream&, complexType&);
friend complexType operator+(const complexType& one,
const complexType& two);
public:
void setComplex(const double& real, const double& imag);
complexType(double real = 0, double imag = 0);
complexType& operator=(const complexType &rhs);
complexType operator-(const complexType& two);
complexType operator-(int x);
int realPart;
int imaginaryPart;
};
#endif
Here is complexType.cpp:
#include <iostream>
#include "complexType.h"
using namespace std;
ostream& operator<< (ostream& os, const complexType& complex)
{
os << "(" << complex.realPart << ", "
<< complex.imaginaryPart << ")" << endl;
return os;
}
istream& operator>> (istream& is, complexType& complex)
{
char ch;
// is >> ch;
is >> complex.realPart;
is >> ch;
is >> complex.imaginaryPart;
is >> ch;
return is;
}
complexType::complexType(double real, double imag)
{
setComplex(real, imag);
}
void complexType::setComplex(const double& real, const double& imag)
{
realPart = real;
imaginaryPart = imag;
}
complexType operator+(const complexType& one, const complexType& two)
{
complexType temp;
temp.realPart = one.realPart + two.realPart;
temp.imaginaryPart = one.imaginaryPart + two.imaginaryPart;
return temp;
}
complexType complexType::operator-( const complexType &operand2 )
{
return complexType( realPart - operand2.realPart,
imaginaryPart - operand2.imaginaryPart );
}
complexType complexType::operator-( int operand2 )
{
return complexType( realPart - operand2,
imaginaryPart - operand2 );
}
complexType& complexType::operator=(const complexType &rhs){
realPart = rhs.realPart;
imaginaryPart = rhs.imaginaryPart;
return *this;
}