How to call a function from another header c++ - c++

I have most of this working I am struggling with syntax errors on the employee.h
here are all 3 of the headers
Address.h
#pragma once
#include <string>
using namespace std;
class Address
{
public:
explicit Address();
explicit Address(const string& city, const string& state,
const string& street, const string& zip);
const string& getCity() const;
const string& getState() const;
const string& getStreet() const;
const string& getZip() const;
void printAddress() const;
private:
string street;
string city;
string state;
string zip;
};
Address::Address() :
city("Beverly Hills,"),
state("CA,"),
street("99999 Sunset Boulevard,"),
zip("99999")
{ }
Address::Address(const string& city, const string& state,
const string& street, const string& zip) :
city(city), state(state), street(street), zip(zip)
{ }
const string& Address::getCity() const
{
return city;
}
const string& Address::getState() const
{
return state;
}
const string& Address::getStreet() const
{
return street;
}
const string& Address::getZip() const
{
return zip;
}
void Address::printAddress() const
{
std::cout << street << city << state << zip << endl;
};
Name.h
#include <string>
using namespace std;
class Name
{
public:
explicit Name();
explicit Name(const string& firstName, const string& middleName, const string& lastName);
const string& getFirstLast() const;
void printName() const;
private:
string firstName;
string middleName;
string lastName;
Name::Name() :
firstName("John"),
middleName("H."),
lastName("Doe")
{}
Name::Name(const string& firstName, const string& middleName, const string& lastName) :
firstName(firstName), lastName(lastName)
{ }
const string& Name::getFirstLast() const
{
return name;
}
void Name::printName() const
{
std::cout << firstName << middleName << lastName << endl;
}
};
Employee.H
This is where I am getting most of my errors.
#include <iostream>
#include <string>
#include "Name.h"
#include "Address.h"
using namespace std;
class Employee
{
public:
explicit Employee();
explicit Employee(const Name& name, const Address& address, const string& ssn);
const string& getName() const;
const string& getSSN() const;
const string& getAddress() const;
void printEmployee() const;
private:
Name name;
Address address;
string ssn;
};
Employee::Employee() :
name("John H. Doe"),
address("99999 Sunset Boulevard", "Beverly Hills", "CA", "99999"),
SSN("999-99-9999")
{}
Employee::Employee(const Name& name, const Address& address, const std::string& ssn) :
name(name), address(address), ssn(ssn)
{ }
const string& Employee::getName() const
{
return printName;
}
const string& Employee::getSSN() const
{
return ssn;
}
const string& Employee::getAddress() const
{
return address
}
void Employee::printEmployee() const
{
cout << Name.printName() << Address.printAddress() << ssn << std::endl;
}
Here are the instructions for the assignment
Name Header file (Name.h) will have:
A default constructor. Remember that the default constructor for Name has the following initial values: Name to "John H. Doe".
A constructor with 3 parameters: one for First name, one for Middle Name, and One for last Name.
3 private string instance variables for :First Name, Middle Name, and a last Name.
getFirstLast() function: it returns the first and middle name and last name in order
printName() function: it prints the first, middle, and last name.
Address Header File (Address.h) will have:
A default constructor. Remember that the default constructor for Address has the following initial values: Address to "99999 Sunset Boulevard" , "Beverly Hills", "CA", "99999"
4 private string instance variables for :Street, City, State, Zip
A constructor with 4 parameters: one for street, one for city, one for state, one for zip.
getCity(): it returns the City
getState(): It returns the State
getStreet(): It returns the street
getZip(): it returns the zip
printAddress(): it prints the Street, City, State, and Zip.
Employee Header File (Employee.h) will have:
3 private instance variables: One for Name (Use the Header Name from above), One for The Address (Use the Address Header from above), a string variable for the SSN.
a default constructor which initializes the SSN to "999-99-9999", Name to "John H. Doe", Address to "99999 Sunset Boulevard" , "Beverly Hills", "CA", "99999"
a constructor with 3 parameters: One for Name, one for Address, a string for SSN.
getName() function: it returns the Name of the Employee
getAddress() function : it returns the address of the Employee.
getSSN() function: it returns the SSN as a string
printEmployee() function:
print the name: Make sure you use the printName() function from Name.h
print the address: Make sure you use the printAddress() function from Address.h
prints the SSN.
Employee (Employee.cpp) class will have:
In the void main() function you would declare:
a Name n;
an Address A;
and an Employee e;
and print e using the printEmployee().
Also you need to declare:
A Name n1: Your Name
an Address a1: your own address
string ssn1: 987-65-4321
An employee e1 which has a name n1, and address A1, and ssn1.
Print e1 using printEmployee().

I tried to create a complete example for you.
You should put declarations in header *.h files.
Implementations go into the *.cpp files.
Address.h
#pragma once
#include <string>
class Address
{
public:
explicit Address();
explicit Address(const std::string& city, const std::string& state,
const std::string& street, const std::string& zip);
const std::string& getCity() const;
const std::string& getState() const;
const std::string& getStreet() const;
const std::string& getZip() const;
void printAddress() const;
private:
std::string street;
std::string city;
std::string state;
std::string zip;
};
Address.cpp
#include "Address.h"
#include <iostream>
// Default Constructor
Address::Address() :
city("Beverly Hills"),
state("CA"),
street("99999 Sunset Boulevard"),
zip("99999")
{ }
Address::Address(const std::string& city, const std::string& state,
const std::string& street, const std::string& zip) :
city(city), state(state), street(street), zip(zip)
{ }
const std::string& Address::getCity() const
{
return city;
}
const std::string& Address::getState() const
{
return state;
}
const std::string& Address::getStreet() const
{
return street;
}
const std::string& Address::getZip() const
{
return zip;
}
void Address::printAddress() const
{
// removed the endl here !
std::cout << "Address: " << street << ", " << city << ", "
<< state << ", " << zip;
};
Name.h
#pragma once
#include <string>
class Name
{
public:
explicit Name(const std::string& firstName, const std::string& lastName);
void printName() const;
private:
std::string firstName;
std::string lastName;
};
Name.cpp
#include "Name.h"
#include <iostream>
Name::Name(const std::string& firstName, const std::string& lastName) :
firstName(firstName), lastName(lastName)
{ }
void Name::printName() const
{
std::cout << "Name: " << lastName << ", " << firstName;
}
Employee.h
#pragma once
#include <string>
#include "Name.h"
#include "Address.h"
class Employee
{
public:
explicit Employee(const Name& name, const Address& address, const std::string& ssn);
void printEmployee() const;
private:
Name name;
Address address;
std::string ssn;
};
Employee.cpp
#include "Employee.h"
#include <iostream>
Employee::Employee(const Name& name, const Address& address, const std::string& ssn) :
name(name), address(address), ssn(ssn)
{ }
void Employee::printEmployee() const
{
std::cout << "Employee: ";
name.printName();
std::cout << ", ";
address.printAddress();
std::cout << ", ssn: " << ssn << std::endl;
}
Main.cpp
#include <iostream>
#include <string>
#include "Employee.h"
int main(int argc, char* argv[]) {
Address address("Cologne", "NRW", "Domplatz 1", "D-50668");
Name name("John", "Jones");
Employee employee(name, address, "123-abc-456");
employee.printEmployee();
return 0;
}
That was fun! :-)
EDIT:
I think I need to add a little explanation:
If you want to use a function or method from one cpp file in another cpp file you should seperate declaration and implementation. Actually you should always do this. Put declaration in the *.h file and implementation in *.cpp file. In the cpp file where you want to use the function include the header file where the function is declared. Then compile all cpp files to object files. And then link all the objects files together.
For example look in Employee.cpp file. There I use the print method from the Address class. Now look at the includes of the Employee.cpp file. There you see I included Employee.h which in turn includes Address.h. Including is actually just inserting the content of one file in the other. So now we have included the declaration of the Address class and methods in the Employee cpp file so it can be called from there.
Another Update
I tested the code I posted yesterday. It works fine. The problem starts because you want to change the printEmployee() method like this:
void Employee::printEmployee() const
{
cout << Name.printName() << Address.printAddress() << ssn << std::endl;
}
This will only work when the printName() and printAddress() methods do not print but rather return a string. In that case software developers often name the method ToString() or similiar. But we will leave the name as it is because we stick to the assignment of your professor.
You must change the declaration of the methods in the header files to return a string. And then you must also change the implementation in the cpp files to return a string. Here you go (not complete source code, only the changes) :
Address.h
class Address
{
public:
// .....
std::string& printAddress() const;
// .....
};
Address.cpp
// Add this new include to the others at the top of the file:
#include <sstream>
// ......
std::string& Address::printAddress() const
{
std::stringstream ss;
ss << "Address: " << street << ", " << city << ", "
<< state << ", " << zip;
return ss.str();
}
Name.h
class Name
{
public:
// .....
std::string& printName() const;
// .....
};
Name.cpp
// Add this new include to the others at the top of the file:
#include <sstream>
// ......
std::string& Name::printName() const
{
std::stringstream ss;
ss << "Name: " << lastName << ", " << firstName;
return ss.str();
}
Now you can use them in the Employee class like you wanted:
Employee.cpp
void Employee::printEmployee() const
{
std::cout << Name.printName() << Address.printAddress() << ssn << std::endl;
}
Final Edit
After I read the details of the assignment you posted, I think my first solution was good. Your Professor clearly says the printAddress() and printName() methods should print not return a string. So maybe you should consider using the code from my first solution.

Related

Why do I get a "no matching constructor error?

I want my code to take a name, mail and car as argument types, and I try to do so in a class named Person. In main(), I try to give that class a variable a which I can call later in cout. However, I get this exact error:
no matching constructor for initialization of "Person"
How can I fix this?
The h. file
#pragma once
#include <iostream>
#include "car.h"
#include <string>
class Person{
private:
std::string name;
std::string mail;
Car* car;
public:
Person(std::string name, std::string mail);
Person(std::string name, std::string, Car* car);
void setMail(std::string mail);
std::string getMail() const;
std::string getName() const;
bool hasAvailableFreeSeats();
friend std::ostream& operator <<(std::ostream& os, const Person& person);
};
The cpp file:
#include "person.h"
std::string Person:: getMail() const{
return mail;
}
std:: string Person:: getName() const{
return name;
}
void Person:: setMail(std::string mail){
this -> mail = mail;
}
Person:: Person(std::string name, std::string mail) : Person(name, mail, nullptr){};
Person::Person(std::string name, std::string, Car* car) : name{name}, mail{mail}, car{car}{};
bool Person:: hasAvailableFreeSeats(){
if (car != nullptr){
return car-> hasFreeSeats();
}
}
std::ostream& operator <<(std::ostream& os, const Person& person){
return os << person.name << ": " << person.mail << "\n";
}
main:
#include "person.h"
int main(){
std::string name{"Ola Normann"};
std::string mail{"ola.normann#norge.no"};
std::unique_ptr<Car> car{new Car{5}};
Person a{name, mail, std::move(car)};
};
First off, you have a typo in your 3-parameter Person constructor. The 2nd parameter has no name assigned to it, so you end up initializing the mail class member with itself, not with the caller's input:
Person::Person(std::string name, std::string, Car* car) : name{name}, mail{mail}, car{car}{};
^ no name here! ^ member!
That should be this instead:
Person::Person(std::string name, std::string mail, Car* car) : name{name}, mail{mail}, car{car}{};
^ has a name now! ^ parameter!
Now, that being said, your main() code is passing 3 values to the Person constructor:
Person a{name, mail, std::move(car)};
Your Person class only has 1 constructor that accepts 3 parameters:
Person(std::string name, std::string, Car* car);
In main(), your name and mail variables are std::string objects, which is fine, but your car variable is a std::unique_ptr<Car> object, not a Car* pointer. std::move(car) will return a std::unique_ptr<Car>&& rvalue reference, which Person does not accept, hence the compiler error. std::unique_ptr is not implicitly convertible to a raw pointer. You would have to use its get() method instead:
Person a{name, mail, car.get()};
Which defeats the purpose of using std::unique_ptr in the first place. You should instead change the Person class to hold a std::unique_ptr object instead of a raw pointer, eg:
.h
#pragma once
#include <iostream>
#include <string>
#include <memory>
#include "car.h"
class Person{
private:
std::string name;
std::string mail;
std::unique_ptr<Car> car;
public:
...
Person(std::string name, std::string mail);
Person(std::string name, std::string mail, std::unique_ptr<Car> car);
...
};
.cpp
#include "person.h"
...
Person::Person(std::string name, std::string mail) : Person(name, mail, nullptr){};
Person::Person(std::string name, std::string, std::unique_ptr<Car> car) : name{name}, mail{mail}, car{std::move(car)}{};
...
main
#include "person.h"
int main(){
std::string name{"Ola Normann"};
std::string mail{"ola.normann#norge.no"};
std::unique_ptr<Car> car{new Car{5}};
Person a{name, mail, std::move(car)};
// alternatively:
// Person a{name, mail, std::make_unique<Car>(5)};
};

Why I get this random weird number C++

I am new to using pointers with c++, so I am trying this small code but the problem is that when i try to print name i get this random weird number \364\277\357\376\326\241+\310\364\277\357\376\310. This is not the memory address, which is confusing and what confuses me more that when i replace name with getName() it works perfectly and prints the name! Thanks you!!
Person.cpp
#include "Pesron.hpp"
#include <string>
#include <iostream>
using namespace std;
Person:: Person()
{
}
Person::Person(string Name, int Age)
{
name=&Name;
age=Age;
}
void Person:: setName(string Name)
{
name=&Name;
}
void Person:: setAge(int Age)
{
age=Age;
}
string Person:: getName()
{
return *name;
}
int Person:: getAge()
{
return age;
}
void Person:: display()
{
cout<<*name<<" "<<age<<" ";
}
Person::~Person()
{
}
Student.cpp
#include "Student.hpp"
Student:: Student(string Name, int Age,int Grades, int ID):Person(Name , Age)
{
grades=Grades;
id=ID;
}
void Student:: setId(int ID)
{
id=ID;
}
int Student:: getId()
{
return id;
}
void Student:: setGrades(int Grades )
{
grades= Grades;
}
int Student:: getGrades()
{
return grades;
}
void Student:: display()
{
Person::display();
cout<<grades<<" "<<id<<endl;
}
main.cpp
#include "Pesron.hpp"
#include "Student.hpp"
#include "graduteStudent.hpp"
#include <iostream>
int main(int argc, const char * argv[]) {
// insert code here...
Student student("ZAID",21,2211,11);
student.display();
return 0;
}
Output
\364\277\357\376\326\241+\310\364\277\357\376\310 21 2211 11
Person::name looks like it is a std::string *. In Person::Person(string Name, int Age) you pass the paramater Name by value and then store the address of this local variable in name. When Name goes out of scope you have a dangling pointer.
(This also applies to void Person::setName(string Name))
Dereferencing Person::name is undefined behaviour, because the object it is pointing doesn't exist anymore. The solution is to simply store a std::string and not just a pointer to it.
So you get something like
class Person {
private:
std::string name;
int age;
public:
Person(std::string Name, int Age) : name(Name), age(Age) {}
};

what is the difference between two types of defining constructors? first one using ":" and the second one defining it in curly braces like functions

I have problem in defining constructors for classes and the way I have defined them earlier, doesn't work here properly.
This is for edx c++ intermediate course. when I try to define a class that inherits from some other class, referring to my base class with a constructor doesn't work properly and so my new class won't be defined correctly. What's the difference between the first use of constructors in the first code (using ":" notation that works properly), and the second one in second code (defining like function, that I have used before properly and doesn't work here)?
I have a base class called Person and a Student class that inherits from the base class. When I try to initialize a Student object that calls one of Person class constructors, it gives wrong answers. I think this is because of the way I define constructors. I make them like functions and initialize variables in curl braces. I have used this method earlier and it was working properly, but it doesn't work here. But the standard method using ":" before curly braces works properly here. I want to know what is the difference between these two?
Person.h:
#pragma once
#include <string>
class Person
{
private:
std::string name;
protected:
int age;
public:
Person();
Person(const std::string & name, int age);
void displayNameAge() const;
};
Person.cpp:
#include "pch.h"
#include "Person.h"
#include <iostream>
//Person::Person()
// : name("[unknown name]"), age(0)
//{
// std::cout << "Hello from Person::Person()" << std::endl;
//}
Person::Person()
{
name = "[unknown name]";
age = 0;
std::cout << "Hello from Person::Person()" << std::endl;
}
Person::Person(const std::string & name, int age)
{
this->name = name;
this->age = age;
std::cout << "Hello from Person::Person(string, int)" << std::endl;
}
//Person::Person(const std::string & name, int age)
// : name(name), age(age)
//{
// std::cout << "Hello from Person::Person(string, int)" << std::endl;
//}
void Person::displayNameAge() const
{
std::cout << name << ", " << age << std::endl;
}
Student.h:
#pragma once
#include "Person.h"
class Student : public Person
{
private:
std::string course;
public:
Student();
Student(const std::string & name, int age, const std::string & course);
void displayCourse() const;
};
Student.cpp:
#include "pch.h"
#include "Student.h"
#include <iostream>
Student::Student()
{
course = "[unassigned course]";
std::cout << "Hello from Student::Student()" << std::endl;
}
// first method: the right one
//Student::Student(const std::string & name, int age, const std::string & course)
// : Person(name, age), course(course)
//{
// std::cout << "Hello from Student::Student(string, int, string)" << std::endl;
//}
// second method: the wrong one
Student::Student(const std::string & name, int age, const std::string & course)
{
Person(name, age);
this->course = course;
std::cout << "Hello from Student::Student(string, int, string)" << std::endl;
}
void Student::displayCourse() const
{
std::cout << course << std::endl;
}
Main.cpp:
#include "pch.h"
#include "Student.h"
int main()
{
// Create a Student object using the no-argument constructor.
Student Student1;
Student1.displayNameAge();
Student1.displayCourse();
// Create a Student object using the parameterized constructor.
Student Student2("Jane Smith", 25, "Physics");
Student2.displayNameAge();
Student2.displayCourse();
return 0;
}
expected result:
Hello from Person::Person()
Hello from Student::Student()
[unknown name], 0
[unassigned course]
Hello from Person::Person(string, int)
Hello from Student::Student(string, int, string)
Jane Smith, 25
Physics
actual result:
Hello from Person::Person()
Hello from Student::Student()
[unknown name], 0
[unassigned course]
Hello from Person::Person()
Hello from Person::Person(string, int)
Hello from Student::Student(string, int, string)
[unknown name], 0
Physics
Initializer List
What you are missing is the initializer list.
Type::Type(Parameters)
: member1(init) // This is the initializer list
, member2(init)
{
Your code
}
If you do not explicitly provide one then the compiler will do it for you using the default constructor for the parent class then a call to the default constructor for each member.
So lets look at your class.
Student::Student(const std::string & name, int age, const std::string & course)
{
// Code
}
That is what you wrote. But this is what the compiler implemented.
Student::Student(const std::string & name, int age, const std::string & course)
: Person()
, course()
{
// Code
}
So because you did not do anything the compiler added it calls to the Person default constructor and course (std::string) default constructor.
Now the problem comes if your base class does not have a default constructor. Then the compiler can not add the appropriate calls and will generate a compiler error.
But there is also the issue that the way you are writing this is very inefficient as you are basically initializing all the members twice. Your calling the default constructor then in the Code section you are re-initializing the members with another value.
Student::Student(const std::string & name, int age, const std::string & course)
: Person(name, age)
, course() // Initialize it to empty here.
{
course = "[unassigned course]"; // Re-Initialize it with info.
}
You could simply do it once:
Student::Student()
: Person() // Note person must have a default constructor for this to work.
, course("[unassigned course]")
{}
Temporary Object
Student::Student(const std::string & name, int age, const std::string & course)
{
Person(name, age);
// CODE.
}
This is not doing what you think.
Lets add the initializer list.
Student::Student(const std::string & name, int age, const std::string & course)
: Person()
, course()
{
Person(name, age); // This is creating a person object localy.
// the person object has no name so is a
// temporary variable and goes out of scope
// at the ';'. So it is created and destroyed
// in place before other code is executed.
//
// This does not help initialize the class.
// It is creating a completely different
// and independent object.
// CODE.
}
You can see the execution here:
Hello from Person::Person() // Init parent
Hello from Person::Person(string, int) // Init the temp object.
// If you put a print in the destructor
// You will also see that executed
// Before the Student
// This is also why the object has "unknown name" and 0 age.
Hello from Student::Student(string, int, string) // Now we init the student
[unknown name], 0
Physics
Advice
Is there a valid scenario where you want initialized versions of the object? Personally I think not (if there are then ignore this) so get rid of the default constructors of Person and Student. Then you can not create uninitialized Students or `People.

C++ Constructor Initilizer

I am trying to use constructor initializer for name and time but the visual studio is giving me errors, I do not see any issues with it, it seems fine to me, I tried to see the problem, I tried other things as well, Please Help. Thanks you in advance.
Any help really appreciated, I added all the header file and implementation of Entry class, I know it seems, I added so you can see it.
// Entry.h
#pragma once
#include <iostream>
#include <string>
#include "RelationType.h"
using namespace std;
class Name
{
public:
Name();
Name(string firstName, string middleName, string lastName);
string GetFristName() const;
string GetLastName() const;
string GetMiddleName() const;
char GetMiddleInitial() const;
RelationType ComparedTo(Name otherName) const;
private:
string first;
string last;
string middle;
};
//Entry.cpp
#include "Entry.h"
#include<iostream>
#include <string>
using namespace std;
Entry::Entry() {}
Entry::Entry(string firstName, string middleName, string lastName,
int initHours, int initMinutes, int initSeconds)
: name{ (firstName, middleName, lastName) , //name is where its mad at
time(initHours, initMinutes, initSeconds) } {}
string Entry::GetNameStr () const
{
return (name.GetFirstName() + ' ' + name.GetLastName());
}
string Entry::GetTimeStr () const
{
return (name.FirstName() + ' ' + name.LastName());
}
// Name.h
#pragma once
#include <iostream>
#include <string>
#include "RelationType.h"
using namespace std;
class Name
{
public:
Name();
Name(string firstName, string middleName, string lastName);
string GetFristName() const;
string GetLastName() const;
string GetMiddleName() const;
char GetMiddleInitial() const;
RelationType ComparedTo(Name otherName) const;
private:
string first;
string last;
string middle;
};
// RealtionType.h
#pragma once
#ifndef RELATION
#define RELATION
enum RelationType { BEFORE, SAME, AFTER };
#endif
// TimeOfDay.h
#pragma once
class TimeOfDay
{
public:
//Intentionally missed const, see what happened without const
TimeOfDay(); // zero timepfday object
TimeOfDay(int hours, int minutes, int seconds); //takes 3 parameters
TimeOfDay Increment() const; //increment by 1 sec
void Write() const; //write the timeofday obj to print
bool Equal(TimeOfDay otherTime) const; //true if timeofday obj equals othertime
bool LessThan(TimeOfDay otherTime) const; //const, true if the timeofday obj is
//before itherTime
private:
int hours;
int minutes;
int seconds;
};
Your Entry class constructor code should be something like below
Entry::Entry(string firstName, string middleName, string lastName,
int initHours, int initMinutes, int initSeconds)
: name(firstName, middleName, lastName)
, time(initHours, initMinutes, initSeconds) {}

Unable to use member function inside a class

I am currently learning about classes and am having a problem in my class implementation file.
In my class header/specification file Book.h , I have the public member function setPages.
#ifndef BOOK_H
#define BOOK_H
#include <string>
#include "Author.h"
#include "Publisher.h"
class Book
{
private:
std::string _title;
std::string _edition;
int _pages;
int _copyrightYear;
Author _author;
Publisher _publisher;
public:
Book (std::string title, Author author, Publisher publisher)
{_title = title; _author = author; _publisher = publisher;}
void setPages (int pages);
void setCopyYear (int copyrightYear);
void setEdition (std::string edition);
std::string getTitle () const;
std::string getEditon () const;
int getPages () const;
int getCopyYear () const;
Author getAuthor () const;
Publisher getPublisher () const;
};
#endif
In my Book.cpp implementation file I have
#include <string>
#include "Author.h"
#include "Publisher.h"
#include "Book.h"
void Book::setPages (int pages)
{
_pages = pages;
}
I keep getting the error that Book is not a classname or namespace but I don't see what I've done wrong. I included my Book header file and checked to make sure everything was spelled correctly in the class. I've done the same thing in my other classes and it is working so I don't see why this isn't.
Any help appreciated thanks.
Here is Publisher.h and Author.h
#ifndef PUBLISHER_H
#define PUBLISHER_H
class Publisher
{
private:
std::string _name;
std::string _address;
std::string _phoneNumber;
public:
Publisher (std::string& name)
{_name=name;}
void setAddress (std::string address);
void setNumber (std::string phoneNumber);
std::string getAddress () const;
std::string getNumber () const;
bool operator==(std::string name)
{
if (_name == name)
return true;
else
return false;
};
#endif
and Author.H
#ifndef AUTHOR_H
#define AUTHOR_H
class Author
{
private:
std::string _name;
int _numberOfBooks;
public:
Author(std::string& name)
{_name = name;}
void setNumOfBooks (int numberOfBooks);
int getNoOfBooks () const;
bool operator==(std::string _name)
{
if (this->_name == _name)
return true;
else
return false;
}
};
#endif
Until #ahenderson decides to turn his comments into an answer:
bool operator==(std::string name) in "Publisher.h" is missing a brace in your example. is that actually in your code or a copy and paste error?
bool operator==(std::string name)
{
if (_name == name)
return true;
else
return false;
Oops, no brace here!
Suggestion: Simplify your operator== method:
The expression _name == name will already return true or false. No need to put it into an if clause that returns true or false.
Try this:
bool operator==(const std::string& name)
{
return (_name == name);
}
In the above example, the expression is evaluated and the result is returned, directly.
Also, you may run into compiler issues if your variables begin with an underscore, '_'. Change your naming convention so this issue doesn't raise its ugly head. Two common practices are to append a suffix, name_, or prefixing with something like m_name.