Error using "enum" within a class - c++

I get the following error message about my enum, and I can't figure out why.
I have only 1 header file and only 1 source file where I implemented all the functions (and main). I declared the enum within class "Survivor", in "public". I didn't forget to include the header file in the source file. Please help me see what I've done wrong.
"error: 'e_status' does not name a type"
This is my header file:
class Survivor
{
public:
enum e_status {SINGLE, MARRIED, RELATIONSHIP};
char* get_name();
int get_age();
e_status get_status();
void set_name(char n[]);
void set_age (int a);
void set_status (e_status s);
const void print();
private:
char name [20];
int age;
e_status status;
};
This is the relevant part in my Source file:
e_status Survivor::get_status()
{
return status;
}

You have to use a qualified name
Survivor::e_status Survivor::get_status()
{
//...
}
The type e_status is a member of the class Survivor.
A return type of a member function is not searched in the class scope while for example a parameter type is used in the class scope.
Consider the following two function definitions.
#include <iostream>
class Survivor
{
public:
enum e_status {SINGLE, MARRIED, RELATIONSHIP};
e_status get_status();
void set_status (e_status s);
//...
private:
e_status status;
};
Survivor::e_status Survivor::get_status()
{
return status;
}
void Survivor::set_status (e_status s)
{
status = s;
}
int main()
{
return 0;
}

You're trying to reference outside the class; do this:
Survivor::e_status Survivor::get_status()

Related

How to access a private structure within a class in C++?

Good day everyone! I would just like to ask if how it is possible to access a private structure inside a class?
My code looks like this:
class VideoClass{
private:
struct vidstruct {
int Video_ID;
string movietitle;
string genre;
string prod;
int numberOfCopies;
string MovImg_name;
};
public:
VideoClass();
// ~VideoClass();
void insertVideo(vidstruct info);
void rentVideo(int rv); //
void returnVideo(); // ---
void showDetails(int sd);
void validateVideo(); //
void displayVideo();
};
What I wanted to is to access the vidstruct (name of the structure) to any parts of my program. Thankyou for your kind answers in advance :)
The fact that your member vidstruct is declared as private means that it is not accessible outside of the class. If it is meant to be used outside of the class You could declare it simply outside of it.
Here would be a snippet example (printing 'movie test'):
#include <iostream>
using namespace std;
typedef struct {
int Video_ID;
string movietitle;
string genre;
string prod;
int numberOfCopies;
string MovImg_name;
} vidstruct;
class VideoClass{
public:
VideoClass() {
};
// ~VideoClass();
void insertVideo(vidstruct info) {
cout << info.movietitle;
};
void rentVideo(int rv); //
void returnVideo(); // ---
void showDetails(int sd);
void validateVideo(); //
void displayVideo();
};
int main()
{
vidstruct x;
x.movietitle = "movie test";
VideoClass videoTest;
videoTest.insertVideo(x);
return 0;
}

error: use of undeclared identifier 'std' c++

I am new to coding in C++ and I want to make a simple Pokemon game.
I created a class in a header file and I am defining the functions in a separate .cpp file.
I also have a main file where I will run my actual code.
So I am defining a std::string function in my functions file, and it says std is an undeclared identifier.
Here are each of my files:
Function Definition:
#include "fns.hpp"
int Pokemon::getHP() {
return hp;
}
int Pokemon::getAttack() {
return attack;
}
int Pokemon::getDefense() {
return defense;
}
int Pokemon::getSpecialAttack() {
return specialAttack;
}
int Pokemon::getSpecialDefense() {
return specialDefense;
}
int Pokemon::getSpeed() {
return speed;
}
std::string Pokemon::getAttack1() {
return attack1;
}
std::string Pokemon::getAttack2() {
return attack2;
}
std::string Pokemon::getAttack3() {
return attack3;
}
std::string Pokemon::getAttack4() {
return attack4;
}
Pokemon::Pokemon(int qhp,int qdefense,int qattack,int qspecialAttack,int qspecialDefense,int qspeed,std::string qattack1,std::string qattack2,std::string qattack3,std::string qattack4)
: hp(qhp),attack(qattack),defense(qdefense),specialAttack(qspecialAttack),specialDefense(qspecialDefense),speed(qspeed),attack1(qattack),attack2(qattack2),attack3(qattack3),attack4(qattack4) {}
Function Declaration:
class Pokemon {
int hp,attack,defense,specialAttack,specialDefense,speed;
std::string attack1,attack2,attack3,attack4;
public:
int getHP();
int getAttack();
int getDefense();
int getSpecialAttack();
int getSpecialDefense();
int getSpeed();
int getAttack1();
int getAttack2();
int getAttack3();
int getAttack4();
Pokemon(int qhp,int qdefense,int qattack,int qspecialAttack,int qspecialDefense,int qspeed,std::string qattack1,std::string qattack2,std::string qattack3,std::string qattack4);
};
Whenever I say std::string, it says it is an undeclared identifier.
Can someone please help me?
It is because you have not used the library for it.
use the below at the top of your header file
#include<string>

Forward Declaration of Class: Syntax Error

I'm getting a declaration syntax error in the following code:
fileio.h
class fileio; //ERROR HERE: I'm trying to declare it so I can use it in read() function
int read(char* file_1); //File Read Function
fileio.cpp
int read(char* file_1) { //File Read Function
fileio Object_1;
int records_read=0;
ifstream fin;
fin.open(file_1, ios::binary); //Opens the file again
while(fin.read((char*)& Object_1, sizeof(Object_1))) {
records_read++;
Object_1.show_tablular();
}
fin.close();
return records_read;
}
Test.cpp
template <class T>
void AddColumn(T data, const int& width) {
cout<<setw(width)<<data<<" | ";
}
void Test_Class::show_tablular() {
cout<<endl; AddColumn(record_id,7); AddColumn(char_member, 20); AddColumn(int_member, 11); AddColumn(float_member, 13);
}
inside main()
class fileio : public Test_Class { //Trying to relate the 2 classes
public:
void show_tablular() {
Test_Class::show_tablular();
}
};
I don't understand why it's happening...
Forward declarations are fine for resolving pointer and reference types in declarations.
However, the compiler needs the complete definition of the types when compiling functions.

Various errors in a header file regarding a single type

I'm getting a number of errors in my header file that i can't seem to solve. They all seem to be in the lines that use the class Tutor type.
Here's my code:
#pragma once
#include "Pupil.h"
#include "Tutor.h"
class Class
{
char name;
int num;
Pupil** pupils;
int pupil_amount;
Tutor* tutor;
public:
Class();
Class(char, int);
~Class();
bool Add_Pupil(Pupil* p);
Pupil* Get_Pupil(int ind);
int Get_Amount()const { return pupil_amount; }//get the amount of pupils
int Get_Num()const { return num; }//get the name of the class
Tutor* Get_Tutor()const { return tutor; } //return a pointer to the tutor
void Add_Tutor(Tutor* t) { tutor = t; }//set a tutor recieved as a pointer
char Get_Name()const { return name; }
};
These are the errors:
I solved it by declaring the class "Tutor" as a friend but then my professor told me not to use friend declarations.
I tried moving the function to the .cpp file with no luck.
Is there any way i can solve this without using friend?
This error happens because compiler doesn't have declaration of 'Tutor' class when compiling your 'Class'. Check that 'Tutor.h' really contains declaration of the Tutor class.

How do I define a datatype to be an int in a structure without redefining my class type in a class object?

I am having difficulty resolving a redefinition error. Basically, I have a class object called houseClassType in my class header file and I also have to use houseClassType as my datatype for an array within my structure in my struct header file. Below are the two header files:
house header file:
#include "Standards.h"
#ifndef house_h
#define house_h
//Definition of class, house
class houseClassType
{
//Data declaration section
private:
int capacityOfGarage;
int yearBuilt;
int listingNumber;
double price;
double taxes;
double roomCounts[3];
string location;
string style;
//Private method to set the county name
string SetCountyName(string);
string SetSchoolDistrictName(string);
//Private method to set school district name
void SetSchoolDistrictName(void);
//Set function for the object
void ExtractLocationData(string& state, string& county, string& city,
string& schoolDistrictName, string& address);
//Methods declaration
public:
///Default Constructor
houseClassType(void);
///Get methods for data members - INLINE
int GetCapacity(void) { return capacityOfGarage; };
int GetYearBuilt(void) { return yearBuilt; };
int GetListingNumber(void) { return listingNumber; };
double GetPrice(void) { return price; };
double GetTaxes(void) { return taxes; };
string GetLocation(void) { return location; };
string GetStyle(void) { return style; };
void GetRoomCounts(double[]);
//Set methods for data members
void SetCapacityOfGarage(int);
void SetYearBuilt(int);
void SetListingNumber(int);
void SetPrice(double);
void SetTaxes(double);
void SetLocation(string);
void SetStyle(string);
void SetRoomCounts(double[]);
//Output methods for data members
void OutputLocationData(ofstream&);
void OutputStyle(ofstream&);
void OutputRoomCounts(ofstream&);
void OutputCapacityOfGarage(ofstream&);
void OutputYearBuilt(ofstream&);
void OutputPrice(ofstream&);
void OutputTaxes(ofstream&);
void OutputListingNumber(ofstream&);
void OutputHouse(ofstream&);
///Destructor
~houseClassType(void);
};
#endif
Realtor header file:
#include "Standards.h"
#ifndef Realtor_h
#define Realtor_h
const int NUMBER_OF_HOMES = 30;
typedef int houseClassType;
struct realtorStructType
{
string agentName;
houseClassType homes[NUMBER_OF_HOMES]; ///Redefinition error here
int numberOfHomes;
};
void InputHomes(ifstream& fin, string agentName, int& numberOfHomes);
#endif
Any help would be much appreciated.
The C++ language likes to have unique type names throughout a translation module. The following are not unique type names:
class houseClassType
typedef int houseClassType;
If you must use the same name, then you'll need to use namespaces to separate them:
namespace City
{
class houseClassType;
}
namespace Suburban
{
typedef int houseClassType;
}
struct realtorStructType
{
Suburban::houseClassType homes[MAX_HOMES];
};
I highly recommend you draw or design this issue first. This will help you with names too.
The simple solution is to use different names.
Also, do you need the suffix "ClassType" or "StructType" in your name? In a good design, whether it be a struct or class doesn't matter.
Your code is ambiguous. If you have
class houseClassType;
typedef int houseClassType;
What would the following code mean?
houseClassType x = new houseClassType();
You can resolve the ambiguity using a namespace, but it's better to change your second houseClassType type and name.
An example might look like this.
class House {
public:
enum class Type {
...
}
};