Overloading << operator to print vector contents - c++

I've looked up information for overloading the << operator, and it seems like I did everything correctly, but I keep getting a compile error. I've friended this function in my header file, and placed a prototype at the top of my cpp file.
My University.h:
#ifndef UNIVERSITY_H
#define UNIVERSITY_H
#include <string>
#include <vector>
#include <iostream>
using namespace std;
#include "Department.h"
#include "Student.h"
#include "Course.h"
#include "Faculty.h"
#include "Person.h"
class University
{
friend ostream& operator<< (ostream& os, const vector<Department>& D);
friend ostream& operator<< (ostream& os, const Department& department);
protected:
vector<Department> Departments;
vector<Student> Students;
vector<Course> Courses;
vector<Faculty> Faculties;
static bool failure;
static bool success;
public:
bool CreateNewDepartment(string dName, string dLocation, long dChairID);
bool ValidFaculty(long dChairID);
};
#endif
My University.cpp:
#ifndef UNIVERSITY_CPP
#define UNIVERSITY_CPP
#include<string>
#include<vector>
#include<iostream>
using namespace std;
#include "University.h"
ostream& operator<<(ostream& os, const vector<Department>& D);
ostream& operator<<(ostream& os, const Department& department);
bool University::failure = false;
bool University::success = true;
bool University::CreateNewDepartment(string dName, string dLocation, long dChairID)
{
if((dChairID != 0) && (ValidFaculty(dChairID)== University::failure))
{
Department D(dName, dLocation, dChairID);
Departments.push_back(D);
for (int i = 0; i < Departments.size(); i++)
cout << Departments;
return University::success;
}
return University::failure;
}
bool University::ValidFaculty(long dChairID)
{
for (int i = 0; i < Faculties.size(); i++)
{
if (Faculties[i].ID == dChairID)
return University::success;
}
return University::failure;
}
ostream& operator<<(ostream& os, const vector<Department>& D)
{
for (int i = 0; i < D.size(); i++)
os << D[i] << endl;
os << "\n";
return os;
}
ostream& operator<< (ostream& os, const Department& department)
{
department.Print(os);
return os;
}
#endif
My Department.h:
#ifndef DEPARTMENT_H
#define DEPARTMENT_H
#include<vector>
#include<string>
#include<iostream>
using namespace std;
class Department
{
friend class University;
friend ostream& operator<< (ostream& os, Department& department);
protected:
long ID;
string name;
string location;
long chairID;
static long nextDepartID;
public:
Department();
Department(string dName, string dLocation, long dChairID);
void Get();
void Print(ostream& os)const;
};
#endif
My Department.cpp:
#ifndef DEPARTMENT_CPP
#define DEPARTMENT_CPP
#include<iostream>
#include<string>
using namespace std;
#include "Department.h"
long Department::nextDepartID = 100;
Department::Department()
{
ID = nextDepartID++;
name = "Null";
location = "Null";
chairID = 0;
}
Department::Department(string dName, string dLocation, long dChairID):name(dName), location(dLocation), chairID(dChairID)
{
ID = nextDepartID++;
}
void Department::Get()
{
}
void Department::Print(ostream& os)const
{
os << "\n";
os << ID << endl;
os << name << endl;
os << location << endl;
os << chairID << endl;
os <<"\n\n";
}
ostream& operator<< (ostream& os, const Department& department)
{
department.Print(os);
return os;
}
#endif
Now everything can be seen that pertains only to this problem. The only error I receive now is that void value is not being ignored.
Snippet of error:
University.cpp: In function ‘std::ostream& operator<<(std::ostream&, const Department&)’:
University.cpp:53: error: void value not ignored as it ought to be
Department.cpp: In function ‘std::ostream& operator<<(std::ostream&, const Department&)’:
Department.cpp:42: error: void value not ignored as it ought to be
FINAL EDIT:
Thanks to everyone that helped me. I definitely have a better understanding of operator overloading now...especially when it deals with printing vectors of user-defined types!

The complaint was that while your function to iterate over and print the vector contents may have been correct, the actual object contained by the vector did not have an operator<< specified.
You need to have one.
If you already have a method called Print() in your Department class, you could simply create an overload for operator<< as follows:
std::ostream& operator<<(std::ostream& os, const Department& department) {
os<<department.Print();
return os;
}
I had prepared the following code before you posted your update. Maybe it can help you.
#include<iostream>
#include<vector>
#include<string>
class Department {
public:
Department(const std::string& name)
: m_name(name) { }
std::string name() const {
return m_name;
}
private:
std::string m_name;
};
// If you were to comment this function, you would receive the
// complaint that there is no operator<< defined.
std::ostream& operator<<(std::ostream& os, const Department& department) {
os<<"Department(\""<<department.name()<<"\")";
return os;
}
// This is a simple implementation of a method that will print the
// contents of a vector of arbitrary type (not only vectors, actually:
// any container that supports the range-based iteration): it requires
// C++11.
template<typename T>
void show(const T& container) {
for(const auto& item : container) {
std::cout<<item<<std::endl;
}
}
int main() {
std::vector<Department> deps = {{"Health"}, {"Defense"}, {"Education"}};
show(deps);
}
Compile with g++ example.cpp -std=c++11 -Wall -Wextra (I used OS X 10.7.4 and GCC 4.8.1) to get:
$ ./a.out
Department("Health")
Department("Defense")
Department("Education")

Related

How can I fix g++ Undefined symbols for architecture x86_64 error? I use IDE clion

I have only one custom class with .h and .cpp files but I get error while trying to create an instance of this class from main.cpp (using IDE clion).
I am just a beginner and reading C++ primer learning class, if there are some inappropriate things please point out.
main.cpp
#include "Fun.h"
void Solve();
int main()
{
Solve();
return 0;
}
void Solve()
{
Sales_data total;
if(read(cin,total)){ //read the first record
Sales_data temp;
while(read(cin,temp)){
if(temp.isbn() == total.isbn())
total.combine(temp);
else{
print(cout,total) << endl;
total = temp;
}
}
print(cout,total);
}//if
else{
cerr << "No data!?" << endl;
}
}
Fun.cpp
#include "Fun.h"
using std::istream;
using std::ostream;
double Sales_data ::avg_price() const {
if(units_sold)
return revenue/units_sold;
else
return 0;
}
Sales_data& Sales_data:: combine(const Sales_data& rhs){
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
}
istream &read(istream &is,Sales_data &item)
{
double price = 0;
is >> item.bookNo >> item.units_sold >> price;
item.revenue = price * item.units_sold;
return is;
}
ostream &print(ostream &os, const Sales_data &item)
{
os << item.isbn() << " " << item.units_sold << " "
<< item.revenue << " " << item.avg_price() ;
return os;
}
Sales_data add(const Sales_data& lhs,const Sales_data& rhs)
{
Sales_data sum = lhs;
sum.combine(rhs);
return sum;
}
Fun.h
#ifndef PROJECT1_FUN_H
#define PROJECT1_FUN_H
#include <cstdio>
#include <cstring>
#include <cassert>
#include <vector>
#include <iostream>
using std::istream;
using std::ostream;
using std :: cin;
using std :: cout;
using std :: cerr;
using std :: endl;
struct Sales_data{
std::string isbn()const{return bookNo;}
Sales_data& combine(const Sales_data&);
double avg_price() const;
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0;
};
Sales_data add(const Sales_data&,const Sales_data&);
istream &read(istream &,Sales_data &);
ostream &print(ostream &,Sales_data &);
#endif //PROJECT1_FUN_H
This is the error information:
I wonder what caused the problem?
I think the problem is that declaration and definition of the print function are different (the Sales_data parameter is a reference in declaration while is a constant reference in definition):
ostream &print(ostream &,Sales_data &); // Fun.h
ostream &print(ostream &os, const Sales_data &item) // Fun.cpp
Change the declaration in the header file to:
ostream &print(ostream &, const Sales_data &);

Errors: Redefinition of class and Previous Definition of class in C++

When I try to run the class with the class counter.cpp, I get the following error
In file included from Bounded_Counter.h:7:0,
from counterApp.cpp:4:
Lower_Bounded_Counter.h:9:7: error: redefinition of âclass LowerBoundedCounterâ
Lower_Bounded_Counter.h:9:7: error: previous definition of âclass LowerBoundedCounterâ
In file included from Bounded_Counter.h:8:0,
from counterApp.cpp:4:
Upper_Bounded_Counter.h:9:7: error: redefinition of âclass UpperBoundedCounterâ
Upper_Bounded_Counter.h:9:7: error: previous definition of âclass UpperBoundedCounterâ
I am aware that I am including some classes twice, but I don't know how to find it. Can you please help me find what I am doing wrong? There are 4 classes Counter.h, LowerBoundedCounter.h, UpperBoundedCounter.h, and BoundedCounter.h. LowerBoundedCounter.h and UpperBoundedCounter.h both include the Counter.h file. BoundedCounter.h includes both the LowerBoundedCounter.h and the UpperBoundedCounter.h files. The implementation file is counterApp.cpp (not provided here)
Here is the Counter.h class.
#ifndef COUNTER_H
#define COUNTER_H
#include <iostream>
class Counter{
private:
int val;
public:
Counter():val(0) {}
Counter(int val):val(val){}
void up(){
this->val++;
}
void down(){
this->val--;
}
int getVal() const {
return this->val;
}
friend std::ostream &operator <<(std::ostream &os, const Counter &counter) {
os << "A Counter with a value of " << counter.val;
return os;
}
};
#endif
Here is LowerBoundedCounter.h class. This class contains a 'Counter' object.
#ifndef LOWER_BOUNDED_COUNTER_H
#define LOWER_BOUNDER_COUNTER_H
#include<iostream>
#include "Counter.h"
class LowerBoundedCounter{
private:
Counter counter;
int limit;
public:
LowerBoundedCounter(int limit,int val):counter(val), limit(limit){
}
LowerBoundedCounter(int val):counter(val),limit(10){
}
LowerBoundedCounter():counter(),limit(0){
}
void up(){
if(getVal() > limit){
counter.up();
}
}
void down(){
counter.down();
}
int getLimit() const{
return this->limit;
}
int getVal() const{
return counter.getVal();
}
friend std::ostream &operator <<(std::ostream &os, const LowerBoundedCounter &LowerBoundedCounter){
os << "An LowerBoundedCounter with a value of " << LowerBoundedCounter.getVal() << " and a limit of "<<LowerBoundedCounter.limit;
return os;
}
};
#endif
Here's the UpperBoundedCounter.h class
#ifndef UPPER_BOUNDED_COUNTER_H
#define UPPER_BOUNDER_COUNTER_H
#include<iostream>
#include "Counter.h"
class UpperBoundedCounter{
private:
Counter counter;
int limit;
public:
UpperBoundedCounter(int limit,int val):counter(val), limit(limit){
}
UpperBoundedCounter(int val):counter(val),limit(10){
}
UpperBoundedCounter():counter(),limit(10){
}
void up(){
if(getVal() < limit){
counter.up();
}
}
void down(){
counter.down();
}
int getLimit() const {
return this->limit;
}
int getVal() const{
return counter.getVal();
}
friend std::ostream &operator <<(std::ostream &os, const UpperBoundedCounter &UpperBoundedCounter){
os << "An UpperBoundedCounter with a value of " << UpperBoundedCounter.getVal() << " and a limit of "<<UpperBoundedCounter.limit;
return os;
}
};
Finally, I have objects from all 3 of the above classes in BoundedCounter.h
#ifndef BOUNDED_COUNTER_H
#define BOUNDER_COUNTER_H
#include<iostream>
#include "Counter.h"
#include "Lower_Bounded_Counter.h"
#include "Upper_Bounded_Counter.h"
class BoundedCounter{
private:
Counter counter;
UpperBoundedCounter upperBoundedCounter;
LowerBoundedCounter lowerBoundedCounter;
public:
BoundedCounter(int val, int upperLimit, int lowerLimit):counter(val),upperBoundedCounter(upperLimit),lowerBoundedCounter(lowerLimit){
}
BoundedCounter(int val,int upperLimit):counter(val), upperBoundedCounter(upperLimit), lowerBoundedCounter(){
}
BoundedCounter(int val):counter(val),upperBoundedCounter(),lowerBoundedCounter(){
}
BoundedCounter():counter(), upperBoundedCounter(), lowerBoundedCounter(){
}
void up(){
if(getVal() < upperBoundedCounter.getLimit() && getVal() > lowerBoundedCounter.getLimit()){
counter.up();
}
}
void down(){
counter.down();
}
int getLowerLimit() const{
return lowerBoundedCounter.getLimit();
}
int getUpperLimit() const{
return upperBoundedCounter.getLimit();
}
int getVal() const{
return counter.getVal();
}
friend std::ostream &operator <<(std::ostream &os, const BoundedCounter &BoundedCounter){
os << "An BoundedCounter with a value of " << BoundedCounter.getVal() << " and a lower limit of "<<BoundedCounter.getLowerLimit()
<<" and a higher limit of "<<BoundedCounter.getUpperLimit();
return os;
}
};
#endif
Your header protection macro is wrong in all header files.
#ifndef BOUNDED_COUNTER_H
#define BOUNDER_COUNTER_H
Typo 'R'
There is a smaller problem in the UpperBoundedCounter.h, which does not have the closing #endif . But this will cause another problem.

Unable to find segmentation fault error in c++

I have 3 c++ files, instrument.h, percussion.h, and instrumentApp.cpp. Instrument.h is the base class and percussion.h inherits it. Percussion objects are defined and implemented in the instrumentApp.cpp class. Whenever I run instrumentApp.cpp, I get the segmentation fault error.
I have managed to trace the cause of the error to the overloaded << operator function in percussion.h where I am calling a method of the base class instrument.h. For some reason, my code is unable to call methods of the base class and I don't know why. Can you please help me?
Here is the instrument.h class
#ifndef INSTRUMENT_H
#define INSTRUMENT_H
class Instrument{
private:
std::string name;
std::string sound;
std::string lowRange;
std::string highRange;
public:
Instrument(std::string name, std::string sound, std::string lowRange, std::string highRange){
this->name = name;
this->sound = sound;
this->lowRange = lowRange;
this->highRange = highRange;
}
std::string getName() const{
return this->name;
}
std::string play()const {
return this->sound;
}
std::string getLowRange() const{
return this->lowRange;
}
std::string getHighRange() const{
return this->highRange;
}
bool isWind();
bool isWoodWind();
bool isBrass();
bool isKeyboard();
bool isPercussion();
bool isStrings();
friend std::ostream &operator <<(std::ostream &os, const Instrument &instrument){
}
};
#endif
Here is the percussion.h class
#ifndef PERCUSSION_H
#define PERCUSSION_H
#include "instrument.h"
class Percussion : public Instrument{
private:
bool struck;
public:
Percussion(std::string name, std::string sound, std::string lowRange, std::string highRange, bool struck) : Instrument(name,sound,lowRange,highRange){
this->struck=struck;
}
bool isStrucked() const {
return this->struck;
}
bool isPercussion() {
return true;
}
std::string getType() const{
if(this->struck){
return "struck";
}
else{
return "";
}
}
friend std::ostream &operator <<(std::ostream &os, Percussion &percussion){
//The error stems from this line of code
//Apparently, the getName() method in the base class isn't called
os<<percussion.getName();
}
};
#endif
Here is the implementation file instrumentApp.cpp
#include <iostream>
#include <string>
#include <sstream>
#include <cstdlib>
#include "instrument.h"
#include "percussion.h"
#include "strings.h"
using namespace std;
int main() {
Percussion timpani("timpani", "boom", "D2", "A2", true);
cout << timpani << endl;
Percussion harp("harp", "pling", "Cb1", "F#7", false);
cout << harp << endl;
return 0;
}
The problem here is that I wasn't returning the os object when I overloaded the << operator.
The fix is as follows in the percussion.h file
friend std::ostream &operator <<(std::ostream &os, Percussion &percussion){
os<<percussion.getName();
return os;
}

C++ error: output not defined in namespace

Update: I made Sergii's changes below, but now I get the error: undefined reference to `cs202::operator<<(std::basic_ostream >&, cs202::Rational const&)'. Any ideas how to fix this? Thanks
I would appreciate help figuring out why I am getting this error:
"error: 'output' is not a member of namespace 'cs202'"
I have a class called Rational as follows:
#ifndef RATIONAL_H
#define RATIONAL_H
namespace cs202{
class Rational
{
private:
int m_numerator;
int m_denominator;
public:
Rational(int nNumerator = 0, int nDenominator = 1) {
m_numerator = nNumerator;
m_denominator = nDenominator;
}
int getNumerator(){return m_numerator;}
int getDenominator(){return m_denominator;}
friend std::ostream& operator<<(std::ostream& output, const Rational& cRational);
};
}
#endif
The implementation file for the friend function which overrides the << operator looks like this:
#include "rational.h"
namespace cs202{
friend std::ostream& operator<<(std::ostream& output, const Rational& cRational)
{
output << cRational.m_numerator << "/" << cRational.m_denominator;
return output;
}
}
Finally, Main looks like this:
#include <iostream>
#include "rational.h"
using namespace std;
using namespace cs202;
int main()
{
Rational fraction1(1, 4);
cs202::output << fraction1 << endl;
return 0;
}
I have tried using cout instead of cs202:output, I have tried with and without the namespace cs202 (which is a requirement of the assignment), and I have tried making the operator overload function a member function of the class rather than a friend function to no avail.
What am I missing? Thanks
I suppose you want it out to standard output (to console)
int main()
{
Rational fraction1(1, 4);
std::cout << fraction1 << endl;
return 0;
}
Also you do not need friend here. "Friend" keyword is used only in a class
#include "rational.h"
namespace cs202{
std::ostream& operator<<(std::ostream& output, const Rational& cRational)
{
output << cRational.m_numerator << "/" << cRational.m_denominator;
return output;
}
}
Thanks, I figured it out. I had to change the placement of the {} for the namespace.

Operator<< cant access private int of class

I get this when i try to compile:
../Monster.h:26:9: error: ‘int ProjectIV::Monster::con’ is private
`int con;`
^
../Monster.cpp:17:39: error: within this context
cout << "Constitution: " << monster.con << endl;
^
make: * [Monster.o] Error 1
From what I understand making operator<< a friend should allow it to access int con. What am I not seeing.
Monster.h:
#ifndef MONSTER_H_
#define MONSTER_H_
#include <iostream>
using std::cout;
using std::endl;
using std::ostream;
#include <string>
using std::string;
namespace ProjectIV
{
class Monster
{
friend ostream &operator<< (ostream &out, const Monster &monster);
public:
Monster(int con);
private:
int con;
};
} /* namespace ProjectIV */
#endif /* MONSTER_H_ */
Monster.cpp:
#include "Monster.h"
ostream &operator<< (ostream &out, const ProjectIV::Monster &monster)
{
cout << "Constitution: " << monster.con << endl;
return out;
}
ProjectIV::Monster::Monster(int con): con(con)
{}
main.cpp:
#include "Monster.h"
using namespace ProjectIV;
int main()
{
Monster Gojira(140);
cout << Gojira << endl;
return 0;
}
This:
ostream& operator<<(ostream& out, const ProjectIV::Monster& monster)
should be:
ostream& ProjectIV::operator<<(ostream& out, const ProjectIV::Monster& monster)
Here your not working example, and here is the working one.
Also, as per AndreyT's comment, you should add a function declaration before the friend declaration:
namespace ProjectIV {
class Monster {
friend ostream& operator<<(ostream& out, const Monster& monster);
public:
Monster(int con);
private:
int con;
};
ostream& operator<<(ostream& out, const Monster& monster);
// ^^^ this
}
There are two problems with your code.
Firstly, the friend declaration inside Monster class refers to ProjectIV::operator << function. It is ProjectIV::operator << that will become the friend of Monster. What you defined in your Monster.cpp file is actually a ::operator <<. This is a completely different function that is not a friend of Monster. This is why you get the error.
So, you need to decide what function you want to make a friend - the one in global namespace or the one in ProjectIV namespace - and act accordingly.
If you want to make your operator << a member of ProjectIV namespace, you run into the second problem. Friend declarations refer to member of enclosing namespace, but they don't introduce the corresponding declarations into the enclosing namespace. It is still your responsibility to add a declaration for operator << in ProjectIV
namespace ProjectIV
{
class Monster
{
friend ostream &operator<< (ostream &out, const Monster &monster);
public:
Monster(int con);
private:
int con;
};
ostream &operator<< (ostream &out, const Monster &monster);
} /* namespace ProjectIV */
and then later define it as a member of ProjectIV
ostream &ProjectIV::operator<< (ostream &out, const ProjectIV::Monster &monster)
{
cout << "Constitution: " << monster.con << endl;
return out;
}