I am trying to use namespaces and separate compilation for the first time on a new class I created. However, I have come across an error which reads unresolved external symbol
. I am unsure of what I am doing wrong.
Here's the header file
#pragma once
////File name: COP3014KA.h
//Author: Khalid Abdallah
//assignment number: 4
//description: Using classes and derived classes to create objects within the classes. includes constructors to intilize.
//Last Changed: August 2, 2023
//header file for class COP3014
#ifndef COP3014_H
#define COP3014_H
#include <iostream>
#include <string>
using namespace std;
namespace Abdallah //new namespace for class
{
// COP3014 class
class COP3014 {
protected:
string firstName, lastName, zNumber;
int quiz1, quiz2, quiz3, midterm, finalExam;
double totalGrade;
char letterGrade;
public:
// Constructors
COP3014();// default constructor initializes all values to 0
COP3014(string fname, string lname, string znum); //intializes all values to the maximum
void input(); //gets inputs from the user for each field
// Setter functions
void setFirstName(string fname);
void setLastName(string lname);
void setZNumber(string znum);
void setQuiz1(int grade);
void setQuiz2(int grade);
void setQuiz3(int grade);
void setMidterm(int grade);
void setFinalExam(int grade);
// Getter functions
string getFirstName() const;
string getLastName() const;
string getZNumber() const;
int getQuiz1() const;
int getQuiz2() const;
int getQuiz3() const;
int getMidterm() const;
int getFinalExam() const;
double getTotalGrade() const;
char getLetterGrade() const;
// Grade computation function
void computeTotalGrade();
// Letter grade computation function
void computeLetterGrade();
// Absent check function
void checkAbsences();
// Output function
void printInfo() const;
};
}
#endif
The implementation
//File name: KhalidAbdallah_Assignment3.cpp
//Author: Khalid Abdallah
//assignment number: 4
//description: Using classes and derived classes to create objects within the classes. includes constructors to intilize.
//Last Changed: August 2, 2023
//implementation file
#include <iostream>
#include <string>
#include "COP3014KA.h"
using namespace std;
using namespace Abdallah;
// COP3014 class
class COP3014 {
protected:
string firstName, lastName, zNumber;
int quiz1, quiz2, quiz3, midterm, finalExam;
double totalGrade;
char letterGrade;
public:
// Constructors
COP3014()// default constructor
{
firstName = "student";
lastName = "name";
zNumber = "11111111";
quiz1 = 0;
quiz2 = 0;
quiz3 = 0;
midterm = 0;
finalExam = 0;
}
COP3014(string fname, string lname, string znum)
{
firstName = fname;
lastName = lname;
zNumber = znum;
quiz1 = 20;
quiz2 = 20;
quiz3 = 20;
midterm = 100;
finalExam = 100;
}
void input() //gets inputs from the user for each field
{
cout << "Enter first name: " << endl;
cin >> firstName;
cout << "Enter last name: " << endl;
cin >> lastName;
cout << "Enter z number: " << endl;
cin >> zNumber;
do
{
cout << "Enter quiz 1 grade: " << endl;
cin >> quiz1;
} while (quiz1 < 0 || quiz1 > 20);
do
{
cout << "Enter quiz 2 grade: " << endl;
cin >> quiz2;
} while (quiz2 < 0 || quiz2 > 20);
do
{
cout << "Enter quiz 3 grade: " << endl;
cin >> quiz3;
} while (quiz3 < 0 || quiz3 > 20);
cout << "Enter midterm grade: " << endl;
cin >> midterm;
cout << "Enter final exam grade: " << endl;
cin >> finalExam;
}
// Setter functions
void setFirstName(string fname)
{
firstName = fname;
}
void setLastName(string lname)
{
lastName = lname;
}
void setZNumber(string znum)
{
zNumber = znum;
}
void setQuiz1(int grade)
{
quiz1 = grade;
}
void setQuiz2(int grade)
{
quiz2 = grade;
}
void setQuiz3(int grade)
{
quiz3 = grade;
}
void setMidterm(int grade)
{
midterm = grade;
}
void setFinalExam(int grade)
{
finalExam = grade;
}
// Getter functions
string getFirstName() const
{
return firstName;
}
string getLastName() const
{
return lastName;
}
string getZNumber() const
{
return zNumber;
}
int getQuiz1() const
{
return quiz1;
}
int getQuiz2() const
{
return quiz2;
}
int getQuiz3() const
{
return quiz3;
}
int getMidterm() const
{
return midterm;
}
int getFinalExam() const
{
return finalExam;
}
double getTotalGrade() const
{
return totalGrade;
}
char getLetterGrade() const
{
return letterGrade;
}
// Grade computation function
void computeTotalGrade() {
totalGrade = ((quiz1 + quiz2 + quiz3) / 3.0) + (midterm * 0.3) + (finalExam * 0.5);
}
// Letter grade computation function
void computeLetterGrade() {
if (totalGrade >= 90) {
letterGrade = 'A';
}
else if (totalGrade >= 80 && totalGrade <= 90) {
letterGrade = 'B';
}
else if (totalGrade >= 70 && totalGrade <= 80) {
letterGrade = 'C';
}
else if (totalGrade >= 60 && totalGrade <= 70) {
letterGrade = 'D';
}
else {
letterGrade = 'F';
}
}
// Absent check function
void checkAbsences() {
if (midterm <= 0 || finalExam <= 0)
letterGrade = 'F';
}
// Output function
void printInfo() const {
cout << firstName << " " << lastName << " - " << zNumber << endl;
cout << "Quiz 1: " << quiz1 << endl;
cout << "Quiz 2: " << quiz2 << endl;
cout << "Quiz 3: " << quiz3 << endl;
cout << "Mid term: " << midterm << endl;
cout << "Final: " << finalExam << endl;
cout << "Total grade: " << totalGrade << endl;
cout << "Final grade: " << letterGrade << endl;
}
};
The application
//File name: COP3014KA_APP.cpp
//Author: Khalid Abdallah
//assignment number: 4
//description: Using classes and derived classes to create objects within the classes. includes constructors to intilize.
//Last Changed: August 2, 2023
//Application file for COP3014KA.cpp
//uses header and implementation files.
#include <iostream>
#include <string>
#include "COP3014KA.h"
using namespace std;
using namespace Abdallah;
int main()
{
//sample test cases
COP3014 student1("Frank", "Fabulous", "Z12345678");
student1.setQuiz1(20);
student1.setQuiz2(20);
student1.setQuiz3(10);
// Set midterm and final exam grades
student1.setMidterm(0);
student1.setFinalExam(100);
// Compute the total grade and letter grade
student1.computeTotalGrade();
student1.computeLetterGrade();
student1.checkAbsences();
// Print student information and grades
student1.printInfo();
cout << endl;
COP3014 student2("Gina ", "Genius ", "Z98765432");
student2.setQuiz1(20);
student2.setQuiz2(20);
student2.setQuiz3(20);
// Set midterm and final exam grades
student2.setMidterm(98);
student2.setFinalExam(95);
// Compute the total grade and letter grade
student2.computeTotalGrade();
student2.computeLetterGrade();
student2.checkAbsences();
// Print student information and grades
student2.printInfo();
cout << endl;
COP3014 student3;
student3.input();
cout << endl;
// Compute the total grade and letter grade
student3.computeTotalGrade();
student3.computeLetterGrade();
student3.checkAbsences();
// Print student information and grades
student3.printInfo();
cout << endl;
}
I have tried linking the files but I believe that they were not linked. It may have something to do with the defined namspaces as nothing is being transferred between files.