我在eclipse中链接文件时遇到了很多麻烦。即使我将每个文件路径添加到Project中的Paths and Symbols设置,我仍然会收到未定义的引用错误。我还包括头文件。我有1个应用程序文件以及3个实现文件和2个头文件。
这是一个实现文件:
#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;
}
这是另一个实现文件:
#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();
}
对于这两个文件,任何带有complexType的内容都会出错:
对complexType :: complexType(double,double)&#39;的未定义引用
同样对于主文件,我收到此错误:
找不到-lC:\ Users \ Altemush \ Documents \ SJSU_Dev \ projects_mingw \ complex2 \ src
我想我没有正确地将文件链接在一起,虽然我以为我做过了。拜托,任何人都可以帮助我吗?
这是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
这是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;
}