Maybe it's because I've been staring at this for 5 hours straight and I'm just reading over the obvious solution, but Xcode states that my vector of objects (that is declared in a header file) is undeclared in one .cpp file; however, my other .cpp file can access the vector, so I'm not sure what the problem is. Maybe my header files are out of place, or I'm unintentionally "circular referencing"?? Suggestions please?
publications.h
#ifndef PUBLICATIONS_H
#define PUBLICATIONS_H
#include "std_lib_facilities.h"
class Publication {
private:
string
publication_title,
publication_author,
publication_year,
publication_genre,
publication_media,
publication_target_age,
publication_ISBN;
bool currently_checked_out;
public:
Publication(string title, string author, string year, string genre, string media, string target_age, string ISBN, bool checked_out) {
publication_title = title;
publication_author = author;
publication_year = year;
publication_genre = genre;
publication_media = media;
publication_target_age = target_age;
publication_ISBN = ISBN;
currently_checked_out = checked_out;
}
Publication(){};
};
#endif /* PUBLICATIONS_H */
library.h
#ifndef LIBRARY_H
#define LIBRARY_H
#include "std_lib_facilities.h"
class Publication;
class Library {
private:
vector<Publication> lb;
public:
Library(){};
void documentation();
void list_all_publications();
void new_publication(string title, string author, string year, string genre, string media, string target_age, string ISBN, bool currently_checked_out);
};
#endif /* LIBRARY_H */
patron.h
#ifndef PATRON_H
#define PATRON_H
#include "std_lib_facilities.h"
class Publication;
class Patron {
private:
string
customer_name,
customer_telephone;
public:
void check_out_publication(string publication_requested);
void return_publication(string check_in_publication_name);
void check_out();
bool is_checked_out();
Patron(){};
};
#endif /* PATRON_H */
library.cpp (works perfectly fine, can access vector in library.h)
#include "library.h"
#include "patron.h"
#include "publications.h"
#include "std_lib_facilities.h"
Patron p;
void Library::documentation() {
cout << "\n-----Create a new publication----\n";
cout << "You will enter all the necessary info for a new publication (title, author, year, genre, media, target age, and ISBN).\n";
cout << "\n----List all Publications-----\n";
cout << "List all publications that have been entered (in this current session).\n";
cout << "\n---Check out Publication----\n";
cout << "You will enter your name and telephone number and the publication will be checked out.\n";
cout << "\n-----Return Publication------\n";
cout << "A previously checked out publication will be marked as returned.\n";
}
void Library::new_publication(string title, string author, string year, string genre, string media, string target_age, string ISBN, bool checked_out) {
lb.push_back(Publication(title, author, year, genre, media, target_age, ISBN, checked_out));
}
void Library::list_all_publications() {
for (int i = 0; i < lb.size(); i++) {
cout << "Title: " << "\tChecked Out: " << p.is_checked_out() << endl;
}
}
patron.cpp (problematic file, cannot access vector in library.h)
#include "publications.h"
#include "library.h"
#include "patron.h"
#include "std_lib_facilities.h"
void Patron::check_out_publication(string publication_requested) {
// check to make sure publication isn't already checked out.
for (int i = 0; i < lb.size(); i++) {
if ((publication_requested == lb[i].publication_title) && lb[i].currently_checked_out) {
cout << "Sorry, this publication is currently checked out." << endl;
} else if ((publication_requested == lb[i].publication_title) && !(lb[i].currently_checked_out)) {
cout << "Enter your name: ";
getline(cin, customer_name);
cout << "Enter your telephone number (no dashes/no spaces): ";
cin >> customer_telephone;
} else {
continue;
}
}
}
void Patron::return_publication(string check_in) {
for (int i = 0; i < lb.size(); i++) {
if ((check_in == lb[i].publication_title) && lb[i].currently_checked_out) {
// marked as no longer checked out.
lb[i].currently_checked_out = false;
}
}
}
bool Patron::is_checked_out() {
return currently_checked_out;
}
void Patron::check_out() {
currently_checked_out = true;
}
Error in patron.cpp
USE OF UNDECLARED IDENTIFIER "lb"
lb is a private member of the Library class. You can access it in your library.cpp file because you are using it within a Library member function. Whereas in the Patron class you're directly accessing it. But the compiler sees it as just another variable, which you haven't declared. Likely you need to add an accessor to Library.
Related
I have a banking project and I am trying to set up the bank name, address, and working hours. My getlines are showing an error as well as my get functions.
Input exact error messages here please.
'getline': no matching overloaded function found
no suitable user-defined conversion from "Bank" to "std::string" exists
Here's the class for bank:
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <cctype>
#include <cstdlib>
using namespace std;
class Bank {
public:
void setBankName(string bn) { bn = bankname; }
string getBankName() { return bankname; }
void setBankAdd(string ba) { ba = bankadd; }
string getBankAdd() { return bankadd; }
void setWorkingHours(string bwh) { bwh = bankworkinghours; };
string getWorkingHours() { return bankworkinghours; }
private:
string bankname, bankadd, bankworkinghours;
};
//and then this is in my main function
int main() {
Bank bankname, bankadd, bankworkinghours;
char userChoice; // numbers 1-9
int number=0;
system ("color 5f");
cout << "Name of bank: ";
getline(cin, bankname); **//all the get lines also show error**
cout << endl;
cout << "Bank address: ";
getline(cin, bankadd);
cout << endl;
cout << "Bank working hours: ";
getline(cin, bankworkinghours);
cout << endl;
bankname.setBankName(bankname); //the things in the parentheses show error
bankadd.setBankAdd(bankadd);
bankworkinghours.setWorkingHours(bankworkinghours);
The error is self explanatory. 2nd parameter of getline function is std:string so define bankname as std:string and then set the name of bank object by setBankName.
1- You did not created bank Object in the main to set class attributes.
You need an Object with reference to that object you will set the parameters of the class bank.
2- bankname, bankadd, bankworkinghours are string and you made them Bank
Here is updated code and working fine in VS 2019 without any error. Just a few changes in the first 2 and last three lines of main
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <cctype>
#include <cstdlib>
using namespace std;
class Bank {
public:
void setBankName(string bn) { bn = bankname; }
string getBankName() { return bankname; }
void setBankAdd(string ba) { ba = bankadd; }
string getBankAdd() { return bankadd; }
void setWorkingHours(string bwh) { bwh = bankworkinghours; };
string getWorkingHours() { return bankworkinghours; }
private:
string bankname, bankadd, bankworkinghours;
};
//and then this is in my main function
int main() {
Bank bankObj;
string bankname, bankadd, bankworkinghours;
char userChoice; // numbers 1-9
int number = 0;
system("color 5f");
cout << "Name of bank: ";
getline(cin, bankname);
cout << endl;
cout << "Bank address: ";
getline(cin, bankadd);
cout << endl;
cout << "Bank working hours: ";
getline(cin, bankworkinghours);
cout << endl;
bankObj.setBankName(bankname);
bankObj.setBankAdd(bankadd);
bankObj.setWorkingHours(bankworkinghours);
}
void setBankName(string bn) { bn = bankname; } is the wrong way around. try bankname = bn.
I am tasked to create a Print function that prints user inputted data that is specific to an object. This print function must use the Get() Function commands I created.
I have googled and looked for similar questions but could not find a way of how I could approach this. How can I create this function my teacher wants?
The object I want to print specifically is book1
My code:
#include <iostream>
#include <string>
#include <cstdio>
using namespace std;
class Book {
public:
void SetTitle(string title_input);
string GetTitle();
void SetAuthor(string& author_input);
string GetAuthor();
void SetCopyRightYear(int copyright_year_input);
int GetCopyRightYear();
void PrintBook();
private:
string title;
string author;
int copyright_year;
};
void Book::SetTitle(string title_input) {
title = title_input;
}
string Book::GetTitle() {
return title;
}
void Book::SetAuthor(string& author_input) {
author = author_input;
}
string Book::GetAuthor() {
return author;
}
void Book::SetCopyRightYear(int copyright_year_input) {
copyright_year = copyright_year_input;
}
int Book::GetCopyRightYear() {
return copyright_year;
}
void Book::PrintBook() {
cout << "Title of Book: " << GetTitle() << endl;
cout << "Author of Book: " << GetAuthor() << endl; // Function is broken FIXME
cout << "Copyright Year: " << GetCopyRightYear() << endl;
}
int main ()
{
string title_input = "";
string author_input = "";
int copyright_year_input = 0;
Book book1;
Book book2;
Book book3;
Book book4;
cout << "Enter the book title: ";
cin >> title_input;
book1.SetTitle(title_input);
cout << book1.GetTitle();
cout << "Enter the author name: ";
cin >> author_input;
book1.SetAuthor(author_input);
cout << "Enter the copyright year: ";
cin >> copyright_year_input;
book1.SetCopyRightYear(copyright_year_input);
cout << PrintBook();
Book.h
#pragma once
#include <string>
class Book
{
public:
Book() = default;
~Book() = default;
const std::string GetTitle() const;
const std::string GetAuthor() const;
const int GetCopyRightYear() const;
void SetTitle(const std::string);
void SetAuthor(const std::string);
void SetCopyRightYear(const int);
void PrintBook();
private:
std::string title;
std::string author;
int copyright_year;
};
Book.cpp
#include "Book.h"
// ------------------------------
#include <iostream>
void Book::SetTitle(const std::string title_input)
{
title = title_input;
}
const std::string Book::GetTitle() const
{
return title;
}
const int Book::GetCopyRightYear() const
{
return copyright_year;
}
const std::string Book::GetAuthor() const
{
return author;
}
void Book::SetCopyRightYear(const int copyright_year_input)
{
copyright_year = copyright_year_input;
}
void Book::SetAuthor(const std::string author_input)
{
author = author_input;
}
void Book::PrintBook()
{
std::string output_str = "";
std::cout << "Title of Book: " << GetTitle() << std::endl;
std::cout << "Author of Book: " << GetAuthor() << std::endl;
std::cout << "Copyright Year: " << GetCopyRightYear() << std::endl;
}
main.cpp
// C++ Libraries.
#include <iostream>
#include <string>
// User classes
#include "Book.h"
// Namespaces
int main()
{
std::string title_input = "";
std::string author_input = "";
int copyright_year_input = 0;
// research dynamic memory allocation.
Book book1;
Book book2;
Book book3;
Book book4;
// user sets book title.
std::cout << "Enter the book title: ";
std::getline(std::cin, title_input);
book1.SetTitle(title_input);
// user sets the authors name
std::cout << "Enter the author name: ";
std::getline(std::cin, author_input);
book1.SetAuthor(author_input);
// user inputs the copyright year.
std::cout << "Enter the copyright year: ";
std::cin >> copyright_year_input;
book1.SetCopyRightYear(copyright_year_input);
// Display the information.
book1.PrintBook();
}
Notes:
When you start using multiple namespaces its easier to see what is what if you dont predefine them.
Const correctness means you and other developers know what can be changed and what cant. It also makes things clearer for the compiler.
std::getline reads the whole line including the blank spaces.
Just a quick note on clarity and understanding. At the moment your code is messy which makes it incredibly hard to debug not only for yourself but for others.
I can't tell on here but just in case, your classes should be in header and source code formatting, with a main source code file for the main function (entry point). Whether or not you've been told this information before I would highly recommend doing some research into basic C++. Just for starters I've put some links below to help. Once your code is neatly formatted you might work out what the problem is.
Happy coding :)
References:
Herb Sutter Cpp Convention 2014 - Simplicity over Complexity:
https://www.youtube.com/watch?v=xnqTKD8uD64
Headers and Includes - C++ formatting:
http://www.cplusplus.com/forum/articles/10627/
Also see the tutorials on cplusplus.com.
I have this requirement.
I am trying to make a simple database schema, a little different than what I have seen in here. I have a class file (client.h) with it's implementation (client.cpp):
#ifndef CLIENT_H_
#define CLIENT_H_
#include <iostream>
using namespace std;
class Client {
public:
// constructors
Client();
Client(string new_name, string new_tel, string new_addr);
// getters
string getName();
string getAddr();
string getTel();
// setters
void setName(string);
void setAddr(string);
void setTel(string);
void display();
void input();
private:
// fields
string name;
string addr;
string tel;
};
#endif /* CLIENT_H_ */
/*
*ad client.cpp
*
* Created on: Jan 12, 2017
* Author: niksarid
*/
#include <iostream>
#include "client.h"
using namespace std;
Client::Client() {
setName("");
setAddr("");
setTel("");
}
Client::Client(std::string new_name, std::string new_addr, std::string new_tel) {
setName(new_name);
setAddr(new_addr);
setTel(new_tel);
}
string Client::getName() {
return name;
}
string Client::getAddr() {
return addr;
}
string Client::getTel() {
return tel;
}
void Client::setName(string p_name) {
name = p_name;
}
void Client::setAddr(string p_addr) {
addr = p_addr;
}
void Client::setTel(string p_tel) {
tel = p_tel;
}
void Client::input() {
string tmp;
cout << "INPUT CLIENT INFO" << endl;
cout << "Name: ";
cin >> tmp;
setName(tmp);
cout << "Address: ";
cin >> tmp;
setAddr(tmp);
cout << "Telephone: ";
cin >> tmp;
setTel(tmp);
}
void Client::display() {
cout << name << "\t" << addr << "\t" << tel << endl;
}
So I am trying to make a Company class that will hold a vector of Clients and at the startup of the program it will load the datafile "clients.dat", into the vector. I will be able to add a client or delete a client from the vector. At the end the vector will be saved back to "clients.dat".
So, the (company.h) file is like this:
class Company {
public:
Company();
~Company();
void add_client();
void print_clients();
void loadClientsFromFile();
void saveClientsToFile();
private:
vector<Client> clients;
} cmp;
#endif /* COMPANY_H_ */
but I can't seem to reference clients vector in any of the public methods of the class company.
EDIT: Sorry! Forgot the important part!!
For example when I try to add_client(),
void add_client() {
Client c;
c.input();
clients.push_back(c);
}
but I get
../src/company.cpp:49:2: error: 'clients' was not declared in this scope
clients.push_back(c);
So, how to achieve that?
As Morgan mentioned in the comments, this problem typically arises when you try to define the member function in your implementation file, but forget to add the class name as prefix (e.g. void add_client() {} instead of void Company::add_client() {}.
This mistake is common and can easily go unnoticed, since it is perfectly legal to define a new free function called add_client in your file, that would have nothing to do with the Company class. That's why the compiler only complains when you try to access a data member, but not before.
I have to create a small console application in C++ which will do the following:
Create class Subject which has next attributes: name of the subject, number of students and array of students who are attending that subject. After that createa class Student which has name and surname of the student as attributes. In Main file count how many duplicate names there are in each Subject.
I have few problems here. First one is that I don't know how to initialize an array in my Subject.h file. Second is how to actually put Student objects into Subject objects and compare the names in the end.
What I'd like output to look like:
Duplicate names in subjectA are: Michael.
Duplicate names in subjectB are: Nicholas, John.
Where subjectA and subjectB should be called C++ and C.
Here is my code so far (I googled for past hour or two about this problem of mine but I just couldn't find a proper answer/example).
NOTE: I'm including all these files for clarification.
Subject.h
#include <string>
#include <iostream>
using namespace std;
/*
* I should also have an array named `arrayOfStudents`
* which should store all students who are attending
* that Subject.
*/
class Subject
{
public:
Subject();
Subject(Subject &subject);
Subject(string nameOfSubject, int numberOfStudents);
~Subject();
const string getNameOfSubject();
const int getNumberOfStudents();
void setNameOfSubject(string nameOfSubject);
void setNumberOfStudents(int numberOfStudents);
void print();
private:
string nameOfSubject;
int numberOfStudents;
};
Subject.cpp
#include <iostream>
#include "Subject.h"
using namespace std;
Subject::Subject()
{
}
Subject::Subject(string nameOfSubject, int numberOfStudents)
{
this->nameOfSubject = nameOfSubject;
this->numberOfStudents = numberOfStudents;
}
Subject::Subject(Subject &Subject) : nameOfSubject(Subject.getNameOfSubject()), numberOfStudents(Subject.getNumberOfStudents())
{
}
Subject::~Subject()
{
cout << "Object is destroyed" << endl;
}
const string Subject::getNameOfSubject()
{
return nameOfSubject;
}
const int Subject::getNumberOfStudents()
{
return numberOfStudents;
}
void Subject::setNameOfSubject(string nameOfSubject)
{
nameOfSubject = this->nameOfSubject;
}
void Subject::setNumberOfStudents(int numberOfStudents)
{
numberOfStudents = this->numberOfStudents;
}
void Subject::print()
{
cout << "Subject: " << nameOfSubject << " :: Number of students: " << numberOfStudents << endl;
}
Student.h
#include <string>
#include <iostream>
using namespace std;
class Student
{
public:
Student();
Student(Student &student);
Student(string name, string surname);
~Student();
const string getName();
const string getSurname();
void setName(string name);
void setSurname(string surname);
void print();
private:
string name;
string surname;
};
Student.cpp
#include <iostream>
#include "Student.h"
using namespace std;
Student::Student()
{
}
Student::Student(string name, string surname)
{
this->name = name;
this->surname = surname;
}
Student::Student(Student &student) : name(student.getName()), surname(student.getSurname())
{
}
Student::~Student()
{
cout << "Object is destroyed" << endl;
}
const string Student::getName()
{
return name;
}
const string Student::getSurname()
{
return surname;
}
void Student::setName(string name)
{
name = this->name;
}
void Student::setSurname(string surname)
{
surname = this->surname;
}
void Student::print()
{
cout << "Student: " << name << " " << surname << endl;
}
Main.cpp
#include <iostream>
#include "Subject.h"
#include "Student.h"
using namespace std;
int main()
{
/*
* First three students should attend first Subject
* while other four the second Subject.
* Also note that only names matter and not surnames.
*/
Student stA("Michael", "Doe");
Student stB("Michael", "Doe");
Student stC("Thomas", "Doe");
Student stD("Nicholas", "Doe");
Student stE("Nicholas", "Doe");
Student stF("John", "Doe");
Student stG("John", "Doe");
Subject subjectA("C++", 3);
Subject subjectB("C", 4);
return 0;
}
1) Get an array of Students into your Subject object: you probably want to use vectors instead of arrays here:
in subject.h add
#include "Student.h"
public:
void addStudent(Student student);
private:
std::vector<Student> students_;
in subject.cpp add
void Subject::addStudent(Student student)
{
this->students_.push_back(student);
}
If you want to extract the student list somehow later, you need to write a function to access it (or make it public).
2) For finding duplicates, look here
Checking for duplicates in a vector
You have to pay attention: the Student objects are in your subject object, not the student names. You have to extract them first and e.g. put them in a vector.
Your task definition sais you should have an array of Students attribute of the Subject class, but i don't see this in your Subject class definition.
And maybe an add Student method and then iterate over the array.
Good afternoon!
Sorry if the question seems rather vague, but here's some (incomplete) code for some context. Specifically, this is about the "UserInfo inputInfo" definition part as seen in the functions UserInfo::setUserInfo() and UserInfo::displayProfile() in the implementation file.
project02.cpp (implementation file)
#include <iostream>
#include "project02.h"
using namespace std;
void UserInfo::setUserInfo()
{
UserInfo inputInfo;
string fName;
string lName;
int bYear;
string city;
string occupation;
cout << "Please enter your first name: ";
cin >> fName;
inputInfo.setFirstName(fName);
cout << "Please enter your last name: ";
cin >> lName;
inputInfo.setLastName(lName);
cout << "You are now registered as: " << inputInfo.getFirstName() << " " << inputInfo.getLastName();
}
void UserInfo::displayProfile()
{
UserInfo inputInfo;
cout << "Profile Information:" << endl;
cout << "Name: " << inputInfo.getFirstName() << " " << inputInfo.getLastName();
}
void UserInfo::setFirstName(string fName)
{
_firstName = fName;
}
string UserInfo::getFirstName()
{
return _firstName;
}
void UserInfo::setLastName(string lName)
{
_lastName = lName;
}
string UserInfo::getLastName()
{
return _lastName;
}
project02.h (header file)
#ifndef PROJECT02_H
#define PROJECT02_H
using namespace std;
class UserInfo
{
public:
string getFirstName();
void setFirstName(string first);
string getLastName();
void setLastName(string last);
int getBirthYear();
void setBirthYear(int year);
string getCurrentCity();
void setCurrentCity(string city);
string getOccupation();
void setOccupation(string occ);
void setUserInfo();
void displayProfile();
private:
string _firstName;
string _lastName;
int _birthYear;
string _currentCity;
string _occupation;
};
#endif // PROJECT02_H
project02main.cpp (main file)
#include <iostream>
#include "project02.h"
using namespace std;
int main()
{
UserInfo inputInfo;
inputInfo.setUserInfo();
return 0;
}
Now the question is: is there an alternative to repeatedly defining the object "UserInfo inputInfo;" each time for a different function in the implementation file?
Don't create objects of the same data type within methods of that datatype at this point - simply call setFirstname() and getFirstname() to use methods that will modify the same object that you are currently using.