Using Overloading Arithmetic Assignment Operators [duplicate] - c++

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Undefined Reference to [duplicate]
(6 answers)
Closed 20 days ago.
I'm creating a program that will use overloading arithmetic assignment operators to do math.
When I go to run my program it says "undefined reference to 'complexNumber::toStringabi:cxx11 const"
I was wondering if anyone could tell me how to fix this.
Header file:
#ifndef COMPLEX_NUMBER_H
#define COMPLEX_NUMBER_H
#include <iostream>
class ComplexNumber
{
private:
double real;
double imaginary;
public:
ComplexNumber(double real = 0, double imaginary = 0);
double getReal() const;
double getImaginary() const;
ComplexNumber &operator+=(const ComplexNumber &other);
std::string toString() const;
};
#endif
cpp file:
#include "ComplexNumber.h"
#include <iostream>
#include <cmath>
ComplexNumber &ComplexNumber::operator+=(const ComplexNumber &other)
{
real += other.real;
imaginary += other.imaginary;
return *this;
}
test file:
#include <iostream>
#include <iomanip>
#include <vector>
#include "ComplexNumber.h"
using namespace std;
int main()
{
z1 += ComplexNumber(-1, 3);
cout << z1.toString() << endl;
return 0;
}

Related

keep getting the error ' no match for 'operator>>' ;

I have seen numerous other questions with the same error but those are from people who haven't overloaded the >> operator. I am having this issue with a few other programs I have written all very similar to each other as they are practice questions.
I have also looked into my textbook and compared my program with their example programs and can't see where I'm going wrong.
The full error is (in main.cpp)
line 17: error: no match for 'operator>>' (operand types are 'std::ifstream {aka std::basic_ifstream}' and 'Suitcase()')|
Any and all advice is greatly appreciated.
My Header is
//suitcase.h
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#ifndef SUITCASE_H
#define SUITCASE_H
using namespace std;
class Suitcase
{
public:
Suitcase();
Suitcase(double p,double w,double v,string s,string b);
~Suitcase();
double calc_ratio();
friend bool operator>(const Suitcase s1,const Suitcase s2);
friend ostream& operator<<(ostream & out,const Suitcase& s);
friend istream& operator >>(istream& in,Suitcase& s);
private:
double price,weight,volume;
string brand,shop;
};
#endif // SUITCASE_H
My implementation is
//suitcaseimplement.cpp
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include "suitcase.h"
using namespace std;
Suitcase::Suitcase()
{
price=0;
weight=0;
volume=0;
shop="";
brand="";
}
Suitcase::Suitcase(double p,double w,double v,string s,string b)
{
price=p;
weight=w;
volume=v;
shop=s;
brand=b;
}
Suitcase::~Suitcase()
{
}
double Suitcase::calc_ratio()
{
return (volume/weight);
}
bool operator>(Suitcase s1,Suitcase s2)
{
if(s1.calc_ratio()>s2.calc_ratio())
return true;
}
ostream& operator<<(ostream & out,const Suitcase& s)
{
out<<"The suitcase with the highest ratio is the "<<s.brand<<endl;
out<<"It is available at "<<s.shop<<endl;
out<<"It weighs "<<s.weight<<"kgs, has a volume of "<<s.volume<<"and costs R"<<s.price<<endl;
return out;
}
istream& operator >>(istream& in,Suitcase& s)
{
in>>s.price>>s.weight>>s.volume>>s.brand>>s.shop;
return in;
}
and finally my main program is.
//main.cpp
#include <iostream>
#include <string>
#include <cstdlib>
#include <fstream>
#include "suitcase.h"
using namespace std;
int main()
{
ifstream infile;
infile.open("Suitcase.txt");
if(infile.fail())
exit(1);
Suitcase current_suitcase(), best_suitcase();
infile>>best_suitcase;
while(infile>>current_suitcase)
{
if(current_suitcase>best_suitcase)
{
current_suitcase=best_suitcase;
}
}
infile.close();
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout<<best_suitcase;
return 0;
}
This line
Suitcase current_suitcase(), best_suitcase();
declares two functions that return Suitcase instead of defining two variables. This is called the Most vexing parse rule. Removing the superfluous parentheses solves the problem:
Suitcase current_suitcase, best_suitcase;
Also, don't do using namespace std.
In C++ when you have to initialize object without parameters, you must not put the () because it could be parsed as an object definition with an empty initializer or a function declaration the language standard specifies that the ambiguity is always resolved in favour of the function declaration. so instead of
Suitcase current_suitcase(), best_suitcase();
use
Suitcase current_suitcase, best_suitcase;
Note: empty initializer can be called with curly brackets in this way Suitcase current_suitcase{};
Also, just in case:
note that bool operator>(const Suitcase s1,const Suitcase s2); the const qualifier on the params is applied to an object, and those objects are been created by the copy constructor, so the const qualifier is useless in this case, but instead of taken out the const to the params, change those params to alias of objects, so declare the operator in this way bool operator>(const Suitcase& s1,const Suitcase& s2);
if you want to explicit an empty destructor use this sintax Suitcase::~Suitcase() = default; but take in account that C++ has already a default destructor that will do the same thing of an empty destructor like your
it's not a good trend using using namespace std;: instead just declare the function you want to use of that namespace, like so using std::cout; using std::cin;

error: no matching function for call to 'std::reference_wrapper<Medium>::reference_wrapper()

Book and Article are derived classes from Medium.
Why am I getting this error when trying to insert Medium / Book / Article in the bibliography?
error: no matching function for call to '**std::reference_wrapper<Medium>::reference_wrapper()**
main.cc
#include <iostream>
using namespace std;
#include "Bibliography.h"
#include "Medium.h"
#include "Book.h"
#include "Article.h"
int main()
{
Bibliography p(1);
Medium m1("PN","I","Pasol nah",2017);
p.insert(m1);
cout << p;
return 0;
}
Bibliography.h
#ifndef BIBLIOGRAPHY_H_
#define BIBLIOGRAPHY_H_
#include "Medium.h"
#include "Article.h"
#include "Book.h"
#include <iostream>
#include <functional>
#include <vector>
class Bibliography
{
private:
int m_Size;
std::vector<std::reference_wrapper<Medium>> v;
int index;
public:
Bibliography(int size);
void insert(Medium m);
friend std::ostream& operator<<(std::ostream& out, const Bibliography &b1);
};
#endif
Bibliography.cc
#include "Bibliography.h"
Bibliography::Bibliography(int size)
{
std::cout << "Bibliography created \n";
m_Size = size;
v.resize(m_Size);
index = 0;
}
void Bibliography::insert(Medium m)
{
v.push_back(m);
}
std::ostream& operator<<(std::ostream& out, const Bibliography &b1)
{
for (Medium &Medium : b1.v)
{
out << Medium.toString() << std::endl;
}
return out;
}
You should not use reference_wrapper in vector, because vector is restricted when using it with classes that do not have a default constructor. reference_wrapper doesn't have it, look at these constructors of reference_wrapper:
// initialization (1)
reference_wrapper (type& ref) noexcept;
reference_wrapper (type&&) = delete;
// copy (2)
reference_wrapper (const reference_wrapper& x) noexcept;
In this line
v.resize(m_Size);
you want to create m_Size reference_wrapperobjects, but the default constructor for reference_wrapper doesn't exist, and code cannot be compiled.
You can use reference_wrapper with vector but you will get a compilation error
when a method of vector is called, that needs the default constructor to be defined.

Implement a non-member, overloaded operator, in a namespace, with correct syntax

Disclaimer: I'm new to programming in c++, I've read through dozens of forums, and can't find an answer to my specific question.
I've included the header and definition files for a Point class below and a main function that calls an overloaded ostream to print the Point. I can't find the correct syntax for the definition for the non member overloaded operator<<. If I have it the way it's posted, I get error message:
undefined reference to `Clustering::operator<<(std::ostream&, Clustering::Point const&)
If I add Clustering:: before the operator<< I get error message:
std::ostream& Clustering::operator<<(std::ostream&, const Clustering::Point&)' should have been declared inside 'Clustering'
std::ostream &Clustering::operator<<(std::ostream &os, const ::Clustering::Point &point)
How is this code supposed to be written?
Point.h file:
#ifndef CLUSTERING_POINT_H
#define CLUSTERING_POINT_H
#include <iostream>
namespace Clustering {
class Point {
int m_dim; // number of dimensions of the point
double *m_values; // values of the point's dimensions
public:
Point(int);
friend std::ostream &operator<<(std::ostream &, const Point &);
};
}
#endif //CLUSTERING_POINT_H
Point.cpp file:
#include "Point.h"
//Constructor
Clustering::Point::Point(int i)
{
m_dim = i;
m_values[i] = {0};
}
std::ostream &operator<<(std::ostream &os, const Clustering::Point &point) {
os << "Test print";
return os;
}
Main.cpp:
#include <iostream>
#include "Point.h"
using namespace std;
using namespace Clustering;`
int main() {
Point p1(5);
cout << p1;
return 0;
}
You need to put your std::ostream &operator<< in Clustering namespace in Point.cpp
namespace Clustering {
std::ostream &operator<<(std::ostream &os, const Clustering::Point &point)
{
// ...
}
}

error undefined reference to .. c++? [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 9 years ago.
so I have a class x that is being used by class y and it's also going to be used by other classes.
.h for class x
#pragma once
#include <string>
#ifndef X_H
#define X_H
class x
{
public:
x();
const std::string & getName() const;
int getQuantity();
private:
std::string name;
int quantity;
};
#endif
.cpp for x
#include <string>
#include "x.h"
using namespace std;
x::x()
: name(),quantity(0)
{
}
const string & x::getName() const
{
return name;
}
const string & x::getQuantity() const
{
return quantity;
}
this is the .h for class y
#pragma once
#include <string>
#include <array>
#include "x.h"
class y
{
public:
static const size_t number = 20;
y();
float getTotal();
private:
std::array<X*, number> arrayList;
};
and this is the .cpp for class y
#include "y.h"
#include "x.h"
#include <array>
#include <string>
#include <iostream>
using namespace std;
y::y()
: arrayList()
{
}
float y::getTotal()
{
float total=0.0;
for(int i=0; i< number; i++)
{
if(arrayList[i] != nullptr)
{
total += arrayList[i]->getQuantity();
}
}
}
methods in the y class uses an array of pointers to method y and I'm trying to use some methods from class x using the array members but I get an error saying:
undefined reference to `x::x(...)
I think it has something to do with the preprocessors or the headers.
This means that you forgot to define the default constructor X::X() or some other constructor with parameters ( what does x::x(...) mean?) of class X. You only declared it in the class definition.
Or the other reason is that the module with the constructor definition was not included in the project build.
In class x you have explicitly declared the default constructor x() but you have not defined it. If you want to use the default constructor, remove its definition or define it with x::x():name(std::string()),quantity(0){}

"undefined reference to" while i am trying to inherit vector class [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 9 years ago.
Main.cpp
#include <iostream>
#include "include/Numbers.h"
#include <vector>
#include <string>
#include <fstream>
using namespace std;
int main()
{
ofstream myofile;
ifstream myifile;
myofile.open("output.txt");
myifile.open("input.txt");
int number;
Numbers input;
if(myifile.is_open())
while(myifile >> number) {
input.push_back(number);
}
cout << input.size() << endl;
myofile.close();
myifile.close();
cout << "Hello world!" << endl;
return 0;
}
Numbers.h
#ifndef NUMBERS_H
#define NUMBERS_H
#include <vector>
class Numbers: public std::vector<int>
{
public:
Numbers();
~Numbers();
int size();
Numbers prob();
protected:
private:
};
#endif // NUMBERS_H
Numbers.cpp
#include "../include/Numbers.h"
#include <iostream>
using namespace std;
Numbers::Numbers()
{
}
Numbers::~Numbers()
{
//dtor
}
I am trying to create a new Numbers class which inherits functions from vector class.
The error I am getting is undefined reference to 'Numbers::size()' although the push_back function didn't give any problem
I am using codeblocks to write my code, and I have included all files in the build properties
First, what you are doing there is not a good idea. It is generally not intended to use STL-classes as base-classes (except for those especially designed for this, such as std::unary_function). std::vector does not have any virtual methods, so it does not have any value as a public base-class. Making things worse, std::vector does not even have a virtual destructor, so when you use it polymorphically, you will probably create a memory leak:
void deleteVector(std::vector<int>* v)
{
delete v;
}
deleteVector( new Numbers() );
In your case, you declared a Method Numbers::size which is not defined in the source-file. You could use a using declaration to use the size-method of your base-class, but that is not needed for public methods.
class Numbers: private std::vector<int>
{
public:
using std::vector<int>::size;
};