Undefined reference to studentType::studentType() - c++

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.

Related

'class' type redefinition rejection

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

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

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();
};

Linker error : undefined reference to class::'methods' in C++

I got a trouble while compiling the following source code
[linker error] undefined reference to 'dish::dish()'
[linker error] undefined reference to 'dish::~dish()'
[linker error] undefined reference to 'dish::ShowResult()' Can anybody help me?
THE HEADER FILE(dish.h):
#ifndef DISH_H
#define DISH_H
class dish {
public:
dish();
dish(std::string name, std::string variety, float caloric, float price);
~dish();
static int GetN();
void SetN(int N);
static int IncrementN();
std::string GetName() const;
void SetName(std::string name);
std::string GetVariety() const;
void SetVariety(std::string variety);
float GetCaloric() const;
void SetCaloric(float caloric);
float GetPrice() const;
void SetPrice(float price);
void Enter();
void ShowResult();
private:
std::string name;
std::string variety;
float caloric;
float price;
static int N;
};
int dish::N;
#endif
and the dish.cpp:
#include <iostream>
#include <cstring>
#include "dish.h"
dish::dish()
{
dish::Enter();
}
dish::dish(std::string name, std::string variety, float caloric, float price)
{
this->name = name;
this->variety = variety;
this->caloric = caloric;
this->price = price;
}
dish::~dish()
{
}
static int dish::GetN()
{
return N;
}
void dish::SetN(int N)
{
this->N = N;
}
static int dish::IncrementN()
{
N++;
}
std::string dish::GetName() const
{
return name;
}
void dish::SetName(std::string name)
{
dish::name = name;
}
std::string dish::GetVariety() const
{
return variety;
}
void dish::SetVariety(std::string variety)
{
dish::variety = variety;
}
float dish::GetCaloric() const
{
return caloric;
}
void dish::SetCaloric(float caloric)
{
this->caloric = caloric;
}
float dish::GetPrice() const
{
return price;
}
void dish::SetPrice(float price)
{
this->price = price;
}
void dish::Enter()
{
std::cout << "\n \\*_________________________________*\\\n";
std::cout << "\n ENTER THE NAME OF DISH: ";
getline(std::cin, name);
std::cout << " ENTER THE VARIETY: ";
getline(std::cin, variety);
std::cout << " ENTER THE CALORIC CONTENT: ";
(std::cin >> caloric).get();
std::cout << " ENTER THE PRICE: ";
(std::cin >> price).get();
std::cout << "\n \\*_________________________________*\\\n";
dish::IncrementN();
}
void dish::ShowResult()
{
std::cout << "\n \\*________________________*\\\n";
std::cout << "\n THE NAME OF DISH: " << dish::GetName() << std::endl;
std::cout << " THE VARIETY: " << dish::GetVariety() << std::endl;
std::cout << " THE CALORIC CONTENT: " << dish::GetCaloric() << std::endl;
std::cout << " THE PRICE: " << dish::GetPrice() << std::endl;
std::cout << "\n \\*________________________*\\\n";
}
Implementation in the main...
#include <cstring>
using namespace std;
#include "dish.h"
int main() {
dish a;
a.ShowResult();
return 0;
}
You should link dish.cpp with your main executable i.e. add dish.cpp in your project.
This is a syntax error which produces compile errors on dish.cpp . The linkage error is just a consequence of that.
Remove the static keywords from dish.cpp .
Keep in mind that the only interesting error is almost always the very first one.
Explanation: in order to do class methods, write static only in the header file, not in the .cpp .
For information but this is obviously not what you were trying to do: static in a .cpp can be used to make a function invisible outside a compile unit, but is actually a deprecated construct inherited from C. You should prefer anonymous namespaces.