Im am writing a code using the concept of Composition: Objects as Members of Classes and this error appeared for me: [Error] no match to 'operator<<'. Any one can help me? The error started appearing when i tried to include the trabalhador.h and trabalhador.cpp files. If i try to compile the code without this two files it works but i need bouth because im trying to implement this concept by my own.
Here is the code:
//main
#include <iostream>
using std::cout;
#include "trabalhador.h"
int main()
{
cout << "INFORME OS DADOS SOLICITADOS: \n\n";
cout << "Nome do funcionario: ";
info i1;
cout << "CPF: ";
info i2;
trabalhador t(i1, i2);
return 0;
}
//trabalhador.h
#ifndef TRABALHADOR_H
#define TRABALHADOR_H
#include "info.h"
class trabalhador
{
public:
trabalhador (const info &, const info &);
void print () const;
private:
const info funcionarioNome;
const info funcionarioCPF;
};
#endif
//trabalhador.cpp
#include <iostream>
using std::cout;
#include "trabalhador.h"
#include "info.h"
trabalhador::trabalhador (const info &infoNome, const info &infoCPF)
:funcionarioNome (infoNome),
funcionarioCPF (infoCPF)
{
cout << "Dados do funcionario: ";
}
void trabalhador::print() const
{
cout << funcionarioNome << funcionarioCPF;
}
//info.h
#include <string>
using std::string;
#ifndef INFO_H
#define INFO_H
class info
{
public:
info (string = "");
void setInfoDado (string);
void setInfo ();
private:
string infoDado;
};
#endif
//info.cpp
#include <iostream>
using std::cout;
using std::cin;
#include <string>
using std::string;
using std::getline;
#include "info.h"
info::info (string info)
{
setInfoDado (info);
}
void info::setInfoDado (string info)
{
infoDado = info;
setInfo();
}
void info::setInfo ()
{
string nome;
getline (cin, nome);
infoDado = nome;
}
You need to provide an overloaded operator<< for your info class.
The requirement for this stems from the line
cout << funcionarioNome << funcionarioCPF;
It needs to know how to render the class using an ostream.
It's as simple as
class info
{
public:
// ...
friend std::ostream& operator<<(std::ostream& os, info const& inf)
{
os << inf.infoDado;
return os;
}
// ...
};
See the full code here.
//new constructor in trabalhador.cpp
trabalhador::trabalhador (const info &infoNome, const info &infoCPF)
:funcionarioNome (infoNome),
funcionarioCPF (infoCPF)
{
cout << "\nDados do funcionario: \n";
print();
}
//function created in trabalhador.cpp
void trabalhador::print() const
{
funcionarioNome.print();
funcionarioCPF.print();
}
//function created in info.h
void print () const;
//function created in info.cpp
void info::print () const
{
cout << infoDado << "\n";
}
Related
I'd like to know is it possible that two classes has attributes and can use methods of each other. For example, there're a class STUDENT and a class COURSE, a STUDENT have a list of joined courses and a COURSE have list of participants(students). I tried this:
in STUDENT.h
#include <iostream>
#include <vector>
// #include "COURSE.h"
class COURSE;
class STUDENT {
string name;
std::vector<COURSE*> listCourses;
public:
STUDENT(){};
addCourse(COURSE* &course){
listCourses.push_back(course);
course.addStudent(this);
}
string getName(){
return this->name;
}
void showCourses(){
for(COURSE* course : listCourses)
std::cout << course->getName() << std::endl;
}
};
in COURSE.h
#include <iostream>
#include <vector>
// #include "STUDENT.h"
class STUDENT;
class COURSE {
string name;
std::vector<STUDENT*> listStudents;
public:
COURSE(){}
addStudent(STUDENT* &student){
listStudents.push_back(student);
student.addCourse(this);
}
string getName(){
return this->name;
}
void showStudent(){
for(STUDENT* student : listCourses)
std::cout << student->getName() << std::endl;
}
};
If I include two classes each other, it said errors. If I just include one, just one class worked, other class has problem.
Can anyone help me to fix it and I wonder that is it necessary to use some design patterns or data structures to solve this.
Thank you
Yes, what you are attempting is possible, but not in the way you are attempting it. You need to separate your method declarations and definitions.
Also, you have a flaw in your add... methods that will lead to endless recursion as soon as a Student is added to a Course or vice versa. You need to detect when the two are already linked together so that you can avoid the loop.
Try something more like this:
Student.h
#ifndef StudentH
#define StudentH
#include <vector>
#include <string>
class Course;
class Student {
std::string name;
std::vector<Course*> listCourses;
public:
Student() = default;
~Student();
std::string getName() const;
void addCourse(Course* course);
void removeCourse(Course* course);
void showCourses() const;
};
#endif
Student.cpp
#include "Student.h"
#include "Course.h"
#include <iostream>
#include <algorithm>
Student::~Student() {
for(Course* course : listCourses) {
course->removeStudent(this);
}
}
std::string Student::getName() const {
return name;
}
void Student::addCourse(Course* course) {
if (std::find(listCourses.begin(), listCourses.end(), course) == listCourses.end()) {
listCourses.push_back(course);
course->addStudent(this);
}
}
void Student::removeCourse(Course* course) {
auto iter = std::find(listCourses.begin(), listCourses.end(), course);
if (iter != listCourses.end()) {
listCourses.erase(iter);
course->removeStudent(this);
}
}
void Student::showCourses() const {
for(Course* course : listCourses) {
std::cout << course->getName() << std::endl;
}
}
Course.h
#ifndef CourseH
#define CourseH
#include <vector>
#include <string>
class Student;
class Course {
std::string name;
std::vector<Student*> listStudents;
public:
Course() = default;
~Course();
std::string getName() const;
void addStudent(Student* student);
void removeStudent(Student* student);
void showStudents() const;
};
#endif
Course.cpp
#include "Course.h"
#include "Student.h"
#include <iostream>
#include <algorithm>
Course::~Course() {
for(Student* student : listStudents) {
student->removeCourse(this);
}
}
std::string Course::getName() const {
return name;
}
void Course::addStudent(Student* student) {
if (std::find(listStudents.begin(), listStudents.end(), student) == listStudents.end()) {
listStudents.push_back(student);
student->addCourse(this);
}
}
void Course::removeStudent(Student* student) {
auto iter = std::find(listStudents.begin(), listStudents.end(), student);
if (iter != listStudents.end()) {
listStudents.erase(iter);
student->removeCourse(this);
}
}
void Course::showStudents() const {
for(Student* student : listStudents) {
std::cout << student->getName() << std::endl;
}
}
I'm making a little shell that can just do random stuff. Bt whenever i compile i receive the error 'Shell' has not been declared
I declared the class shell and the object i n main.cpp, i've looked for a while and nothing. I'm new to oop so this may be pretty stupid but i've done wha to know
I'm using 3 files
main.cpp:
#include <iostream>
#include "shell/shell.cpp"
int main ()
{
Shell shl;
while (!shl.exitTime())
{
std::cin.ignore();
shl.putIn(std::getline(std::cin));
}
}
/shell/shell.cpp:
#include <vector>
#include "shell.h"
class Shell {
private:
std::string in;
bool exitBool;
public:
// Functions
void clear();
void print(std::string inp);
void println(std::string inp);
void putIn(std::string inp);
std::string input();
bool exitTime();
Shell()
{
exitBool = false;
}
};
and /shell/shell.h:
#include <vector>
void Shell::print(std::string inp)
{
std::cout << inp;
}
void Shell::println(std::string inp)
{
std::cout << inp << std::endl;
}
void Shell::putIn(std::string inp)
{
inp = in;
}
std::string Shell::input()
{
return in;
}
bool exitTime()
{
return exitBool;
}
You should
Write definitions of class functions in .cpp files.
Write declarations of class functions in .h files.
Include .h files.
You actually did
Write definitions of class functions in .h files.
Write declarations of class functions in .cpp files.
Include .cpp files.
Try this:
main.cpp:
#include <iostream>
#include "shell/shell.h"
int main ()
{
Shell shl;
while (!shl.exitTime())
{
std::cin.ignore();
shl.putIn(std::getline(std::cin));
}
}
/shell/shell.cpp:
#include <vector>
#include "shell.h"
void Shell::print(std::string inp)
{
std::cout << inp;
}
void Shell::println(std::string inp)
{
std::cout << inp << std::endl;
}
void Shell::putIn(std::string inp)
{
inp = in;
}
std::string Shell::input()
{
return in;
}
bool exitTime()
{
return exitBool;
}
and /shell/shell.h:
#include <vector>
#include <string>
class Shell {
private:
std::string in;
bool exitBool;
public:
// Functions
void clear();
void print(std::string inp);
void println(std::string inp);
void putIn(std::string inp);
std::string input();
bool exitTime();
Shell()
{
exitBool = false;
}
};
I am coding a little RPG(Role Playing Game)
Here is the situation: I created an object Personnage.
In my classes, I created a method atttaquer. But I would like that after calling my method attaquer it writes something like this: Goliath attaque David . But to that, I need to grab the name of the Object. Because the player may want to edit the name of Object (The personage name) before playing.
There is my code:
Personnage.h
#ifndef Personnage_h
#define Personnage_h
#include <string>
#include "Arme.h"
class Personnage{
//methods
public:
Personnage();
Personnage(std::string nomArme, int degatsArme);
Personnage(int vie, int mana);
// ~Personnage();
void recevoirDegats(int nbDegats);
void attaquer(Personnage &cible);
private:
// Attributs
int m_vie;
int m_magie;
std::string m_nom;
};
#endif
My Personnage.cpp code:
#include "Personnage.h"
#include <string>
#include <iostream>
void Personnage::recevoirDegats(int nbDegats){
m_vie -= nbDegats;
if (m_vie < 0) {
m_vie = 0;
}
}
void Personnage::attaquer(Personnage &cible){
cible.recevoirDegats(m_arme.getDegats());
// if David attacks Goliath I want to write std::cout << David << "attaque "<< Goliath << endl; but I do not know how to grab the name of the object after it's creation
}
There is my main.cpp
#include <iostream>
#include <string>
#include "Personnage.h"
//#include "Personnage.cpp"
#include <ctime>
using namespace std;
int main()
{
Personnage David, Goliath, Atangana("Ak47", 35);
Goliath.attaquer(David);
return 0;
}
If you want to give your objects names, it cannot be the variable names. They are only meant for the compiler and they are fixed. So you need to create a class that can have a name:
class NamedObject
{
private:
std::string m_name;
public:
const std::string& getName() const
{
return m_name;
}
void setName(const std::string& name)
{
m_name = name;
}
}
And if you want your classes to have a name, the easiest way would be to derive from it:
class Personnage : NamedObject {
Then you can say:
Personnage player1, player2;
player1.setName("David");
player2.setName("Goliath");
Alternatively, you can get those string from user input.
And if you need to address one by name:
std::cout << player1.getName() << " please make your move." << std::endl;
I'm trying to define the << operator for a base class, so I can later print the identifier of each object easily.
I have tried an SSCCE, but I don't really know where the problem is, since I'm printing a direction and not the content. I don't know if the problem appears by printing the vector position, or by printing the std::string(char[]).
main.cpp:
#include "City.h"
#include <vector>
#include <iostream>
int main() {
std::vector<Location*> locations;
locations.push_back(new City("TEST"));
for(unsigned int it = 0; it<locations.size(); it++){
std::cout << locations[it] << std::endl;
}
return 0;
}
Location.h:
#include <cstring>
#include <string>
class Location {
public:
Location(const std::string id);
friend std::ostream& operator<<(std::ostream& os, Location& loc);
std::string GetId();
private:
// Name/identifier of the location
char ID[5];
};
Location.cpp:
#include "Location.h"
Location::Location(const std::string id){
memset(this->ID, 0, 5);
strncpy(this->ID, id.c_str(), 5); // I have it as a char[5] at a larger app
}
std::string Location::GetId(){
return std::string(this->ID);
}
std::ostream& operator<<(std::ostream& os, Location& loc){
os << loc.GetId();
return os;
}
City.h:
#include "Location.h"
class City: public Location{
public:
City(std::string id);
};
City.cpp:
#include "City.h"
City::City(const std::string id) : Location(id){
}
Any idea?
I am attempting to make part of a program that uses a bank account class as the base class and checking and savings as the derived classes. I have been trying to set up the basic framework before I do any fancy data handling and I've followed some tutorials to get a better understanding of classes and inheritance.
I have looked for answers but the answers I have found don't seem to be my problem but I might just need another set of eyes on my code.
the compiler errors:
In function main':
badriver.cpp:20: undefined reference toChecking::getAccount()'
badriver.cpp:23: undefined reference to Checking::setAccount(int)'
badriver.cpp:24: undefined reference toSavings::setAccount(int)'
badriver.cpp:26: undefined reference to `Checking::getAccount()'
badriver.cpp
#include "BankAccount.cpp"
#include "Checking.cpp"
#include "Savings.cpp"
#include <string>
#include <iostream>
using namespace std;
int main(){
Checking c;
Savings s;
cout << "Checking: " << c.getAccount() << " - Type: " << c.getType() << endl;
cout << "Savings: " << s.getAccount() << " - Type: " << s.getType() << endl;
c.setAccount(9);
s.setAccount(15);
cout << "New Checking: " << c.getAccount() << endl;
cout << "New Savings: " << s.getAccount() << endl;
return 0;
}
BankAccount.h
#ifndef BANKACCOUNT_H
#define BANKACCOUNT_H
#include <string>
using std::string;
using std::ostream;
using std::istream;
class BankAccount{
private:
int myAccount;
const char* color;
public:
// default constructor
BankAccount();
BankAccount(int account);
virtual ~BankAccount();
virtual void setAccount(int)=0;
int getAccount();
//
// void setSAccount(int);
// int getSAccount();
//
virtual const char* getColor();
virtual const char* getType() = 0;
//virtual const char* getCType() = 0;
protected:
void setColor(const char*);
};
#endif // BANKACCOUNT_H
BankAccount.cpp
#include "BankAccount.h"
#include "Checking.h"
#include "Savings.h"
#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;
// default constructor
BankAccount::BankAccount(){
account = 1;
}
BankAccount::~BankAccount(){}
// void BankAccount::setAccount(int account){
// myAccount = account;
// }
int BankAccount::getAccount(){
return myAccount ;
}
BankAccount::BankAccount(int account){
myAccount = account;
}
const char* BankAccount::getColor(){
return color;
}
void BankAccount::setColor(const char* c){
color = c;
}
Checking.h
#ifndef CHECKING_H
#define CHECKING_H
#include "BankAccount.h"
#include <string>
using std::string;
using std::ostream;
using std::istream;
class Checking : public BankAccount{
private:
const char* type;
public:
Checking();
virtual ~Checking();
void setAccount(int account);
virtual const char* getType();
void setChecking(int);
int getChecking();
};
#endif //CHECKING_H
Checking.cpp
#include "Checking.h"
#include <string>
#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;
Checking::Checking() : BankAccount(1), type("Checking"){}
Checking::~Checking(){}
BankAccount::~BankAccount(){}
void BankAccount::setAccount(int account){
myAccount = account;
}
const char* Checking::getType(){
return type;
}
Savings.h
#ifndef SAVINGS_H
#define SAVINGS_H
#include "BankAccount.h"
#include <string>
using std::string;
using std::ostream;
using std::istream;
class Savings: public BankAccount{
private:
const char* type;
public:
Savings();
virtual ~Savings();
void setAccount(int account);
virtual const char* getType();
void setSavings(int);
int getSavings();
};
#endif // SAVINGS_H
Savings.cpp
#include "Savings.h"
#include <string>
#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;
Savings::Savings() : BankAccount(2), type("Savings"){}
Savings::~Savings(){}
BankAccount::~BankAccount(){}
void BankAccount::setAccount(int account){
myAccount = account;
}
const char* Savings::getType(){
return type;
}
Thanks for any help pointing me in the right direction.
Checking.cpp and Savings.cpp contain:
BankAccount::~BankAccount(){}
void BankAccount::setAccount(int account){
myAccount = account;
}
This causes undefined behaviour because you defined those functions in multiple files. You need to delete those lines from Checking.cpp and Savings.cpp, and instead put in definitions for the functions which are listed as being missing in the compiler output:
void Checking::setAccount(int account){
// code here
}
etc.