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;
}
Related
I am defining my own string class called StringSet using a vector of strings. I am assigned to overload the >>, <<, ==, >, >=, +, += and * operators, and ran into a problem with <<. The output should be:
Welcome to stringset
hi everyone
"all" does not exist in the set.
hi
But it seems to be skipping the second and third lines. I am very new to overloading operators, so I am probably overlooking an obvious mistake.
header and class declaration:
#include <iostream>
#include <vector>
#include<string>
#include <iterator>
#include <algorithm>
#include <fstream>
using namespace std;
class StringSet
{
public:
//Constructor
StringSet();
//Copy Constructor
StringSet(const StringSet& s);
//Default constructors
StringSet(string initialStrings[], const int ARRAYSIZE);
//Destructor
~StringSet();
void add(const string s);
void remove(const string s);
//Returns length
int size()
{
return length;
}
// Overload the << operator so that it outputs the strings
friend ostream& operator <<(ostream& outs, const StringSet& s);
private:
//size of the vector
int length;
// Vector to store strings
vector <string> data;
};
function definitions:
ostream& operator<<(ostream& outs, const StringSet& s)
{
outs << "\n";
for (int i = 0; i < s.length; i++)
{
outs << s.data[i] << " ";
}
outs << "\n";
return outs;
}
//Add a string to the vector
void StringSet::add(const string s)
{
bool c = check(s);
if (c == false)
{
data.push_back(s);
}
else
{
cout << "\"" << s << "\" already exists in the set.";
}
}
// Remove a string from the vector
void StringSet::remove(const string s)
{
bool c = check(s);
if (c == true)
{
vector<string>::iterator position = search(s);
data.erase(position);
}
else
{
cout << "\"" << s << "\" does not exist in the set\n";
}
}
StringSet::StringSet()
{
length = 0;
}
StringSet::StringSet(string initialStrings[], const int ARRAYSIZE)
{
for (int i = 0; i < data.size(); i++)
{
initialStrings[i] = " ";
}
}
// Copy constructor
StringSet::StringSet(const StringSet& s)
{
for (int i = 0; i < data.size(); i++)
{
data[i] = s.data[i];
}
}
StringSet::StringSet()
{
length = 0;
}
StringSet::StringSet(string initialStrings[], const int ARRAYSIZE)
{
for (int i = 0; i < data.size(); i++)
{
initialStrings[i] = " ";
}
}
// Copy constructor
StringSet::StringSet(const StringSet& s)
{
for (int i = 0; i < data.size(); i++)
{
data[i] = s.data[i];
}
}
// Check if a string exists in the vector
bool StringSet::check(const string s)
{
vector<string>::iterator it = find(data.begin(), data.end(), s);
if (it != data.end())
{
return true;
}
else
{
return false;
}
}
Main function:
int main()
{
ofstream outs;
ifstream ins;
StringSet doc1, doc2, query
cout << "Welcome to stringset\n";
doc1.add("hi");
doc1.add("everyone");
outs << doc1;
doc1.remove("everyone");
doc1.remove("all");
outs << doc1;
}
If you use a variable that stores the size of the set, you should increment/decrement it when adding/removing elements. You can also change the definition of the StringSet::size():
int size() const
{
return static_cast<int>(data.size());
}
I have those classes and I want to sort an array of objects, considering x coordinate, and sort just those with a particular value to an attribute.
Class.h
#include <iostream>
#include <algorithm>
class Punct2D
{
protected:
int x, y;
public:
Punct2D() {};
~Punct2D() {};
int get_x() const;
int get_y() const;
void set_x(const int x);
void set_y(const int y);
friend std::ostream &operator << (std::ostream &flux, Punct2D dot);
friend std::istream &operator >> (std::istream &flux, Punct2D &dot);
};
class Punct2DColorat :public Punct2D
{
private:
char *color;
public:
Punct2DColorat() { this->color = NULL; };
~Punct2DColorat() {};
char *get_color();
void set_color(char *color);
bool operator<(Punct2DColorat dot);
};
Here I have the implementation.
#include "Class.h"
int Punct2D::get_x() const
{
return this->x;
}
int Punct2D::get_y() const
{
return this->y;
}
void Punct2D::set_x(const int x)
{
this->x = x;
}
void Punct2D::set_y(const int y)
{
this->y = y;
}
char *Punct2DColorat::get_color()
{
return this->color;
}
void Punct2DColorat::set_color(char *color)
{
this->color = new char[strlen(color) + 1];
for (int i = 0; i < strlen(color) + 1; i++) this->color[i] = color[i];
}
bool Punct2DColorat::operator<(Punct2DColorat dot)
{
return this->x < dot.get_x();
}
std::ostream &operator << (std::ostream &flux, Punct2D dot)
{
flux << "Punct(" << dot.get_x() << "," << dot.get_y() << ")\n";
return flux;
}
std::istream &operator >> (std::istream &flux, Punct2D &dot)
{
std::cout << "Introduceti x :";
flux >> dot.x;
std::cout << "Introduceti y :";
flux >> dot.y;
return flux;
}
And here is the Main.
#include "Class.h"
void main()
{
int n, it = 0; char *aux = new char[15]; bool value;
Punct2DColorat *dots;
std::cout << "Cate puncte introduceti :"; std::cin >> n;
dots = new Punct2DColorat[n];
for (int i = 0; i < n; i++)
{
std::cout << "Introduceti 0 pentru Punct2D, respectiv 1 pentru Punct2D colorat :";
std::cin >> value;
if (value)
{
std::cin >> dots[i];
std::cout << "Introduceti culoarea punctului :";
std::cin >> aux;
dots[i].set_color(aux);
}
else
{
std::cin >> dots[i];
}
}
std::sort(dots, dots + n, [](Punct2DColorat dot) { return dot.get_color() != NULL; });
for (int i = 0; i < n; i++)
{
std::cout << dots[i];
if (dots[i].get_color() != NULL)
{
std::cout << "Culoare :" << dots[i].get_color() << "\n";
}
std::cout << "\n";
}
}
I want to sort the dots with color !=NULL, I tried this, it works but I have a runtime error.
bool Punct2DColorat::operator<(Punct2DColorat dot)
{
if ((this->color != NULL) && (dot.get_color() != NULL))return this->x < dot.get_x();
return true;
}
How can I sort just the objects with color !=NULL and the other objects with color==NULL remain in the same position?
Here is an example:
//If have 3 objects in the following order stored in the dots array.
dots[0].get_x()=3;
dots[0].get_y()=3;
dots[0].get_color()="Red";
dots[1].get_x()=0;
dots[1].get_y()=0;
dots[1].get_color()=NULL;
dots[2].get_x()=1;
dots[2].get_y()=1;
dots[2].get_color()="Blue";
//After sort i want to have them like this:
dots[0].get_x()=1;
dots[0].get_y()=1;
dots[0].get_color()="Blue";
dots[1].get_x()=0;
dots[1].get_y()=0;
dots[1].get_color()=NULL;
dots[2].get_x()=3;
dots[2].get_y()=3;
dots[2].get_color()="Red";
Thanks.
The problem is, your comparison operator evaluates to true for any couple of non-colored points.
A possible solution is to construct a second vector, sort it and re-insert
std::vector<Punct2DColorat> tmp;
for (int i = 0; i < n; i++)
{
if (dots[i].get_color() != NULL)
{
tmp.push_back(dots[i]);
}
}
std::sort(tmp.begin(), tmp.end());
int j = 0;
for (int i = 0; i < n; i++)
{
if (dots[i].get_color() != NULL)
{
dots[i] = tmp[j];
++j;
}
}
I have problem with dynamic allocation in c++.
This is my code:
#include "stdafx.h"
#include <iostream>
using namespace std;
class Wektor {
int rozmiar;
float *TabWe;
friend std::istream& operator >> (std::istream &Strm, Wektor &Wek);
public:
Wektor(int rozmiar) : rozmiar(rozmiar) {
TabWe = new float[rozmiar];
}
Wektor() {
for (int i = 0; i < rozmiar; i++)
{
TabWe[i] = 0;
}
}
~Wektor()
{
for (int i = 0; i <rozmiar; i++)
{
delete[] TabWe;
}
}
};
istream& operator >>(istream &Strm, Wektor &Wek)
{
cout << "Size: ";
Strm >> Wek.rozmiar;
for (int i = 0; i < Wek.rozmiar; i++)
{
Strm >> Wek.TabWe[i];
}
return Strm;
}
int main()
{
Wektor wek;
cin >> wek;
}
After I enter first value to the matrix I get this error:
I think there is problem with default constructor, because you can see on the screenshot that this matrix has no value when program starts. What is wrong with it?
I encountered the following problem with this code.
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iomanip>
//////////////////////////////////////////////////////////
//////////////////////////CORE CLASS//////////////////////
//////////////////////////////////////////////////////////
class Core {
public:
Core() : midterm(0), final(0) { }
Core(std::istream& is) { read(is); }
std::string name() const;
virtual std::istream& read(std::istream&);
virtual double grade() const;
protected:
std::istream& read_common(std::istream&);
std::istream& read_hw(std::istream&, std::vector<double>&);
double midterm, final;
std::vector<double> homework;
private:
std::string n;
};
std::string Core::name() const { return n; }
double Core::grade() const
{
return calc_grade(midterm, final, homework);
}
std::istream& Core::read_common(std::istream& in)
{
in >> n >> midterm >> final;
return in;
}
std::istream& Core::read(std::istream& in)
{
read_common(in);
read_hw(in, homework);
return in;
}
std::istream& Core::read_hw(std::istream& in, std::vector<double>& homework)
{
double input;
while (in >> input)
homework.push_back(input);
return in;
}
//////////////////////////////////////////////////////////
//////////////////////////GRAD CLASS//////////////////////
//////////////////////////////////////////////////////////
class Grad : public Core {
public:
Grad() : thesis(0) { }
Grad(std::istream& is) { read(is); }
double grade() const;
std::istream& read(std::istream&);
private:
double thesis;
};
std::istream& Grad::read(std::istream& in)
{
read_common(in);
in >> thesis;
read_hw(in, homework);
return in;
}
double Grad::grade() const
{
return std::min(Core::grade(), thesis);
}
//////////////////////////////////////////////////////////
//////////////////////MISCELLANEOUS///////////////////////
//////////////////////////////////////////////////////////
bool compare_grades(const Core& c1, const Core& c2)
{
return c1.grade() < c2.grade();
}
bool compare_Core_ptrs(const Core* cp1, const Core* cp2)
{
return compare_grades(*cp1, *cp2);
}
double calc_grade(double midterm, double final, std::vector<double>& homework)
{
double result = 0.0;
for (std::vector<double>::const_iterator i = homework.begin(); i != homework.end(); ++i)
result += *i;
return midterm*0.4 + final*0.4 + (result / homework.size())*0.2;
}
using namespace std;
int main()
{
vector<Core*> students;
Core* record;
char ch;
string::size_type maxlen = 0;
while (cin >> ch)
{
if (ch == 'U')
record = new Core;
else
record = new Grad;
record->read(cin);
maxlen = max(maxlen, record->name().size());
students.push_back(record);
}
sort(students.begin(), students.end(), compare_Core_ptrs);
for (vector<Core*>::size_type i = 0; i != students.size(); ++i)
{
cout << students[i]->name() << string(maxlen + 1 - students[i]->name().size(), ' ');
try
{
double final_grade = students[i]->grade();
streamsize prec = cout.precision();
cout << setprecision(3) << final_grade << setprecision(prec) << endl;
}
catch (domain_error e)
{
cout << e.what() << endl;
}
delete students[i];
}
return 0;
}
error C3861: 'calc_grade': identifier not found. I tried to move the function before the classes but nothing changed. I don't understand what is wrong. Help.
You need to move calc_grade above grade so that it knows it exists. Or declare it above like this.
double calc_grade(double midterm, double final, std::vector<double>& homework);
Your grade method is marked const, so it may not change your class. But you are calling calc_grade which does not take a const vector therefor it could change the class member you are handing it. You can make it take a const vector to solve this. I would make that method const as well to keep things consistent.
One possible fix:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iomanip>
//////////////////////////////////////////////////////////
//////////////////////////CORE CLASS//////////////////////
//////////////////////////////////////////////////////////
class Core {
public:
Core() : midterm(0), final(0) { }
Core(std::istream& is) { read(is); }
std::string name() const;
virtual std::istream& read(std::istream&);
virtual double grade() const;
protected:
std::istream& read_common(std::istream&);
std::istream& read_hw(std::istream&, std::vector<double>&);
double midterm, final;
std::vector<double> homework;
private:
std::string n;
};
std::string Core::name() const { return n; }
double calc_grade(double midterm, double final, const std::vector<double>& homework)
{
double result = 0.0;
for (std::vector<double>::const_iterator i = homework.begin(); i != homework.end(); ++i)
result += *i;
return midterm*0.4 + final*0.4 + (result / homework.size())*0.2;
}
double Core::grade() const
{
return calc_grade(midterm, final, homework);
}
std::istream& Core::read_common(std::istream& in)
{
in >> n >> midterm >> final;
return in;
}
std::istream& Core::read(std::istream& in)
{
read_common(in);
read_hw(in, homework);
return in;
}
std::istream& Core::read_hw(std::istream& in, std::vector<double>& homework)
{
double input;
while (in >> input)
homework.push_back(input);
return in;
}
//////////////////////////////////////////////////////////
//////////////////////////GRAD CLASS//////////////////////
//////////////////////////////////////////////////////////
class Grad : public Core {
public:
Grad() : thesis(0) { }
Grad(std::istream& is) { read(is); }
double grade() const;
std::istream& read(std::istream&);
private:
double thesis;
};
std::istream& Grad::read(std::istream& in)
{
read_common(in);
in >> thesis;
read_hw(in, homework);
return in;
}
double Grad::grade() const
{
return std::min(Core::grade(), thesis);
}
//////////////////////////////////////////////////////////
//////////////////////MISCELLANEOUS///////////////////////
//////////////////////////////////////////////////////////
bool compare_grades(const Core& c1, const Core& c2)
{
return c1.grade() < c2.grade();
}
bool compare_Core_ptrs(const Core* cp1, const Core* cp2)
{
return compare_grades(*cp1, *cp2);
}
using namespace std;
int main()
{
vector<Core*> students;
Core* record;
char ch;
string::size_type maxlen = 0;
while (cin >> ch)
{
if (ch == 'U')
record = new Core;
else
record = new Grad;
record->read(cin);
maxlen = max(maxlen, record->name().size());
students.push_back(record);
}
sort(students.begin(), students.end(), compare_Core_ptrs);
for (vector<Core*>::size_type i = 0; i != students.size(); ++i)
{
cout << students[i]->name() << string(maxlen + 1 - students[i]->name().size(), ' ');
try
{
double final_grade = students[i]->grade();
streamsize prec = cout.precision();
cout << setprecision(3) << final_grade << setprecision(prec) << endl;
}
catch (domain_error e)
{
cout << e.what() << endl;
}
delete students[i];
}
return 0;
}
I have a function Readf I'm trying to call to fill in an array inside each constructor, I tried only with the default constructor with a code statement ReadF(cap,*point,counter);.
The second argument is what's giving me a problem in figuring out, I would get an error with '&' beside it or ' '. And I get an external error with the '*', I'm still new to c++ so I'm not experienced in dealing with classes.
I have to use the ReadF member function, are there other ways instead of calling it to get the results I need.
Also some of the code for the functions I have might not be perfect, but I would like to deal with this problem first before I move on to another.
array.h:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class AR {
public:
AR();
AR(int);
AR(const AR&);
//~AR();
void ReadF(const int, string&, int);
AR& operator=(const AR&);
friend ostream& operator<<(ostream&, AR&);
friend ifstream & operator>>(ifstream&, AR&);
private:
string* point;
int counter;
int cap;
};
array.cpp:
#include "array.h"
AR::AR() {
counter = 0;
cap = 2;
point = new string[cap];
}
AR::AR(int no_of_cells) {
counter = 0;
cap = no_of_cells;
point = new string[cap];
}
AR::AR(const AR& Original) {
counter = Original.counter;
cap = Original.cap;
point = new string[cap];
for(int i=0; i<counter; i++) {
point[i] =Original.point[i];
}
}
// AR::~AR() {
// delete [ ]point;
//}
ostream& operator<<(ostream& out, AR& Original) {
cout << "operator<< has been invoked\n";
for (int i=0; i< Original.counter; i++) {
out << "point[" << i <<"] = " << Original.point[i] << endl;
}
return out;
}
AR& AR::operator=(const AR& rhs) {
cout << "operator= invoked\n";
if (this == &rhs)
return *this;
point = rhs.point;
return *this;
}
void ReadF(const int neocap, string& neopoint, int neocounter) {
ifstream in;
in.open("sample_strings.txt"); //ifstream in; in.open("sample_data.txt");
if (in.fail())
cout<<"sample_data not opened correctly"<<endl;
while(!in.eof() && neocounter < neocap) {
in >> neopoint[neocounter];
neocounter++;
}
in.close();
}
ifstream& operator>>(ifstream& in, AR& Original) {
Original.counter = 0;
while(!in.eof() && Original.counter < Original.cap) {
in >> Original.point[Original.counter];
(Original.counter)++;
}
return in;
}
It appears you are missing AR:: in the ReadF definition.
On second thought you are reading into an index of neopoint that likely isn't allocated yet. You could change ReadF to read:
void ReadF(const int neocap, string& neopoint,int neocounter)
{
ifstream in;
in.open("sample_strings.txt"); //ifstream in; in.open("sample_data.txt");
if (in.fail())
{
cout<<"sample_data not opened correctly"<<endl;
return;
}
neopoint.resize(neocap);
while(neocounter < neocap && in >> neopoint[neocounter])
{
neocounter++;
}
in.close();
}
Although you probably want to look into stringstream
std::ifstream in("sample_strings.txt");
if (in)
{
std::stringstream buffer;
buffer << in.rdbuf();
in.close();
inneopoint= buffer.str();
}