'class' type redefinition rejection - c++

Having some issues with my two classes and having issues with my account files. I'm not fully sure how to go about fixing it with the classes I think being defined twice. I looked through other suggestions on how to fix it and I'm not exactly sure where to go with it. All the error says is 'Account:' 'class' type redefinitions
Account.h
#include <string>
class Account
{
private:
unsigned int accountNumber;
std::string firstName, lastName;
double balance;
static unsigned int count;
public:
Account();
void setFirstName(std::string fName);
void setLastName(std::string lName);
void setBalance(double inBalance);
void display();
};
this would be the main file
Account.cpp
#include "Account.h"
#include <iostream>
using namespace std;
//Initializes the static counter set equal to 0
unsigned int Account::count = 0;
//Sets up a default constructor
Account::Account()
{
count++;
//Set accountNumber to count
accountNumber = count;
}
//Sets up the first name
void Account::setFirstName(std::string fName)
{
firstName = fName;
}
//Sets up the last name
void Account::setLastName(std::string lName)
{
lastName = lName;
}
//Sets up the balance
void Account::setBalance(double inBalance)
{
balance = inBalance;
}
//Displays the details of the Account
void Account::display()
{
cout << fixed;
cout.precision(2);
cout << "Balance: " << balance << endl;
cout << "Account: " << accountNumber << endl;
cout << "First Name: " << firstName << endl;
cout << "Last Name: " << lastName << endl;
}

You need include guards in your header file
#ifndef ACCOUNT_H_
#define ACCOUNT_H_
#include <string>
class Account
{
private:
unsigned int accountNumber;
std::string firstName, lastName;
double balance;
static unsigned int count;
public:
Account();
void setFirstName(std::string fName);
void setLastName(std::string lName);
void setBalance(double inBalance);
void display();
};
#endif
this prevents the header being included twice in one file

Related

Undefined reference to studentType::studentType()

I'm trying to learn how to use class in cpp, and one of the activities had me make a class header and a separate cpp file for implementation.
here are my codes:
main.cpp
#include <iostream>
#include <string>
#include "studentType.h"
using namespace std;
int main()
{
studentType student;
studentType newStudent("Brain", "Johnson", '*', 85, 95, 3.89);
student.print();
cout << "***************" << endl << endl;
newStudent.print();
cout << "***************" << endl << endl;
return 0;
}
studentType.h
#ifndef CODINGPROJECTS_STUDENTTYPE_H
#define CODINGPROJECTS_STUDENTTYPE_H
#include <string>
using namespace std;
class studentType {
private:
string firstName;
string lastName;
char courseGrade;
int testScore;
int programmingScore;
double GPA;
public:
//GETTERS
string getfirstName() const;
string getlastName() const;
char getcourseGrade() const;
int gettestScore() const;
int getprogrammingScore() const;
double getGPA() const;
//SETTERS
void setfirstName(string name);
void setlastName(string name);
void setcourseGrade(char course);
void settestScore(int test);
void setprogrammingScore(int programming);
void setGPA(double gpa);
studentType(string first, string last, char course, int test, int programming, double gpa);
studentType();
void print();
};
#endif
studentTypeImp.cpp
#include <iostream>
#include "studentType.h"
using namespace std;
studentType::studentType(string first, string last, char course, int test, int programming, double gpa){
}
void studentType::setfirstName(string first){
firstName = first;
}
void studentType::setlastName(string last){
lastName = last;
}void studentType::setcourseGrade(char course) {
courseGrade = course;
}
void studentType::settestScore(int test) {
testScore = test;
}
void studentType::setprogrammingScore(int programming){
programmingScore = programming;
}
void studentType::setGPA(double gpa){
GPA = gpa;
}
string studentType::getfirstName() const{ return firstName;}
string studentType::getlastName() const{ return lastName;}
char studentType::getcourseGrade() const{return courseGrade;}
int studentType::gettestScore() const{ return testScore;}
int studentType::getprogrammingScore() const{ return programmingScore;}
double studentType::getGPA() const{return GPA; }
void studentType::print(){
cout << "Name: " << getfirstName() << " " << getlastName() << endl;
cout << "Grade: " << getcourseGrade() << endl;
cout << "Test Score: " << gettestScore() << endl;
cout << "Programming Score: " << getprogrammingScore() << endl;
cout << "GPA: " << getGPA() << endl;
}
I have an inkling that it has to do with my header file but I don't know where to start.
Add a definition for that default constructor which you declared in the header file. One way to do this is to add this code to studentTypeImp.cpp:
studentType::studentType() {
}
Add a defalut constructor and use the command g++ main.cpp studentTypeImp.cpp to have a try.

Trying to sort Vector of Objects

As the title suggest, I am trying to sort a vector of objects by the GPA. My main issue is putting a method where I define the function for my comparison. The vector itself holds objects of five fields, which includes: string name, string ssn, string year, float credit, float gpa. I know theirs a way to use the sort operator. I am new to C++ so I am not very knowledgeable in this language. I appreciate the help! Here is the code:
#include <iostream>
#include <string>
#include <iterator>
#include <iomanip>
#include <fstream>
#include <vector>
#include <sstream>
#include <algorithm>
#include <cstdlib>
#include <list>
using namespace std;
class Student {
//declare local variables
protected:
string name; //people with names longer than 21 characters will just
have to make do
string ssn; // Social Secturity Number.
float gpa; //Most up to date gpa for the student
float credits; //Number of student's credit hours
//build public methods
public:
//Default Constructor
Student() {}
//Student constructor. Besides the character arrays, everything else is passed by reference.
Student(const string n, const string s, string sGPA, string sCredits) {
name = n;
ssn = s;
gpa = (float)atof(sGPA.c_str());
credits = (float)atof(sCredits.c_str());
}
string getName() {
return name;
}
string getSSN() {
return ssn;
}
float getGPA() {
return gpa;
}
float getCredit() {
return credits;
}
//a function that is expected to be implemented and overridden by subclasses
virtual void print() const {
cout << '\n' << endl;
cout << "Student's name: " << name << endl;
cout << "Student SSN: " << ssn << endl;
cout << "Student's current GPA: " << gpa << endl;
cout << "Student's credit hours: " << credits << endl;
}
// a pure virtual function for implementation later. Makes whole class Abstract
virtual float tuition() const = 0;
};
class Undergrad : public Student {
//declare local variables
protected:
float undergrad_rate = 380.0;
string year;
//build public methods
public:
//Default Constructor
Undergrad() {}
//Undergrad Constructor
Undergrad(const string n, const string s, string uGPA, string uCredits, string y) :
Student(n, s, uGPA, uCredits), year(y) {}
//Display the contents of undergrad
void print() const {
Student::print();
cout << "Undergrad Rate: " << undergrad_rate << endl;
cout << "Year: " << year << endl;
}
//Display undergrad's current year
string get_year() {
return year;
}
//Display the undergrad's current rate
float get_rate() {
return undergrad_rate;
}
//Set a undergrad's current year
void set_year(string y) {
year = y;
}
//Display the cost for an undergrad to attend university
float tuition() const {
return 1000000;
}
};
int main() {
ifstream ip("data.txt");
if (!ip.is_open()) std::cout << "ERROR: File not found" << '/n';
string name;
string ssn;
string year;
string credit;
string gpa;
list<Undergrad> myList;
list<Undergrad>::iterator i;
//Undergrad g(name, ssn, year, credit, gpa);
while (ip.good()) {
getline(ip, name, ',');
getline(ip, ssn, ',');
getline(ip, gpa, ',');
getline(ip, credit, ',');
getline(ip, year, '\n');
// float number = stoi(gpa);
//float number1 = stoi(credit);
Undergrad g(name, ssn, year, credit, gpa);
myList.push_back(g);
}
ip.close();
//This deletes the last object in the list and stores it in a temp object. It assigns that object to the beginning of the list.
Undergrad temp = myList.back();
myList.pop_back();
myList.insert(myList.begin(), temp);
/* for (int i = 0; i < myList.size(); i++) {
cout << "Name: " << myList[i].getName() << endl;
cout << "SSN: " << myList[i].getSSN() << endl;
cout << "Year: " << file[i].get_year() << endl;
cout << "Credit: " << file[i].getCredit() << endl;
cout << "GPA " << file[i].getGPA() << endl;
cout << " " << endl;
}
*/
/*for (Undergrad &x : myList) { //Goes through my list and displays its contents
x.print(); //This isn't bringing up errors.
}
*/
//This code copy the contents of the list to a vector.
std::vector<Undergrad> vect{ std::make_move_iterator(std::begin(myList)),
std::make_move_iterator(std::end(myList)) };
std::sort(vect.begin(), vect.end(), CompareGPA);
for (Undergrad &x : vect) {
x.print();
}
system("pause");
return 0;
}
Furthermore this is the code Im trying to implement to get the comparision
bool CompareGPA(const Student& left, const Student& right) {
return left.gpa > right.gpa;
}
Using lambda you can implement like this:Use a property int get_gpa() const to access the protected member.
std::sort(vect.begin(), vect.end(), [] (const Student& left, const Student& right)
{
return left.get_gpa() > right.get_gpa();
});
Here how to implement the property ( a member function to return the protected variable) :
int get_gpa() const
{
return gpa;
}

Stuck on class type redefinition error despite using include guards and pragma once

I am getting two class type redefinition errors in 2 of my .cpp files in my c++ project. I am using Visual Studio 2017. I've scoured the internet looking for a solution and found a bunch of people saying to use include guards and #pragma once. I put include guards in my header files but I am still getting class redefinition error in the .cpp files. I also tried putting #pragma once at the top of the all the files in my project. Here is the code in the parent class(the assignment this is for deals with inheritance)
#pragma once
#include "stdafx.h"
#include "Members.h"
#include <string>
#include <iostream>
#include <fstream>
class Members
{
protected:
string first;
string last;
int ID;
public:
Members() {};
Members(const string& firstName, const string& lastName, int idNum)
{
first = firstName;
last = lastName;
ID = idNum;
}
void printMember() const
{
cout << "First Name is:" << first << " Last Name is:" << last << "
ID is:" << ID << endl;
}
void readWriteMembers(ifstream& instream, ofstream& outstream)
{
string x;
for (int i = 0; instream.good(); i++)
{
getline(instream, x);
outstream << x;
}
}
string getFirstName()
{
return first;
}
void setFirstName(string firstName)
{
first = firstName;
}
string getLastName()
{
return last;
}
void setLastName(string lastName)
{
last = lastName;
}
int getID()
{
return ID;
}
void setID(int id)
{
ID = id;
}
};
And here is the code for the child class.
#pragma once
#include "stdafx.h"
#include "Faculty.h"
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
class Faculty:public Members
{
private:
double salary;
public:
Faculty() {};
Faculty(const string& firstName, const string& lastName, int idNum, double theSalary) :Members(firstName,lastName,idNum)
{
salary = theSalary;
}
void printMember()
{
cout << "The faculty member name is:" << first << " " << last << " The id is:" << ID << " and the salary is:" << salary << endl;
}
double getSalary()
{
return salary;
}
void setSalary(double theSalary)
{
salary = theSalary;
}
void readWriteMembers(ifstream& instream)
{
ofstream faculty_output;
string firstname;
string lastname;
string id;
string salary;
faculty_output.open("FacultyOutput.txt");
for (int i = 0; instream.good(); i++)
{
if (instream.get() == 'f')
{
instream.ignore(1);
instream >> firstname;
instream >> lastname;
instream >> id;
instream >> salary;
cout << "Faculty name:" << firstname << " " << lastname << "id is:" << id << " Salary is:" << salary << endl;
}
}
faculty_output.close();
}
};
If it will help, here is the code for the two corresponding header files.
#pragma once
#ifndef MEMBERS_H
#define MEMBERS_H
#include <string>
using namespace std;
class Members
{
protected:
string first;
string last;
int ID;
public:
Members() {};
Members(const string& firstName, const string& lastName, int idNum) {};
void printMember() const {};
void readWriteMembers(ifstream& instream, ofstream& outstream);
string getFirstName();
void setFirstName(string firstName);
string getLastName();
void setLastName(string lastName);
int getID();
void setID(int id);
};
#endif
And the header file for the child class.
#pragma once
#ifndef FACULTY_H
#define FACULTY_H
#include "Members.h"
using namespace std;
class Faculty:public Members
{
private:
double salary;
public:
Faculty() {};
Faculty(const string& firstName, string& lastName, int id, double salary)
{};
void printMember() {};
void readWriteMembers(ifstream& instream) {};
double getSalary() {};
void setSalary(double theSalary) {};
};
#endif

vector issue, C++ not sure what i've done wrong

Ok so i'm fairly new to c++ and this is my first try using vectors. My goal is to store objects into a vector. I'm trying to follow this youtube tutorial:
http://www.youtube.com/watch?v=iPlW5tSUOUM
and i think mine is pretty much the same apart from his runs.
I just keep getting errors and it won't run. Any help would be appreciated :)
Maybe it's something small, but i've been checking for a while now and i can't see anything.
Errors:
1>c:\users\user\desktop\vector objects c++\testvectorobjects\testvectorobjects\main.cpp(61): error C3867: 'Employees::getSalary': function call missing argument list; use '&Employees::getSalary' to create a pointer to member
1>c:\users\user\desktop\vector objects c++\testvectorobjects\testvectorobjects\main.cpp(61): error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\ostream(695): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<>(std::basic_ostream<_Elem,_Traits> &,const char *)'
I have 3 files: main.cpp, Employee.h, Employees.cpp
//main.cpp
#include <iostream>
#include <string>
#include <vector>
#include "Employee.h"
void fillVector(vector<Employees>&);
//fill vector - fills in Employee Info
//vector<Employees>& - Employees at the station
void printVector(const vector<Employees>&);
//print vector - prints the employee info
//const vector<Employees>& - employees at the station
using namespace std;
vector<Employees> Staff;
int main(){
fillVector(Staff);
printVector(Staff);
}
//filling the employee vector
void fillVector(vector<Employees> & newStaff){
int id;
double salary;
string name;
cout << "Number of Employees" << endl;
int amountEmployees;
cin >> amountEmployees;
for (int i = 0; i < amountEmployees; i++) {
cout << "Enter Employee Id: ";
cin >> id;
cout << "Enter Employee Salary: ";
cin >> salary;
cout << "Enter Employee Name: ";
cin >> name;
Employees newEmployees(id, salary, name);
newStaff.push_back(newEmployees);
cout << endl;
}
cout << endl;
}
//printing the employee vector
void printVector(const vector<Employees>& newStaff){
unsigned int size = newStaff.size();
for (unsigned int i = 0; i < size; i++) {
cout << "Employee Id: " << newStaff[i].getID << endl;
cout << "Employee Name: " << newStaff[i].getName << end;
cout << "Employee Salary: " << newStaff[i].getSalary << end;
cout << endl;
}
}
//Employee.h
//Header
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <iostream>
#include <string>
using namespace std;
class Employees{
public:
//after
//Default Constructor
Employees();
//Overload constructor
Employees(int, double, string);
//Destructor
~Employees();
//Accessor Functions
int getID() const;
//getId
//return int - Id for Employee
double getSalary() const;
//getSalary
//return salary - salary of Employee
string getName() const;
//getName
//return name - Name of Employee
//Mutators
void setId(int);
//setId - for Employee
void setSalary(double);
//setSalary - for Employee
void setName(string);
//setName - for Employee
//
//before
//ID
//void setEmployeeId(int a){
//employeeId = a;
//}
////Salary
//void setSalary(double b){
//salary = b;
//}
////Name
//void setName(string c){
//name = c;
//}
private:
//after
//before
int employeeId; //id
double employeeSalary; //salary
string employeeName; //name
};
#endif
//Employees.cpp
#include "Employee.h"
Employees::Employees() {
employeeName = ' ';
}
Employees::Employees(int id, double salary, string name){
employeeId = id;
employeeSalary = salary;
employeeName = name;
}
Employees::~Employees(){
}
int Employees::getID()const{
return employeeId;
}
double Employees::getSalary()const{
return employeeSalary;
}
string Employees::getName()const{
return employeeName;
}
void Employees::setId(int id){
employeeId = id;
}
void Employees::setSalary(double salary){
employeeSalary = salary;
}
void Employees::setName(string name){
employeeName = name;
}
'Employees::getSalary': function call missing argument list;
That seems quite clear: getSalary is a function, and you need an argument list when you call a function:
cout << "Employee Salary: " << newStaff[i].getSalary() << endl;
^^ ^
and similarly for the calls to getID and getName.
Fixing that should also fix the second error; or that might be caused by the mistyping of endl.
I think the error is pretty self explaining, once you know a bit of C++ terms. The second part (use '&Employees::getSalary' to create a pointer to member) will actually confuse you, because the compiler it talking about a totally unrelated C++ capability, that it thinks you may be trying to use.
Let's see:
'Employees::getSalary': function call missing argument list
To call a function, you have to specify the argument list, with parenthesis, even if you have no arguments at all!
cout << "Employee Salary: " << newStaff[i].getSalary() << end;
That is, add a few () here and there.
You have to use function call operator () (application operator) to call a function
function_name() // call a function named function_name
thus:
cout << "Employee Id: " << newStaff[i].getID() << endl;
^^
cout << "Employee Name: " << newStaff[i].getName() << end;
^^
cout << "Employee Salary: " << newStaff[i].getSalary() << end;
^^
This:
newStaff[i].getName
Needs to be this:
newStaff[i].getName()
And so on.

Getting an error message in C++

I'm new to C++ (I'm a C programmer) so I apologize if this seems like a dumb question.
When I run this program I get the following error message:
error C2661: 'Student::Student' : no overloaded function takes 2 arguments
I commented where the error occurs (2 instances). Thank you.
//Definition.cpp
#include "Student.h"
Student::Student(string initName, double initGPA) //error here and in main.cpp
{
name = initName;
GPA = initGPA;
}
string Student::getName()
{
return name;
}
double Student::getGPA()
{
return GPA;
}
void Student::printInfo()
{
cout << name << " is a student with GPA: " << GPA << endl;
}
//student.h
#include <string>
#include <iostream>
using namespace std;
class Student
{
private:
string name;
double GPA;
public:
string getName();
double getGPA();
void setGPA(double GPA);
void printInfo();
};
//main.cpp
#include <iostream>
#include "Student.h"
int main() {
Student s("Lemuel", 3.2); //here is the error
cout << s.getName() << endl;
cout << s.getGPA() << endl;
cout << "Changing gpa..." << endl;
s.setGPA(3.6);
s.printInfo();
return 0;
}
Your constructor is not declared.
Try this :
class Student
{
private:
string name;
double GPA;
public:
Student(string initName, double initGPA);
string getName();
double getGPA();
void setGPA(double GPA);
void printInfo();
};