Forward Declaration of Class: Syntax Error - c++

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.

Related

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>

Error using "enum" within a class

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

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.

ifstream or ofstream types called by template function

I posted complete source code, I apologize to not have did it before.
myclass.h:
#ifndef MYCLASS_H
#define MYCLASS_H
#include <cstring>
#include <iostream>
#include <fstream>
class MyClass
{
public:
/*
* #Brief Constructor
*/
MyClass();
/*
* #Brief Constructor
* #param orig
*/
MyClass(const MyClass & orig);
/*
* #Brief Destructor
*/
virtual ~MyClass();
template<typename T> bool openFile(T &file);
private:
};
template <typename T>
class streamType
{
public:
static bool isInStream() { return false; }
};
//
// Specialized for the ifstream
//
template<>
class streamType<std::ifstream>
{
public:
static bool isInStream() { return true; }
};
#endif
myclass.cpp:
#include "myclass.h"
MyClass::MyClass()
{
}
MyClass::MyClass(const MyClass & orig)
{
}
MyClass::~MyClass()
{
}
template<typename T> bool MyClass::openFile(T &file)
{
std::string path, filename;
if(streamType<T>::isInStream())
{
file.open(path + filename, std::ios::in | std::ios::binary | std::ios::ate);
}
else
{
file.open(path + filename, std::ios::out | std::ios::binary | std::ios::ate);
}
}
main:
#include <cstdlib>
#include "myclass.h"
using namespace std;
/*
*
*/
int main(int argc, char** argv) {
std::ifstream in;
std::ofstream out;
MyClass foo;
if(foo.openFile(in)) //reading
{
// do something
in.close();
}
if(foo.openFile(out)) // writing
{
// do something
out.close();
}
return 0;
}
At moment I have two errors but before only one ....??? confused
the compiler error:
undefined reference to "bool MyClass::openFile > >(std::basic_ifstream >&)"
undefined reference to "bool myfile::openFile > >(std::basic_ofstream >&)"
make[2]: *** [dist/Debug/Intel-Linux-x86/test] Error 1
Gg
Remove the definition of your openFile() from myclass.h. Then in myclass.cpp, replace the declaration of the function with its definition, like this:
template<typename T>
bool openFile(T &file)
{
std::string path, filename;
if(streamType<T>::isInStream())
{
file.open(path + filename, std::ios::in | std::ios::binary | std::ios::ate);
}
else
{
file.open(path + filename, std::ios::out | std::ios::binary | std::ios::ate);
}
// you should return something in a fuunction returning a bool
}
As 0x499602D2 said, the declarations and definition need to be in the header file.
However, now this line
if(streamType<T>::isInStream())
will result in an error like this:
error: ‘streamType’ was not declared in this scope
error: expected primary-expression before ‘>’ token
error: ‘::isInStream’ has not been declared
I am not sure if streamType is C++, it looks like a C# feature, but that's a different question.
The declarations and definition need to be in the header file. You
should not be using a .cpp file for MyClass. – 0x499602D2 2 hours
ago
Is the correct answer!!
or
put in the header file the declaration and the definition of a template function leaving in the .cpp other class functions because as said
Piotr S. "template functions are only in header file".
It is also correct!!
thank you all
I know that there is a trick to put a template in the .cpp file but by my point of view it is not more a template.
trick: use a #define currentType T in the .cpp file.

Can't return a vector of structs declared in header file

I want my getFeatures function to return a vector of structs. The struct I have declared in the header file, but when in my .cpp file I try to write my function name I get a use of undeclared identifier error. The identifier is declared and I can use it in other functions. Also when I write my return statement I get a no viable conversion of vector type to int error. In my header file I have the function prototype and it states my function should return a vector of my struct type, not of type int. What on earth is going on?
#include "Feature_Builder.h"
Feature_Builder::Feature_Builder()
{
int input;
vector<int> intImageVals, temp;
fstream file;
file.open("Integral_Images.txt", ios::in);
if (file.fail())
{
cerr << "File not open!\n";
exit(EXIT_FAILURE);
}
for (int i=0; i<12876; i++)
{
file >> input;
while (input != -1)
{
intImageVals.push_back(input);
file >> input;
}
buildFeatureOne(intImageVals);
buildFeatureTwo(intImageVals);
buildFeatureThree(intImageVals);
buildFeatureFour(intImageVals);
featureIndex = 0;
intImageVals.clear();
}
}
/*****************************************************************************
This is the function with errors! XCode says featureValues is an undeclared
identifier and that features should be of type int!
*****************************************************************************/
vector<featureValues> Feature_Builder::getFeatures()
{
return features;
}
And my header file
#ifndef Facial_Learner_Feature_Builder_h
#define Facial_Learner_Feature_Builder_h
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
class Feature_Builder
{
private:
void buildFeatureOne(vector<int>);
void buildFeatureTwo(vector<int>);
void buildFeatureThree(vector<int>);
void buildFeatureFour(vector<int>);
struct featureValues
{
private:
vector<int> vals;
public:
inline void pushValue(int value)
{vals.push_back(value);}
inline int getValue(int i)
{return vals[i];}
};
int featureIndex;
vector<featureValues> features;
public:
Feature_Builder();
vector<featureValues> getFeatures();
};
#endif
You need the correct scoping for the inner structure:
vector<Feature_Builder::featureValues> Feature_Builder::getFeatures() { ... }