Class Constructor error, class "classname" has no member "classname" - c++

#ifndef RESERVATIONS_H_INCLUDED
#define RESERVATIONS_H_INCLUDED
#include <vector>
#include <string.c>
class Reservations
{
public:
Reservations::Reservations { }
Reservations(string FullName, int PhoneNum);
string getname() { return FullName; }
int getnumber() { return PhoneNum; }
private:
string FullName;
int PhoneNum;
}
#endif // RESERVATIONS_H_INCLUDED
Reservations::Reservations(string FullName, int PhoneNum) //error on this line
{
FullName = FullName;
PhoneNum = PhoneNum;
}
I get the error in the title, I don't know why it's assuming I want it to have a member of it's own class...

you included wrong header file
Change
#include <string.c>
to
#include <string>
Also use string with full namespace std.
below code should compile with minor fix:
class Reservations
{
public:
Reservations() : PhoneNum(0) {}
Reservations(std::string FullName, int PhoneNum);
std::string getname() { return FullName; }
int getnumber() { return PhoneNum; }
private:
std::string FullName;
int PhoneNum;
};
Reservations::Reservations(std::string FullName, int PhoneNum)
{
this->FullName = FullName;
this->PhoneNum = PhoneNum;
}
// Better use member initializers list
Reservations::Reservations(std::string FullName, int PhoneNum)
: FullName(FullName),
PhoneNum(PhoneNum)
{
}

Do you mean
Reservations::Reservations() { }
instead of
Reservations::Reservations { }
?

Do this:
class Reservations
{
public:
//Reservations::Reservations { }
Reservations() { }
....

Related

Linking different classes in different files-symbol(s) not found for architecture x86_64-

I am trying to use the "UserAccType" in "UserAccountListType" but the code keeps giving me this error: symbol(s) not found for architecture x86_64. I have tried to use forward declaration and it gave me another error: "field has incomplete type".
how to fix this problem and thanks in advance.
Here is a piece of the code:
//UserAccountListType.h
#ifndef USERACCOUNTLISTTYPE_H
#define USERACCOUNTLISTTYPE_H
#include <iostream>
class UserAccType;
class UserAccountNode;
class UserAccountListType
{
private:
UserAccountNode *first;
UserAccountNode *last;
int counter;
public:
UserAccountListType()
{
first = last = NULL;
counter = 0;
}
void destroyList();
void print() const;
void insertUserAccount(const UserAccType newItem);
void deleteUserAccount(std::string ud);
void printInfoOfUserId(std::string userId);
bool isUserIdExist(std::string ud);
~UserAccountListType()
{
destroyList();
}
};
#endif
//UserAccType.h
#include <iostream>
#ifndef USERACCTYPE_H
#define USERACCTYPE_H
class UserAccType
{
private:
std::string userId;
std::string password;
std::string firstName;
std::string lastName;
std::string encryptedPassword;
public:
UserAccType(std::string userId = "", std::string password = "", std::string firstName = "", std::string lastName = "")
{
this->firstName = firstName;
this->lastName = lastName;
this->password = password;
this->userId = userId;
}
void setUserId(std::string);
void setpassword(std::string);
void setFirstName(std::string);
void setLastName(std::string);
std::string getUserId();
bool isCompleteUserACC();
std::string encryptPassword(std::string);
void printUserInfo();
bool isValidPassword(std::string Password);
};
#endif

Class does not persist vector of objects after addition

My code is as shown below. The problem is inside the int main() function, r.printItems() is not printing anything. What am I missing here?
main.cpp
#include <bits/stdc++.h>
#include "Customer.h"
#include "MenuCreator.h"
#include "FoodItem.h"
MenuCreator m1;
void createCustomer() {
Customer c1("mrg", "m#gmail.com", "9654357", "+91");
}
void createRestaurantItem(Restaurant &rest) {
rest.addItems(FoodItem("t1"));
rest.addItems(FoodItem("D1"));
}
void createMenu() {
m1.createMenu("sg");
Category c1;
c1.setName("Non-veg");
m1.addCategory(c1);
Restaurant r1;
r1.setName("ABC");
r1.setDescription("Test Restaurant");
createRestaurantItem(r1);
c1.addRestaurants(r1);
}
vector<Restaurant> getRestaurantsForCategory(string category) {
return m1.getRestaurantsForCategories(category);
}
int main() {
createCustomer();
createMenu();
for (auto r: getRestaurantsForCategory("Non-veg")) {
r.printItems();
}
return 0;
}
MenuCreator.h
#include <bits/stdc++.h>
#include "Menu.h"
using namespace std;
class MenuCreator {
public:
void createMenu(string name) {
Menu m1;
m1.setName(name);
menu = m1;
}
void addCategory(const Category &categ) {
categories.push_back(categ);
}
const Menu &getMenu() const {
return menu;
}
const vector<Category> &getCategories() const {
return categories;
}
void addRestaurantForCategory(string name, const Restaurant restaurant) {
for(auto categ: categories) {
if (categ.getName() == name) {
categ.addRestaurants(restaurant);
}
}
}
const vector<Restaurant> &getRestaurantsForCategories(string category) {
for(auto categ: categories) {
if(categ.getName() == category) return categ.getRestaurants();
}
}
private:
Menu menu;
vector<Category> categories;
};
Menu.h
#include<bits/stdc++.h>
#include "Category.h"
using namespace std;
class Menu {
public:
const string &getName() const {
return name;
}
void setName(const string &name) {
Menu::name = name;
}
private:
string name;
string description;
vector<Category> categories;
};
Category.h
using namespace std;
class Category {
public:
const string &getName() const {
return name;
}
void setName(const string &name) {
Category::name = name;
}
const string &getDescription() const {
return description;
}
void setDescription(const string &description) {
Category::description = description;
}
const vector<Restaurant> &getRestaurants() const {
return restaurants;
}
void setRestaurants(const vector<Restaurant> &restaurants) {
Category::restaurants = restaurants;
}
void addRestaurants(const Restaurant &rt) {
Category::restaurants.push_back(rt);
}
private:
string name;
string description;
vector<Restaurant> restaurants;
};
FoodItem.h
#include <bits/stdc++.h>
#include <vector>
#include "FoodItem.h"
using namespace std;
class Restaurant {
public:
Restaurant() {
this->id = gen_random(12);
}
virtual ~Restaurant() {
}
const string &getName() const {
return name;
}
void setName(const string &name) {
Restaurant::name = name;
}
const string &getDescription() const {
return description;
}
void setDescription(const string &description) {
Restaurant::description = description;
}
double getLat() const {
return lat;
}
void setLat(double lat) {
Restaurant::lat = lat;
}
double getLang() const {
return lang;
}
void setLang(double lang) {
Restaurant::lang = lang;
}
const string &getImageUrl() const {
return imageUrl;
}
void setImageUrl(const string &imageUrl) {
Restaurant::imageUrl = imageUrl;
}
const string &getVideoUrl() const {
return videoUrl;
}
void setVideoUrl(const string &videoUrl) {
Restaurant::videoUrl = videoUrl;
}
const vector<FoodItem> &getItems() const {
return items;
}
void setItems(const vector<FoodItem> &items) {
Restaurant::items = items;
}
void addItems(const FoodItem &item) {
this->items.push_back(item);
}
string gen_random(const int len) {
string tmp_s;
static const char alphanum[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
srand( (unsigned) time(NULL) * getpid());
tmp_s.reserve(len);
for (int i = 0; i < len; ++i)
tmp_s += alphanum[rand() % (sizeof(alphanum) - 1)];
return tmp_s;
}
const string &getId() const {
return id;
}
void printItems() {
for(auto it: items) {
cout<<"item: "<<it.getName()<<endl;
}
}
private:
string id;
string name;
string description;
double lat;
double lang;
string imageUrl;
string videoUrl;
string createdAt;
string updatedAt;
vector<FoodItem> items;
};
Restaurant.h
#include <bits/stdc++.h>
#include <vector>
#include "FoodItem.h"
using namespace std;
class Restaurant {
public:
Restaurant() {
this->id = gen_random(12);
}
virtual ~Restaurant() {
}
const string &getName() const {
return name;
}
void setName(const string &name) {
Restaurant::name = name;
}
const string &getDescription() const {
return description;
}
void setDescription(const string &description) {
Restaurant::description = description;
}
double getLat() const {
return lat;
}
void setLat(double lat) {
Restaurant::lat = lat;
}
double getLang() const {
return lang;
}
void setLang(double lang) {
Restaurant::lang = lang;
}
const string &getImageUrl() const {
return imageUrl;
}
void setImageUrl(const string &imageUrl) {
Restaurant::imageUrl = imageUrl;
}
const string &getVideoUrl() const {
return videoUrl;
}
void setVideoUrl(const string &videoUrl) {
Restaurant::videoUrl = videoUrl;
}
const vector<FoodItem> &getItems() const {
return items;
}
void setItems(const vector<FoodItem> &items) {
Restaurant::items = items;
}
void addItems(const FoodItem &item) {
this->items.push_back(item);
}
string gen_random(const int len) {
string tmp_s;
static const char alphanum[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
srand( (unsigned) time(NULL) * getpid());
tmp_s.reserve(len);
for (int i = 0; i < len; ++i)
tmp_s += alphanum[rand() % (sizeof(alphanum) - 1)];
return tmp_s;
}
const string &getId() const {
return id;
}
void printItems() {
for(auto it: items) {
cout<<"item: "<<it.getName()<<endl;
}
}
private:
string id;
string name;
string description;
double lat;
double lang;
string imageUrl;
string videoUrl;
string createdAt;
string updatedAt;
vector<FoodItem> items;
};
When createMenu() in main.cpp is setting up the menu, it creates a local Category object named c1 and adds it to the menu, which makes a copy of c1 when pushingnit into the MenuCreator::categories vector. No Restaurant objects had been added to c1 yet when that copy is created. So when a Restaurant is then added to c1 afterwards, the copy is not updated. That is why there are no Restaurant objects in the menu when MenuCreator::getRestaurantsForCategory() tries to return them.
Change createMenu() to fully initialize c1 before adding it to the menu, eg:
void createMenu() {
m1.createMenu("sg");
Category c1;
c1.setName("Non-veg");
Restaurant r1;
r1.setName("ABC");
r1.setDescription("Test Restaurant");
createRestaurantItem(r1);
c1.addRestaurants(r1);
m1.addCategory(c1); // <-- move down here
}
Also, on a side note, the return value of MenuCreator::getRestaurantsForCategories() is undefined if the specified category is not found. Since the return value is a reference to a vector, it needs to either return a static vector that is empty, or else throw an exception.

error c2084 on all my methods but each method only has one body

Only one of my classes are having this problem, it keeps saying error c2084, see previous definition which takes me to the same spot where the error is.
Here's my .h file
//PersonalInfo header file
#ifndef PERSONALINFO_H
#define PERSONALINFO_H
#include <string>
class PersonalInfo {
private:
std::string firstName;
std::string lastName;
int age;
std::string phoneNumber;
public:
//default constructor
PersonalInfo();
//copy constructor
PersonalInfo(const PersonalInfo&) = default;
//getters
std::string getFirstName();
std::string getLastName();
int getAge();
std::string getPhoneNumber();
//setters
void setFirstName(std::string name);
void setLastName(std::string name);
void setAge(int num);
void setPhoneNumber(std::string number);
};
#include "PersonalInfo.cpp"
#endif
Here's my .cpp, as you can see, they are only defined once but the error says they are defined already.
This website is making me add more info to post but there is no more info on this error to give so now im just typing until it lets me post
#include "PersonalInfo.h"
//default constructor
PersonalInfo::PersonalInfo() {
firstName = "";
lastName = "";
age = -1;
phoneNumber = "";
}
//getters
std::string PersonalInfo::getFirstName() {
return firstName;
}
std::string PersonalInfo::getLastName() {
return lastName;
}
int PersonalInfo::getAge() {
return age;
}
std::string PersonalInfo::getPhoneNumber() {
return phoneNumber;
}
//setters
void PersonalInfo::setFirstName(std::string name) {
firstName = name;
}
void PersonalInfo::setLastName(std::string name) {
lastName = name;
}
void PersonalInfo::setAge(int num) {
age = num;
}
void PersonalInfo::setPhoneNumber(std::string number) {
phoneNumber = number;
}
Remove the #include "PersonalInfo.cpp" at the end of the .h file. You're creating a circular include chain between two files and it makes everything duplicated. Your .cpp files should #include your .h files, not the other way around.

Errors in class relationships in c++

I am quite new to the concept of class relationships and wrote a program to test it out. However, it is giving me some errors.
There are 5 header files for classes University,Dept,Teacher,Student and Course and a .cpp file involved. I have implemented composition between University and Dept and bidirectional association between Dept and Student, Teacher, Course
uni.h
#pragma once
//#include"dept.h"
class University
{
public:
University(string,int);
void setDepartmentName(string,int);
Dept* getDeptAddress(int);
~University();
private:
Dept* departments;
int totalDepts;
string name;
};
University::University(string name1,int num) :name(name1)
{
departments = new Dept[num];
totalDepts=num;
}
void University::setDepartmentName(string name,int depNo)
{
departments[depNo].setName(name);
}
Dept* University::getDeptAddress(int i)
{
return &(departments[i]);
}
University::~University()
{
delete[] departments;
}
dept.h
#pragma once
//class Student;
//class Teacher;
//class Course;
#include"course.h"
#include"teacher.h"
#include"student.h"
class Dept
{
public:
Dept();
string getName();
void setName(string);
~Dept();
private:
Student** students;
Course** courses;
Teacher** teachers;
string name;
int noOfStudents, noOfTeachers, noOfCourses;
};
Dept::Dept()
{
}
string Dept::getName() {
return name;
}
void Dept::setName(string name1) {
name = name1;
}
Dept::~Dept()
{
}
course.h
#pragma once
class Dept;
//#include"dept.h"
class Course
{
public:
Course(string, string);
void assignDept(Dept*);
string getDeptName();
~Course();
private:
Dept* department;
string code;
string name;
};
Course::Course(string code1, string name1) :code(code1), name(name1)
{}
void Course::assignDept(Dept * dep)
{
department = dep;
}
string Course::getDeptName()
{
//statement giving error: 'Use of undefined type 'Dept'' & 'left of -> must point to class'
return department->getName();
}
Course::~Course()
{}
student.h
#pragma once
#include"dept.h"
class Student
{
public:
Student(string,string);
void assignDept(Dept*);
string getDeptName();
~Student();
private:
Dept* department;
string rollNo, name;
};
Student::Student(string rNo,string name1):name(name1),rollNo(rNo)
{}
void Student::assignDept(Dept *dep)
{
department = dep;
}
string Student::getDeptName()
{
//statement giving error: 'Use of undefined type 'Dept'' & 'left of -> must point to class'
return department->getName();
}
Student::~Student()
{
}
teacher.h
#pragma once
//#include"dept.h"
class Teacher
{
public:
Teacher(string);
void assignDept(Dept*);
string getDeptName();
~Teacher();
private:
Dept* department;
string name;
};
Teacher::Teacher(string name1) :name(name1)
{}
void Teacher::assignDept(Dept *dep)
{
department = dep;
}
string Teacher::getDeptName()
{
//statement giving error: 'Use of undefined type 'Dept'' & 'left of -> must point to class'
return department->getName();
}
Teacher::~Teacher()
{
}
source.cpp
#include<iostream>
using namespace std;
#include<string>
#include"dept.h"
#include"course.h"
#include"teacher.h"
#include"student.h"
#include"uni.h"
int main()
{
University u1("FAST",3);
u1.setDepartmentName("CS", 0);
u1.setDepartmentName("EE", 1);
u1.setDepartmentName("CV", 2);
Student s1("l144049", "Syed Abdul Wahab");
Course c1("cs1", "ITC");
Teacher t1("abc");
c1.assignDept(u1.getDeptAddress(0));
t1.assignDept(u1.getDeptAddress(1));
s1.assignDept(u1.getDeptAddress(2));
cout << c1.getDeptName()<<endl;
cout << t1.getDeptName() << endl;
cout << s1.getDeptName() << endl;
cin.get();
return 0;
}
However, if i #include 'dept.h' in student.h, course.h and teacher.h, it gives me errors on 'Dept' namely 'identifier 'Dept' is undefined'.
Any help would me greatly appreciated!
The problem is you have a circular dependency: teacher.h includes dept.h which includes teacher.h. This can't work.
To fix it, use "forward declarations" in your header files, and move your implementations to .cpp files.

No member named '' in ''. Why did it happen and how do I fix it?

I have an error when I print my PrintSount() in the main. The compiler said:
No member named 'PrintSound' in 'animal')
Why is this happening and how I can fix it?
main
#include <iostream>
#include "animal.h"
#include "pat.h"
#include "not_pat.h"
int main()
{
std::string lastName;
std::string sound;
std::string name;
std::string type;
double speed = 0;
animal* animals[2];
for (int i = 0; i < 2; i++)
{
std::string ani;
std::cout << "Enter animal: (dog,fish,lion,monkey ... ): " << std::endl;
std::cin >> ani;
if (ani == "dog" || ani == "cat" || ani == "fish")
{
animals[i] = new pat("test", "test", "test","test");
}
else
{
animals[i] = new not_pat(speed, lastName, "test2", "test2");
}
}
for (int i = 0; i < 2; i++)
{
std::cout << animals[i]->PrintName() << animals[i]->PrintLatName() << animals[i]- >PrintType() << animals[i]->PrintSound() << std::endl;
}
}
animal.h
#ifndef ANIMAL_H
#define ANIMAL_H
#include <iostream>
#include <stdio.h>
#include <string>
class animal{
public:
animal(std::string name, std::string type, std::string lastName);
std::string PrintType();
std::string PrintName();
std::string PrintLatName();
protected:
std::string _name;
std::string _lastName;
std::string _type;
private:
};
#endif
animal.cpp
#include "animal.h"
animal::animal(std::string name , std::string type, std::string lastName)
{
_name = name;
_type = type;
_lastName = lastName;
}
std::string animal::PrintType()
{
return this->_type;
}
std::string animal::PrintName()
{
return this->_name;
}
std::string animal::PrintLatName()
{
if(_lastName == "0")
{
return NULL;
}
else
{
return this->_lastName;
}
}
pat.h
#ifndef PAT_H
#define PAT_H
#include "animal.h"
#include <iostream>
#include <stdio.h>
#include <string>
class pat : public animal
{
public:
pat(std::string lastName, std::string sound, std::string name, std::string type);
std::string PrintSoud(animal *p);
protected:
std::string _sound;
private:
};
#endif
pat.cpp
#include "pat.h"
#include "animal.h"
pat::pat(std::string lastName, std::string sound, std::string name, std::string type) : animal(name,type,lastName)
{
_sound = sound;
}
std::string pat::PrintSoud(animal *p)
{
return this->_sound;
}
not_pat.h
#ifndef NOT_PAT_H
#define NOT_PAT_H
#include <iostream>
#include <stdio.h>
#include <string>
class not_pat : public animal
{
public:
not_pat(double speed,std::string lastName, std::string name, std::string type);
double PrintSpeed();
protected:
double _speed;
private:
};
#endif
not_pat.cpp
#include "animal.h"
#include "not_pat.h"
not_pat::not_pat(double speed, std::string lastName, std::string name, std::string type) : animal(name, type,lastName)
{
if(speed == 0)
{
_speed = NULL;
}
else
{
_speed = speed;
}
}
double not_pat::PrintSpeed()
{
return this->_speed;
}
C++ is a statically-typed language. You use pat using a pointer to animal. So the compiler checks if animal has the member function PrintSound(). It does not have it, so there is a compilation error raised. You need to add the declaration of PrintSound to animal (probably a pure virtual function) to fix this.