print() not a member of class? - c++

I am trying to run a getter/setter code in C++ Visual Studio 2008 and have my header file, implementation file and main file. In the main, there is a print function (P1.print();) which tries to print the class object P1. I get error of print() not a member of Persontype. When I declare void print(); in header, I get 3 errors -----
Persontest.obj : error LNK2005: _main already defined in proj1.obj -----
Persontest.obj : error LNK2019: unresolved external symbol "public: void __thiscall
Persontype::print(void)" (?print#Persontype##QAEXXZ) referenced in function _main ----- fatal error LNK1120: 1 unresolved externals.
Can someone pls help me resolve this issue?
Persontype.h(header file):
#ifndef H_Persontype
#define H_Persontype
#include <iostream>
#include <string>
using namespace std;
class Persontype{
public:
Persontype();
Persontype(string fn, string mn, string ln, char g);
//setter
void setfirstName(string fn);
void setmiddleName(string mn);
void setlastName(string ln);
void setGender(char g);
//getter
string getfirstName() const;
string getlastName() const;
string getmiddleName() const;
char getGender() const;
private:
string firstName;
string middleName;
string lastName;
char gender;
};
#endif/
Person.cpp(implementation file):
#include <iostream>
#include <string>
#include "Persontype.h"
using namespace std;
//default constructor
Persontype::Persontype()
{
firstName = "Me";
middleName = "My";
lastName = "Mine";
gender = 'X';
}
//specific constructor
Persontype::Persontype(string fn, string mn, string ln, char g){
firstName = fn;
middleName = mn;
lastName = ln;
gender = g;
}
//setters
void Persontype::setfirstName(string fn)
{
firstName = fn;
}
void Persontype::setmiddleName(string mn)
{
middleName = mn;
}
void Persontype::setlastName(string ln)
{
lastName = ln;
}
void Persontype::setGender(char g)
{
gender = g;
}
//getters
string Persontype::getfirstName () const
{
return firstName;
}
string Persontype::getmiddleName () const
{
return middleName;
}
string Persontype::getlastName () const
{
return lastName;
}
char Persontype::getGender() const
{
return gender;
}
Persontest.cpp(main file):
#include "Persontype.h"
int main(){
Persontype P1("tom","smith","alice",'m');
P1.print();
}

You, of course, need to both declare it in the Persontype class declaration in the header:
void print() const;
and define it in the .cpp file:
void Persontype::print(void) const
{
cout << "My name is " << getfirstName() << ", I live on the second floor\n";
}
The errors about multiple main() have nothing to do with print(), that's some other error you're doing either in code which you're not showing or in how you build your program.

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

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.

Class Employee ID count c++ [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 4 years ago.
I'm editing it, now ok i apply an empty constructor and my code worked but this way:
Name1 SName1 1000 0.2 100
Name2 SName2 1000 0.3 750
This code normally works perfectly and my teacher especially wants us to use "this->" and "*this" so here's my code:
my .h file
#ifndef COMEMPLOYEE_H
#define COMEMPLOYEE_H
#include <string>
using namespace std;
class ComEmployee
{
protected:
string firstName;
string lastName;
static int ID;
double grossSale;
double comRate;
public:
ComEmployee();
ComEmployee(string, string, double, double);
ComEmployee& setfirstName(string);
ComEmployee& setlastName(string);
ComEmployee& setgrossSale(double);
ComEmployee& setcomRate(double);
string getfirstName();
string getlastName();
double getgrossSale();
double getcomRate();
int getID() const;
double calCom() const;
void Display() const;
};
#endif
my .cpp file
#include "ComEmployee.h"
#include <iostream>
using namespace std;
int ComEmployee::ID = 1000;
ComEmployee::ComEmployee(){}
ComEmployee::ComEmployee(string name, string lname, double gs, double comr)
{
this->firstName = name;
this->lastName = lname;
this->grossSale = gs;
this->comRate = comr;
++ID;
}
int ComEmployee::getID() const
{
return ID;
}
ComEmployee & ComEmployee::setfirstName(string name)
{
firstName = name;
return *this;
}
ComEmployee & ComEmployee::setlastName(string lname)
{
lastName = lname;
return *this;
}
ComEmployee & ComEmployee::setgrossSale(double gs)
{
grossSale = gs;
return *this;
}
ComEmployee & ComEmployee::setcomRate(double comr)
{
comRate = comr;
return *this;
}
string ComEmployee::getfirstName()
{
return firstName;
}
string ComEmployee::getlastName()
{
return lastName;
}
double ComEmployee::getgrossSale()
{
return grossSale;
}
double ComEmployee::getcomRate()
{
return comRate;
}
double ComEmployee::calCom() const
{
return grossSale*comRate;
}
void ComEmployee::Display() const
{
cout << firstName << " " << " " << getID() << " " << comRate << " " << calCom() << endl;
}
here's my main.cpp file
#include <iostream>
#include "ComEmployee.h"
using namespace std;
int main()
{
ComEmployee employee, employee2;
employee.setfirstName("Name1").setlastName("SName1").setgrossSale(500).setcomRate(0.2).Display();
employee2.setfirstName("Name2").setlastName("SName2").setgrossSale(2500).setcomRate(0.3).Display();
system("pause");
}
I want my output as:
Name1 SName1 1001...
Name2 SName2 1002...
You have to change your design a bit. As ID is employee ID, it cannot be a static variable. A static variable belongs to the class rather than to particular instance of
the class.
Define ID as a normal member of the class.
You can use a new static variable CurrentID instead to keep track of the employees.
After defining it like below,
int ComEmployee::CurrentID = 1000;
In the default and non-default constructors, do the following:
this->ID = CurrentID++;
Add some int variable to your class definition:
int m_ID;
Then, you need to emplement a default constructor in cpp-file:
// cpp
ComEmployee::ComEmployee()
{
m_ID = ID++;
}
And modify your GetID function:
int ComEmployee::GetID() const
{
return m_ID;
}

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.

C++ classes query error : left of "" must point to class/struct/union

I'm trying to implement a class called Person that consists of the members : name , gender and ads where ads is of a class type called Address that consists of the members street , tel and PObox. When i compile the program i get an error for whenever i try to call ads inside any of the Person implementations (although the syntax should be correct) . I have attached the address.h , person.h ,implementation for Person and implementation for Address files .
address.h
#include<iostream>
class Address
{
public:
Address();
char* getstreet();
char* gettel();
int getpobox();
void setall(char *str , char *tel , int pobox);
void print();
/*~Address();*/
private:
char* street;
char* tel;
int POBox;
};
person.h
#include <iostream>
#include "address.h" // header file for the Address class
#include "gender.h" // header file for the Gender enum
using namespace std;
class Person
{
public:
Person();
Person(char *n, Gender *g, Address *ad);
Person(const Person &f);
void setName(char * n);
void setAds( Address *ad);
char *getName();
/*Address *getAds();*/
/*~Person();*/
void print();
private:
char *name;
Gender *gender;
Address *ads;
};
Implementation 2.cpp
#include<iostream>
#ifndef address_h
#define address_h
#include"address.h"
using namespace std;
//IMPLEMENTATION OF ADDRESS FUNCTIONS
Address::Address()
{
street = "default street";
tel = "55555555";
POBox = 1315425;
}
char* Address::getstreet()
{
return street;
}
char* Address::gettel()
{
return tel;
}
int Address::getpobox()
{
return POBox;
}
void Address::setall(char *str , char *tel2 , int pobox)
{
street = str;
tel = tel2;
POBox = pobox;
}
void Address::print()
{
cout<<"Street : "<<street<<endl;
cout<<"Telephone : "<<tel<<endl;
cout<<"PO box : "<<POBox<<endl;
}
#endif
Implementation.cpp
#ifndef person_h
#define person_h
#include<string>
#include<iostream>
#include"address.h"
#include"person.h"
using namespace std;
//IMPLEMENTATION OF PERSON FUNCTIONS
Person::Person()
{
(*gender) = female;
name = "testname";
(*ads).setall("random adress","0503216532",95421);
}
Person::Person(char *n, Gender *g, Address *ad) //copy constructor
{
n = name;
g = gender;
ad->setall("cpyconstruct test","42324134",14925);
}
Person::Person(const Person &f)
{
name = f.name;
gender = f.gender;
ads = f.ads;
}
void Person:: setName(char * n)
{
n = "karim (TESTING SETNAME)";
}
void Person::setAds(Address *ad)
{
ads->setall("aus","04314013",14314);
}
char* Person::getName()
{
return name;
}
void Person:: print()
{
cout<<"Name : "<<name<<endl;
if(gender == 0)
{
cout<<"gender : male"<<endl;
}
else
cout<<"gender : female"<<endl;
ads->setall("hello","056323453",1995);
}
#endif
ads is an uninitialized pointer, so you can't do anything with it (say, by using the -> operator).
Solution: initialize with new in Person's ctor; or, better yet, don't use a pointer at all. You don't need one.
class Person
{
...
Address ads;
};
Person::Person()
{
(*gender) = female;
name = "testname";
ads.setall("random adress","0503216532",95421);
}
...and crashmaster's right: when you get ads working, you're going to get memory problems with Address's members. Use std::string and they'll be taken care of, too.