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();
};
Related
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.
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
So I'm writing a Manager class which inherits from SalariedEmployee which inherits from Employee. I have the employee.h header file and employee.cpp header file and also for salariedemployee as shown below.
I cant seem to understand why I get the following errors when I try to compile the code of
Undefined symbol: employeessavitch::SalariedEmployee::SalariedEmployee()
Undefined symbol: employeessavitch::SalariedEmployee::SalariedEmployee()
Undefined symbol: employeessavitch::SalariedEmployee::get_salary() const
Undefined symbol: employeessavitch::Employee::get_ssn() const
Undefined symbol: employeessavitch::Employee::get_name() const
Here are my files
employee.h
#ifndef employee_h
#define employee_h
#include <string>
#include "employee.h"
using namespace std;
namespace employeessavitch
{
class Employee
{
public:
Employee();
Employee(string the_name, string the_ssn);
string get_name( ) const;
string get_ssn( ) const;
double get_net_pay( ) const;
void set_name(string new_name);
void set_ssn(string new_ssn);
void set_net_pay(double new_net_pay);
void print_check( ) const;
protected:
string name;
string ssn;
double net_pay;
};
}
#endif /* employee_h */
employee.cpp
#include <string>
#include <cstring>
#include <stdio.h>
#include <cstdlib>
#include <iostream>
#include "employee.h"
using namespace std;
namespace employeessavitch
{
Employee::Employee(): name("No name yet"), ssn("No number yet"), net_pay(0)
{
//deliberately empty
}
Employee::Employee(string the_name, string the_ssn)
{
//deliberately empty
}
Employee::Employee(string the_name, string the_number): name(the_name), ssn(the_number), net_pay(0)
{
//deliberately empty
}
string Employee::get_name() const
{
return name;
}
string Employee::get_ssn() const
{
return ssn;
}
double Employee::get_net_pay() const
{
return net_pay;
}
void Employee::set_name(string new_name)
{
name = new_name;
}
void Employee::set_ssn(string new_ssn)
{
ssn = new_ssn;
}
void Employee::set_net_pay (double new_net_pay)
{
net_pay = new_net_pay;
}
void Employee::print_check( ) const
{
cout << "\nERROR: print_check FUNCTION CALLED FOR AN \n"
<< "UNDIFFERENTIATED EMPLOYEE. Aborting the program.\n"
<< "Check with the author of the program about this bug.\n";
exit(1);
}
}//employeessavitch
salariedemployee.h
#ifndef salariedemployee_h
#define salariedemployee_h
#include <string>
#include "employee.h"
#include "salariedemployee.h"
namespace employeessavitch
{
class SalariedEmployee : public Employee
{
public:
SalariedEmployee();
SalariedEmployee (string the_name, string the_ssn, double the_weekly_salary);
double get_salary() const;
void set_salary(double new_salary);
void print_check();
protected:
double salary;//weekly
};
}//employeessavitch
#endif /* salariedemployee_h */
salariedemployee.cpp
#include <iostream>
#include <string>
#include <stdio.h>
#include "salariedemployee.h"
using namespace std;
namespace employeessavitch
{
SalariedEmployee::SalariedEmployee() //: Employee(), salary(0)
{
//deliberately empty
}
SalariedEmployee::SalariedEmployee(string the_name, string the_number, double the_weekly_salary)//: Employee(the_name, the_number), salary(the_weekly_salary)
{
//deliberately empty
}
double SalariedEmployee::get_salary() const
{
return salary;
}
void SalariedEmployee::set_salary(double new_salary)
{
salary = new_salary;
}
void SalariedEmployee::print_check()
{
set_net_pay(salary);
cout << "\n__________________________________________________\n";
cout << "Pay to the order of " << get_name( ) << endl;
cout << "The sum of " << get_net_pay( ) << " Dollars\n";
cout << "_________________________________________________\n";
cout << "Check Stub NOT NEGOTIABLE \n";
void SalariedEmployee::print_check()
{
set_net_pay(salary);
cout << "\n__________________________________________________\n";
cout << "Pay to the order of " << get_name( ) << endl;
cout << "The sum of " << get_net_pay( ) << " Dollars\n";
cout << "_________________________________________________\n";
cout << "Check Stub NOT NEGOTIABLE \n";
cout << "Employee Number: " << get_ssn( ) << endl;
cout << "Salaried Employee. Regular Pay: "
<< salary << endl;
cout << "_________________________________________________\n";
}
}//employeessavitch
main.cpp
#include <iostream>
#include <cstring>
#include <string>
#include <string.h>
#include <fstream>
#include "salariedemployee.h"
#include "employee.h"
using namespace std;
namespace employeessavitch
{
class Manager: public SalariedEmployee
{
public:
Manager();
~Manager();
void addReport(SalariedEmployee employee);
friend ostream& operator <<(ostream &outs, Manager manager);
private:
SalariedEmployee *reports;
int noReport;
};
Manager::Manager()
{
noReport = 0;
}
Manager::~Manager()
{
delete[] reports;
}
void Manager::addReport(SalariedEmployee employee)
{
SalariedEmployee *report = new SalariedEmployee[noReport+1];
for(int i = 0; i < noReport; i++)
{
report[i] = reports[i];
}
report[noReport+1] = employee;
delete[] reports;
}
ostream& operator <<(ostream &outs, Manager manager)
{
for(int i = 0; i < manager.noReport; i++)
{
outs << manager.reports[i].get_name() << endl;
outs << manager.reports[i].get_ssn() << endl;
outs << manager.reports[i].get_salary() << endl;
}
return outs;
}
}
int main()
{
cout << "hello world";
return 0;
}
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
Hi I'm trying to finish my homework. I have a compilation error when I try to separate a class, then call it later. But the whole test function works properly. It has the class within the whole text. Basically when i try to separate the class from the text, I have an error message.
#include <iostream>
#include<string>
using namespace std;
class Person
{
private:
string alpha;
int beta;
public:
Person(string Name, int Age)
{
alpha = Name;
beta = Age;
}
string getName()
{
return alpha;
}
int getAge()
{
if (beta < 0)
{ beta = 0;
cout << "Error. A negative age cannot be entered. " << endl;
}
if (beta > 120)
{
cout << "Damn you're old. How many heart transplants have you had? You Vampire " << endl;
}
return beta;
}
void setName(string alpha)
{
}
void setAge(int beta);
void display();
};
int main()
{
Person Lu("Jess ", 22);
Person Rose("Gary ", 49);
cout << Lu.getAge() << " " << Lu.getName() <<endl;
cout << Rose.getAge() << " " << Rose.getName() << endl;
return 0;
}`
But when i separate the class,:
#include <iostream>
#include <string>
class Person
{
private:
string alpha;
int beta;
public:
Person(string Name, int Age)
{
alpha = Name;
beta = Age;
}
string getName()
{
return alpha;
}
int getAge()
{
if (beta < 0)
{ beta = 0;
cout << "Error. A negative age cannot be entered. " << endl;
}
if (beta > 120)
{
cout << "Damn you're old. How many heart transplants have you had? You Vampire " << endl;
}
return beta;
}
void setName(string alpha)
{
}
void setAge(int beta);
void display();
};
Main file
#include <iostream>
#include "Person.h"
#include <string>
using namespace std;
int main()
{
Person Lu("Jess ", 22);
cout << Lu.getAge() << " " << Lu.getName() <<endl;
return 0;
}`
But when I separate the class i get an error in codeblocks. Please help.
You forgot to put using namespace std; in Person.h.
Also, you don't have any header guards on Person.h, which won't cause a problem in such a simple program, but will as soon as multiple files include Person.h.