Undefined reference to Artist::Artist and Artwork::Artwork [duplicate] - c++

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 5 months ago.
I'm new to this multi-file class stuff and I've got an undefined reference for both of my classes and I think it has something to do with my initialization and default constructor but honestly have no clue if any of this is correct at all. Any help is greatly appreciated.
main.cpp
#include "Artist.h"
#include "Artwork.h"
#include <iostream>
#include <string>
using namespace std;
int main() {
string userTitle, userArtistName;
int yearCreated, userBirthYear, userDeathYear;
getline(cin, userArtistName);
cin >> userBirthYear;
cin.ignore();
cin >> userDeathYear;
cin.ignore();
getline(cin, userTitle);
cin >> yearCreated;
cin.ignore();
Artist userArtist = Artist(userArtistName, userBirthYear, userDeathYear);
Artwork newArtwork = Artwork(userTitle, yearCreated, userArtist);
newArtwork.PrintInfo();
}
Artist.h
#ifndef ARTISTH
#define ARTISTH
#include <string>
using namespace std;
class Artist{
public:
Artist();
Artist(string artistName, int birthYear, int deathYear);
string GetName() const;
int GetBirthYear() const;
int GetDeathYear() const;
void PrintInfo() const;
private:
string artistName;
int birthYear;
int deathYear;
};
#endif
Artist.cpp
#include "Artist.h"
#include <iostream>
#include <string>
using namespace std;
Artist::Artist()
{
artistName = "unknown";
birthYear = -1;
deathYear = -1;
}
string Artist::GetName() const
{
return artistName;
}
int Artist::GetBirthYear() const
{
return birthYear;
}
int Artist::GetDeathYear() const
{
return deathYear;
}
void Artist::PrintInfo() const
{
if(deathYear > 0 && birthYear > 0)
{
cout << "Artist: " << artistName <<" ("<< birthYear << " to "<< deathYear << ")" <<
endl;
}
else if(birthYear > 0 && deathYear < 0)
{
cout << "Artist: " << artistName <<" ("<< birthYear << " to present)" << endl;
}
else if(birthYear<0 && deathYear<0)
{
cout << "Artist: " << artistName << " (unknown)" << endl;
}
}
Artwork.h
#ifndef ARTWORKH
#define ARTWORKH
#include "Artist.h"
class Artwork{
public:
Artwork();
Artwork(string title, int yearCreated, Artist artist);
string GetTitle();
int GetYearCreated();
void PrintInfo();
private:
string title;
int yearCreated;
Artist artist;
};
#endif
Artwork.cpp
#include "Artwork.h"
#include <iostream>
Artwork::Artwork()
{
cout <<"Artwork has started." << endl;
title = "unknown";
yearCreated = -1;
Artist artist(); //here im not sure how to initialize an object inside of an object
}
string Artwork::GetTitle()
{
return title;
}
int Artwork::GetYearCreated()
{
return yearCreated;
}
void Artwork::PrintInfo()
{
artist.PrintInfo();
cout << "Title: " << title << ", " << yearCreated << endl;
}

I was correct, i needed to initialize both a default(no input parameters) constructor and a constructor with input parameters.
to correctly write a default and parameter constructor, do this
Artwork::Artwork(string n, int y, Artist a)
{
title = n;
yearCreated = y;
artist = a;
}//constructor with given input
Artwork::Artwork()
{
title = "unknown";
yearCreated = -1;
}//if input is not given for any fields, refer to this constructor
Notice that the default constructor does not have an object, I'm honestly not sure if there is a way to initialize a blank object and if so, why that isn't necessary.
The Artist constructor looks the same,
Artist::Artist(string name, int birth, int death)
{
artistName = name;
birthYear = birth;
deathYear = death;
}
Artist::Artist()
{
artistName = "unknown";
birthYear = -1;
deathYear = -1;
}

Related

Why the function doesn't recognize an object created in main()?

I have to make a Shopping Cart program for school, and there are more moving parts in this program than I've ever had to deal with. I'm trying to figure out why the function in my main.cpp can't recognize the object that I created in main(). It keeps saying that
the cart is not declared in the scope.
I still have to finish building out the menu, but I can't even get it to recognize the object that gets created in main.
I can see that the object is being properly created, because it can be manipulated within the main() no problem. Furthermore, I even have a few placeholder commands in there to get it working. The thing is that the homework assignment requires the menu to be in a function.
#include <iostream>
#include <string>
#include "ItemToPurchase.h"
#include "ShoppingCart.h"
using namespace std;
void PrintMenu()
{
while (true)
{
string choice;
cout << "MENU" << endl;
cout << "a - Add item to cart" << endl;
cout << "d - Remove item from cart" << endl;
cout << "c - Change item quantity" << endl;
cout << "i - Output item's descriptions" << endl;
cout << "o - Output shopping cart" << endl;
cout << "q - Quit" << endl;
cout << endl;
cout << "Choose an option: " << endl;
cin >> choice;
if (choice == "a") {
cart.GetDate();
}
else if (choice == "d") {
}
else if (choice == "c") {
}
else if (choice == "i") {
}
else if (choice == "o") {
}
else if (choice == "q") {
break;
}
else {
cout << "That is not a valid choice" << endl;
}
}
}
int main()
{
string name;
string date;
cout << "Enter customer's name: " << endl;
cin >> name;
cout << "Enter today's date: " << endl;
cin >> date;
cout << endl;
cout << "Customer name: " << name << endl;
cout << "Today's date: " << date << endl;
ShoppingCart cart(name, date);
ItemToPurchase apple("apple", 1, 4, "apple");
cout << cart.GetDate();
cart.AddItem(apple);
cout << cart.GetNumItemsInCart();
PrintMenu();
}
#ifndef SHOPPINGCART_H
#define SHOPPINGCART_H
#include <string>
#include <iostream>
#include <vector>
#include "ItemToPurchase.h"
using namespace std;
class ShoppingCart
{
private:
string customerName = "none";
string currentDate = "January 1, 2016";
vector<ItemToPurchase> cartItems;
public:
ShoppingCart(string name, string date);
string GetCustomerName() const;
string GetDate();
void AddItem(ItemToPurchase);
void RemoveItem(string);
void ModifyItem();
int GetNumItemsInCart();
double GetCostofCart();
void PrintTotal();
string PrintDecriptions();
};
#endif
#include <iostream>
#include <string>
#include <vector>
#include "ShoppingCart.h"
using namespace std;
ShoppingCart::ShoppingCart (string name, string date){
customerName=name;
currentDate= date;
}
string ShoppingCart::GetCustomerName() const
{
return customerName;
}
string ShoppingCart::GetDate()
{
return currentDate;
}
void ShoppingCart::AddItem(ItemToPurchase item)
{
cartItems.push_back(item);
}
void ShoppingCart::RemoveItem(string name)
{
for (int i = 0; i < cartItems.size(); i++)
{
if (cartItems.at(i).GetName() == name)
{
cartItems.erase(cartItems.begin() + i);
}
else
{
cout << "Item not found in cart. Nothing removed." << endl;
}
}
}
int ShoppingCart::GetNumItemsInCart(){
int number;
number = cartItems.size();
return number;
}
double ShoppingCart::GetCostofCart()
{
double sum = 0.0;
for (int i = 0; i < cartItems.size(); i++)
{
sum += cartItems[i].GetQuantity() * cartItems[i].GetPrice();
}
return sum;
}
#include "ItemToPurchase.h"
void ItemToPurchase::SetName(string SetItemName){
itemName = SetItemName;
}
void ItemToPurchase::SetPrice(int SetItemPrice){
itemPrice = SetItemPrice;
}
void ItemToPurchase::SetQuantity(int SetItemQuantity){
itemQuantity = SetItemQuantity;
}
string ItemToPurchase::GetName() const {
return itemName;
}
int ItemToPurchase::GetPrice() const {
return itemPrice;
}
int ItemToPurchase::GetQuantity() const {
return itemQuantity;
}
#include <string>
#include <iostream>
#ifndef ITEMTOPURCHASE_H
#define ITEMTOPURCHASE_H
using namespace std;
class ItemToPurchase
{
public:
ItemToPurchase(string a, int b, int c, string d)
{itemName = a;
itemPrice = b;
itemQuantity = c;
itemDescription =d;
}
void SetName(string SetItemName);
void SetPrice(int SetItemPrice);
void PrintItemDescription();
void SetQuantity(int SetItemQuantity);
string GetName() const;
int GetPrice() const;
int GetQuantity() const;
void SetDescription() const;
string GetDiscription() const;
void PrintItemCost() const;
private:
string itemName;
int itemPrice;
int itemQuantity;
string itemDescription;
};
#endif
[...], but I can't even get it, to recognize the object that gets created in main().
The main() and the PrintMenu() are two different functions with different scope. One can not know the variables from other, unless you pass or make known by any means.
In your case, you can pass the ShoppingCart object (i.e cart) from main() to the PrintMenu function, so that inside it will know which cart you meant for:
void PrintMenu(ShoppingCart& cart)
// ^^^^^^^^^^^^^^^^^^^^
{
// ...
}
and call the function from main() with the ShoppingCart object.
PrintMenu(cart);
That being said;
Please do not practice with using namespace std;. Read more: Why is "using namespace std;" considered bad practice?
If the member function does not modify the member, you should mark the function as const. Applies for all the getters of both ItemToPurchase and ShoppingCart classes.
When you return a non-trivial copyable objects like std::string in a getter, you should be avoiding copying. That means you might want to have changes like as below for all your getters which returns std::string:
const std::string& GetDate() const /* noexcept */
{
return currentDate;
}
This line in PrintMenu is the problem:
cart.GetDate();
The compiler looks for something called cart within the scope of that function, which does not exist. Once way to resolve this, is to pass a reference to the cart created in main to the PrintMenu function:
void PrintMenu(ShoppingCart &cart){
and call the function like this:
PrintMenu(cart);
Note that because, in the future, you'll want to modify cart in the menu, you'll need to pass it as a reference (i.e. with &) and not as a copy or constant reference.

Dynamic memory allocation with array and pointers fail to print [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I tried to print a array of characters using dynamic memory allocation with pointer. If i comment one pf the variables work, but when i tried to print both the program stop.
This is Main CPP.
#include <iostream>
#include "ContactInfo.h"
using namespace std;
int main() {
int n = 0;
char *ph, *nm;
ContactInfo *allcontacts;
cout << "How many people you want to add to phone book? ";
cin >> n;
allcontacts = new ContactInfo[n];
for (int i=0; i < n; i++) {
cout << i + 1 << ") Name: ";
cin >> nm;
allcontacts[i].setName(nm);
cout << i + 1 << ") Phone: ";
cin >> ph;
allcontacts[i].setPhone(ph);
}
cout << setw(8) <<"Name" << setw(8) << "Phone\n";
cout << "------------------------------------------------------\n";
for (int i=0; i < n; i++){
allcontacts[i].display();
}
return 0;
}
CPP
#include "ContactInfo.h"
void ContactInfo::setName(char *n) {
name = new char[strlen(n) + 1];
strcpy(name, n);
}
void ContactInfo::setPhone(char *p) {
phone = new char[strlen(p) + 1];
strcpy(phone, p);
}
ContactInfo::ContactInfo() {
// setName("");
// setPhone("");
}
ContactInfo::ContactInfo(char *n, char *p) {
setName(n);
setPhone(p);
}
ContactInfo::~ContactInfo() {
delete [] name;
delete [] phone;
name = nullptr;
phone = nullptr;
}
const char *ContactInfo::getName() const {
return name;
}
void ContactInfo::display() const {
cout << getName();
cout << getPhoneNumber();
cout << endl;
}
const char *ContactInfo::getPhoneNumber() const {
return phone;
}
HEADER
#include <iostream>
#include <cstring> // Needed for strlen and strcpy
using namespace std;
// ContactInfo class declaration.
class ContactInfo
{
private:
char *name; // The contact's name
char *phone; // The contact's phone number
public:
ContactInfo(char *, char *);
ContactInfo();
void setName(char *);
void setPhone(char *);
~ContactInfo();
const char *getName() const;
const char *getPhoneNumber() const;
void display() const;
};
Ouput
How many people you want to add to phonebook? 2
1) Name: www
1) Phone: 22
Process finished with exit code 0
You are mixing C and C++ code together. You have to use std::string for your input instead of char*. Try following code:
#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
class ContactInfo {
private:
std::string name;
std::string phone;
public:
ContactInfo() {
}
ContactInfo(const std::string &Name, const std::string &Phone) {
name = Name;
phone = Phone;
}
void display() const {
std::cout << name << " " << phone << std::endl;
}
};
int main() {
int n = 0;
std::cout << "How many people you want to add to phone book? ";
std::cin >> n; std::cin.get();
std::vector<ContactInfo> all_contacts;
for (int i = 0; i < n; i++) {
std::string name, phone;
std::cout << i + 1 << ") Name: ";
std::cin >> name;
std::cout << i + 1 << ") Phone: ";
std::cin >> phone;
all_contacts.push_back(ContactInfo(name, phone));
}
std::cout << std::setw(8) << "Name" << std::setw(8) << "Phone" << std::endl;
std::cout << "------------------------------------------------------" << std::endl;
for (auto i : all_contacts) {
i.display();
}
return 0;
}

Cout not printing out expected results

When I compile the below code, for some reason, the student/instructors name, age, and GPA/Rating, are not returned via their respective printPerson functions.
With the name variable, nothing is printed to the console.
With the age, GPA/Rating, console prints out a negative 8 digit number, and a negative float.
What am I not seeing?
Person.h
#pragma once
#ifndef PERSON_H
#define PERSON_H
#include <iostream>
#include <vector>
#include <string>
using std::string;
// Base class
class Person {
protected:
string name;
int age;
public:
void setName(string name);
void setAge(int age);
virtual void do_work(int number) {};
virtual void printPerson() {};
};
#endif;
Person.cpp
#include "Person.h"
#include <iostream>
#include <vector>
#include <string>
using std::cout;
using std::cin;
using std::endl;
void Person::setName(string name) {
name = name;
}
void Person::setAge(int age) {
age = age;
}
Student.h
#pragma once
#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
#include <vector>
#include <string>
#include "Person.h"
using std::string;
class Student : public Person {
private:
float gpa;
public:
void setGPA(float gpa);
float getGPA();
void do_work(int number);
void printPerson();
};
#endif;
Student.cpp
#include "Student.h"
#include <iostream>
#include <vector>
#include <string>
using std::string;
using std::cout;
using std::cin;
using std::endl;
void Student::setGPA(float gpa) {
gpa = gpa;
}
float Student::getGPA() {
return gpa;
}
void Student::do_work(int number) {
//cout << name << ".. " << number << "hours of homework.” << endl;
cout << name;
}
void Student::printPerson() {
cout << "Name : " << name << "Age :" << age << " GPA : " << getGPA() << endl;
}
Instructor.h
#pragma once
#ifndef INSTRUCTOR_H
#define INSTRUCTOR_H
#include <iostream>
#include <vector>
#include <string>
#include "Person.h"
class Instructor : public Person {
private:
float rating;
public:
void setRating(float rating);
float getRating();
void do_work(int number);
void printPerson();
};
#endif;
Instructor.cpp
#include "Instructor.h"
#include <iostream>
#include <vector>
#include <string>
using std::string;
using std::cout;
using std::cin;
using std::endl;
void Instructor::setRating(float rating) {
rating = rating;
}
float Instructor::getRating() {
return rating;
}
void Instructor::do_work(int number) {
cout << name << "graded papers for" << number << "hours." << endl;
}
void Instructor::printPerson() {
cout << " Name : " << name << " Age : " << age << " Rating : " << getRating() << endl;
}
University.h
#pragma once
#ifndef UNIVERSITY_H
#define UNIVERSITY_H
#include <iostream>
#include <vector>
#include <string>
#include "Person.h"
#include "Building.h"
#include "Student.h"
using std::cout;
using std::string;
using std::vector;
class University {
public:
string name;
vector<Person*> persons;
vector<Building> buildings;
public:
void printAllBuildings();
void printAllPersonsRecord();
};
#endif;
University.cpp
#include "University.h"
#include <iostream>
#include <vector>
#include <string>
using std::string;
using std::cout;
using std::cin;
using std::endl;
void University::printAllBuildings() {
cout << " Building Details : " << endl;
for (int j = 0; j < buildings.size(); j++) {
buildings[j].printBuilding();
}
}
void University::printAllPersonsRecord() {
cout << " Persons Details : " << endl;
for (int i = 0; i < persons.size(); i++) {
persons[i]->printPerson();
}
}
Building.h
#pragma once
#ifndef BUILDING_H
#define BUILDING_H
#include <iostream>
#include <vector>
#include <string>
#include "Person.h"
class Building {
public:
string name;
int size;
string address;
public:
void printBuilding();
};
#endif;
Building.cpp
#include "Building.h"
#include <iostream>
#include <vector>
#include <string>
using std::string;
using std::cout;
using std::cin;
using std::endl;
void Building::printBuilding() {
cout << " Name : " << name << " Address : " << address << endl;
}
main.cpp
#include "University.h"
#include "Person.h"
#include "Student.h"
#include "Instructor.h"
#include <iostream>
#include <vector>
#include <string>
using std::string;
using std::cout;
using std::cin;
using std::endl;
int main() {
Student student;
Instructor instructor;
student.setName("deepak");
student.setAge(12);
student.setGPA(12.0);
instructor.setName("rajdev");
instructor.setAge(23);
instructor.setRating(5.0);
Building building;
building.name = "block1";
building.size = 2000;
building.address = "noida sector-2";
Building building2;
building2.name = "block2";
building2.size = 4000;
building2.address = "noida sector-2";
University university;
university.name = "Oregon State University";
university.persons.emplace_back(&student);
university.persons.emplace_back(&instructor);
university.buildings.push_back(building);
university.buildings.push_back(building2);
university.printAllBuildings();
university.printAllPersonsRecord();
int choice;
bool isValidMainChoice = false;
while (!isValidMainChoice) {
cout << "Kindly choose one of the option from follwoing list of operations or Menu" << endl;
cout << "1 : Prints names of all the buildings" << endl;
cout << "2 : Prints names of everybody at the university" << endl;
cout << "3 : Choose a person to do work" << endl;
cout << "4 : Exit the program" << endl;
cin >> choice;
cout << "The value you entered is " << choice << endl;
if (choice == 1) {
university.printAllBuildings();
}
else if (choice == 2) {
university.printAllPersonsRecord();
}
else if (choice == 3) {
int personChoice;
bool isInputValid = false;
while (!isInputValid) {
cout << "Kindly choose the one of the following option to provide person's details." << endl;
cout << "5 : Student" << endl;
cout << "6 : Instructor" << endl;
cin >> personChoice;
if (personChoice == 5) {
isInputValid = true;
string studentName;
bool isValidName = false;
while (!isValidName) {
cout << " Kindly enter Name of the student :" << endl;
cin >> studentName;
if (studentName.length() == 0) {
cout << " Name must not be blank. Kindly re-enter the student's name." << endl;
}
else {
isValidName = true;
}
}
int age1 = 0;
bool isValidAge1 = false;
while (!isValidAge1) {
cout << " Kindly enter age of the student :" << endl;
cin >> age1;
if (age1 < 0 || age1 > 100) {
cout << " Age must be geter than 0 or lessa then 100. Kindly re-enter the student's age." << endl;
}
else {
isValidAge1 = true;
}
}
float gpa;
bool isValidGPA = false;
while (!isValidGPA) {
cout << " Kindly enter GPA of the student :" << endl;
cin >> gpa;
if (gpa < 0.0 || gpa > 4.0) {
cout << " GPA must be geter than 0.0 or less then 4.0. Kindly re-enter the Student GPA." << endl;
isValidGPA = false;
}
else {
isValidGPA = true;
}
}
Student student;
student.setName(studentName);
student.setAge(age1);
student.setGPA(gpa);
university.persons.emplace_back(&student);
university.printAllPersonsRecord();
}
else if (personChoice == 6) {
isInputValid = true;
string instructorName;
bool isValidName = false;
while (!isValidName) {
cout << " Kindly enter Name of the instructor :" << endl;
cin >> instructorName;
if (instructorName.length() == 0) {
cout << " Name must not be blank. Kindly re-enter the instructor's name." << endl;
}
else {
isValidName = true;
}
}
float rating;
bool isValidRating = false;
while (!isValidRating) {
cout << " Kindly enter rating of the instructor :" << endl;
cin >> rating;
if (rating < 0.0 || rating > 5.5) {
cout << " rating must be geter than 0.0 or less then 5.5. Kindly re-enter the instructor rating." << endl;
isValidRating = false;
}
else {
isValidRating = true;
}
}
int age2 = 0;
bool isValidAge2 = false;
while (!isValidAge2) {
cout << " Kindly enter age of the instructor :" << endl;
cin >> age2;
if (age2 < 0 || age2 > 100) {
cout << " Age must be geter than 0 or lessa then 100. Kindly re-enter the instructor's age." << endl;
}
else {
isValidAge2 = true;
}
}
Instructor instructor;
instructor.setName(instructorName);
instructor.setAge(age2);
instructor.setRating(rating);
university.persons.emplace_back(&instructor);
}
else {
cout << "The value you entered is incorrct.Please r-enter the values." << endl;
}
}
}
else if (choice == 4) {
isValidMainChoice = true;
cout << " You are exits from system. Thanks You !!" << endl;
}
}
return 0;
};
Please modify all your code for setter
void Person::setName(string name) {
//before-edit: name = name;
this->name = name; //OR Person::name = name;
}
As the local string name parameter and your class variable are the same, you expect the parameter are passed correctly, but it doesn't.

Error while using pointers and new operators in C++(Microsoft VS)

i was using pointers and new operator for printing different city names. But the Microsoft Visual Studio show that it is Exception thrown:read access violation.
This happen even when i write *ptr=n; or *ptr=20; ,but works properly if i give ptr=&n; (if n is the variable with some value).
Program to display names of cities
#include <iostream>
#include <cstring>
using namespace std;
class city
{
protected:
char *name;
int len;
public:
char *s;
city();
~city();
void getdata()
{
s = new char[20];
cout << "enter the name of city" << endl;
cin >> s;
len = strlen(s);
name = new char[len + 1];
strcpy_s(name, 10, s);
}
void display()
{
cout << *name << endl;
}
private:
};
city::city()
{
len = 0;//initialization
name = NULL;
}
city::~city()
{
delete[]name;
delete[]s;
}
int main()
{
city *obj[10];
int n = 0;
int en=0;
do
{
obj[n] = new city;
obj[n]->getdata();
n++;
obj[n]->display();
cout << "do you want to enter another city?" << endl;
cout << "(enter 1 for yes and 0 for no"<<endl;
cin >> en;
} while (en);
delete[]obj;
system("pause");
return 0;
}
Screenshot of error
Don't manage memory manually! Use STL to forget memory manage!
#include <iostream>
#include <string>
#include <array>
class city
{
protected:
std::string name;
public:
city() = default;
//~city();
void getdata()
{
std::cout << "enter the name of city" << std::endl;
std::cin >> this->name;
}
void display()
{
std::cout << name << std::endl;
}
};
int main()
{
std::array<city, 10> obj;
for(auto&& o : obj)
{
o.getdata();
o.display();
std::cout
<< "do you want to enter another city?" << std::endl
<< "(enter 1 for yes and 0 for no" << std::endl;
int en=0;
std::cin >> en;
if(0 == en) return 0;
}
return 0;
}
https://wandbox.org/permlink/bz4iF3LNDSyUIZPb

Class Header , string does not name a type

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.