0

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;
}
alti21
  • 93
  • 7
  • What is `complexType` and `ComplexType.h`? It seems to be a custom library. Have you also added `ComplexType.cpp` to your project and compiled it with your code? –  Sep 15 '16 at 18:37
  • complexType.h is the header file for complexType.cpp. Yes I have added complexType.cpp to the project and yes I compiled it with the code. – alti21 Sep 15 '16 at 19:33
  • Just to be more clear: the first implementation file I gave is complex2.cpp and the other one is complexFile.cpp – alti21 Sep 15 '16 at 19:35
  • @AltemushBhatti Both of posted files `complex2.cpp`, `complexFile.cpp` contains nothing about implementation of `complexType`. You should post `complexType.h` to give more context about this problem. – Nikita Sep 15 '16 at 20:19
  • I added complexType.h above – alti21 Sep 15 '16 at 20:33
  • If you have all headers and implementation files included in your eclipse project, the default settings of the linker should be sufficient, you should not have to change anything. Try a fresh project and just add all the files to it and then try to build it. –  Sep 15 '16 at 21:17
  • Off topic: `while (!fin.eof())` is a common error. [Read more here.](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – user4581301 Sep 15 '16 at 21:17
  • Off topic: `complexDB` looks like it probably violates [the Rule of Three](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three). Can't be sure without seeing the header, though. – user4581301 Sep 15 '16 at 21:19
  • On topic: I don't see a definition of any of `complexType`s methods in the provided code. If a method is not implemented, it cannot be found. Please edit your question and provide a [MCVE] to fill in the blanks. This question currently cannot be answered without guesswork. – user4581301 Sep 15 '16 at 21:25
  • @user4581301 I added complexType.cpp above – alti21 Sep 15 '16 at 21:36
  • @Eichhörnchen I have my headers included, but how do I include the implementation files? Also, how do I add the files to a new project? I have attempted that before, but I got an error saying "Launch binary not found". – alti21 Sep 15 '16 at 21:39
  • Eclipse will detect new files added to the source folder with a right-click on the source folder in the Project Explorer pane and selecting refresh off the pop-up menu. It will automatically compile .cpp file unless told not to. If the cpp file is grey in the Project Explorer, Eclipse thinks it is not supposed to compile the file . Right click the file and select Resource Configurations->Exclude from build and remove the checkmarks for the targets you want to build and link the file. If the cpp files are in another folder, create a library target for that other project, and link the library. – user4581301 Sep 15 '16 at 22:17
  • Thanks for the info. I did the refresh thing you told me about, and then I copied and pasted the implementation files into the other implementation file source folders, but now I'm getting errors that the compiler cannot find the source files. – alti21 Sep 16 '16 at 01:46
  • Also when I try to run it, I get an error saying "Launch failed. Binary not found." – alti21 Sep 16 '16 at 01:53

0 Answers0