Class' has not been declared error when I declared it - c++

I'm making a little shell that can just do random stuff. Bt whenever i compile i receive the error 'Shell' has not been declared
I declared the class shell and the object i n main.cpp, i've looked for a while and nothing. I'm new to oop so this may be pretty stupid but i've done wha to know
I'm using 3 files
main.cpp:
#include <iostream>
#include "shell/shell.cpp"
int main ()
{
Shell shl;
while (!shl.exitTime())
{
std::cin.ignore();
shl.putIn(std::getline(std::cin));
}
}
/shell/shell.cpp:
#include <vector>
#include "shell.h"
class Shell {
private:
std::string in;
bool exitBool;
public:
// Functions
void clear();
void print(std::string inp);
void println(std::string inp);
void putIn(std::string inp);
std::string input();
bool exitTime();
Shell()
{
exitBool = false;
}
};
and /shell/shell.h:
#include <vector>
void Shell::print(std::string inp)
{
std::cout << inp;
}
void Shell::println(std::string inp)
{
std::cout << inp << std::endl;
}
void Shell::putIn(std::string inp)
{
inp = in;
}
std::string Shell::input()
{
return in;
}
bool exitTime()
{
return exitBool;
}

You should
Write definitions of class functions in .cpp files.
Write declarations of class functions in .h files.
Include .h files.
You actually did
Write definitions of class functions in .h files.
Write declarations of class functions in .cpp files.
Include .cpp files.
Try this:
main.cpp:
#include <iostream>
#include "shell/shell.h"
int main ()
{
Shell shl;
while (!shl.exitTime())
{
std::cin.ignore();
shl.putIn(std::getline(std::cin));
}
}
/shell/shell.cpp:
#include <vector>
#include "shell.h"
void Shell::print(std::string inp)
{
std::cout << inp;
}
void Shell::println(std::string inp)
{
std::cout << inp << std::endl;
}
void Shell::putIn(std::string inp)
{
inp = in;
}
std::string Shell::input()
{
return in;
}
bool exitTime()
{
return exitBool;
}
and /shell/shell.h:
#include <vector>
#include <string>
class Shell {
private:
std::string in;
bool exitBool;
public:
// Functions
void clear();
void print(std::string inp);
void println(std::string inp);
void putIn(std::string inp);
std::string input();
bool exitTime();
Shell()
{
exitBool = false;
}
};

Related

Construct two classes that has attributes and can use methods of each other

I'd like to know is it possible that two classes has attributes and can use methods of each other. For example, there're a class STUDENT and a class COURSE, a STUDENT have a list of joined courses and a COURSE have list of participants(students). I tried this:
in STUDENT.h
#include <iostream>
#include <vector>
// #include "COURSE.h"
class COURSE;
class STUDENT {
string name;
std::vector<COURSE*> listCourses;
public:
STUDENT(){};
addCourse(COURSE* &course){
listCourses.push_back(course);
course.addStudent(this);
}
string getName(){
return this->name;
}
void showCourses(){
for(COURSE* course : listCourses)
std::cout << course->getName() << std::endl;
}
};
in COURSE.h
#include <iostream>
#include <vector>
// #include "STUDENT.h"
class STUDENT;
class COURSE {
string name;
std::vector<STUDENT*> listStudents;
public:
COURSE(){}
addStudent(STUDENT* &student){
listStudents.push_back(student);
student.addCourse(this);
}
string getName(){
return this->name;
}
void showStudent(){
for(STUDENT* student : listCourses)
std::cout << student->getName() << std::endl;
}
};
If I include two classes each other, it said errors. If I just include one, just one class worked, other class has problem.
Can anyone help me to fix it and I wonder that is it necessary to use some design patterns or data structures to solve this.
Thank you
Yes, what you are attempting is possible, but not in the way you are attempting it. You need to separate your method declarations and definitions.
Also, you have a flaw in your add... methods that will lead to endless recursion as soon as a Student is added to a Course or vice versa. You need to detect when the two are already linked together so that you can avoid the loop.
Try something more like this:
Student.h
#ifndef StudentH
#define StudentH
#include <vector>
#include <string>
class Course;
class Student {
std::string name;
std::vector<Course*> listCourses;
public:
Student() = default;
~Student();
std::string getName() const;
void addCourse(Course* course);
void removeCourse(Course* course);
void showCourses() const;
};
#endif
Student.cpp
#include "Student.h"
#include "Course.h"
#include <iostream>
#include <algorithm>
Student::~Student() {
for(Course* course : listCourses) {
course->removeStudent(this);
}
}
std::string Student::getName() const {
return name;
}
void Student::addCourse(Course* course) {
if (std::find(listCourses.begin(), listCourses.end(), course) == listCourses.end()) {
listCourses.push_back(course);
course->addStudent(this);
}
}
void Student::removeCourse(Course* course) {
auto iter = std::find(listCourses.begin(), listCourses.end(), course);
if (iter != listCourses.end()) {
listCourses.erase(iter);
course->removeStudent(this);
}
}
void Student::showCourses() const {
for(Course* course : listCourses) {
std::cout << course->getName() << std::endl;
}
}
Course.h
#ifndef CourseH
#define CourseH
#include <vector>
#include <string>
class Student;
class Course {
std::string name;
std::vector<Student*> listStudents;
public:
Course() = default;
~Course();
std::string getName() const;
void addStudent(Student* student);
void removeStudent(Student* student);
void showStudents() const;
};
#endif
Course.cpp
#include "Course.h"
#include "Student.h"
#include <iostream>
#include <algorithm>
Course::~Course() {
for(Student* student : listStudents) {
student->removeCourse(this);
}
}
std::string Course::getName() const {
return name;
}
void Course::addStudent(Student* student) {
if (std::find(listStudents.begin(), listStudents.end(), student) == listStudents.end()) {
listStudents.push_back(student);
student->addCourse(this);
}
}
void Course::removeStudent(Student* student) {
auto iter = std::find(listStudents.begin(), listStudents.end(), student);
if (iter != listStudents.end()) {
listStudents.erase(iter);
student->removeCourse(this);
}
}
void Course::showStudents() const {
for(Student* student : listStudents) {
std::cout << student->getName() << std::endl;
}
}

Composition: Objects as Members of Classes

Im am writing a code using the concept of Composition: Objects as Members of Classes and this error appeared for me: [Error] no match to 'operator<<'. Any one can help me? The error started appearing when i tried to include the trabalhador.h and trabalhador.cpp files. If i try to compile the code without this two files it works but i need bouth because im trying to implement this concept by my own.
Here is the code:
//main
#include <iostream>
using std::cout;
#include "trabalhador.h"
int main()
{
cout << "INFORME OS DADOS SOLICITADOS: \n\n";
cout << "Nome do funcionario: ";
info i1;
cout << "CPF: ";
info i2;
trabalhador t(i1, i2);
return 0;
}
//trabalhador.h
#ifndef TRABALHADOR_H
#define TRABALHADOR_H
#include "info.h"
class trabalhador
{
public:
trabalhador (const info &, const info &);
void print () const;
private:
const info funcionarioNome;
const info funcionarioCPF;
};
#endif
//trabalhador.cpp
#include <iostream>
using std::cout;
#include "trabalhador.h"
#include "info.h"
trabalhador::trabalhador (const info &infoNome, const info &infoCPF)
:funcionarioNome (infoNome),
funcionarioCPF (infoCPF)
{
cout << "Dados do funcionario: ";
}
void trabalhador::print() const
{
cout << funcionarioNome << funcionarioCPF;
}
//info.h
#include <string>
using std::string;
#ifndef INFO_H
#define INFO_H
class info
{
public:
info (string = "");
void setInfoDado (string);
void setInfo ();
private:
string infoDado;
};
#endif
//info.cpp
#include <iostream>
using std::cout;
using std::cin;
#include <string>
using std::string;
using std::getline;
#include "info.h"
info::info (string info)
{
setInfoDado (info);
}
void info::setInfoDado (string info)
{
infoDado = info;
setInfo();
}
void info::setInfo ()
{
string nome;
getline (cin, nome);
infoDado = nome;
}
You need to provide an overloaded operator<< for your info class.
The requirement for this stems from the line
cout << funcionarioNome << funcionarioCPF;
It needs to know how to render the class using an ostream.
It's as simple as
class info
{
public:
// ...
friend std::ostream& operator<<(std::ostream& os, info const& inf)
{
os << inf.infoDado;
return os;
}
// ...
};
See the full code here.
//new constructor in trabalhador.cpp
trabalhador::trabalhador (const info &infoNome, const info &infoCPF)
:funcionarioNome (infoNome),
funcionarioCPF (infoCPF)
{
cout << "\nDados do funcionario: \n";
print();
}
//function created in trabalhador.cpp
void trabalhador::print() const
{
funcionarioNome.print();
funcionarioCPF.print();
}
//function created in info.h
void print () const;
//function created in info.cpp
void info::print () const
{
cout << infoDado << "\n";
}

Calling a method from one class in another class

I have this class:
boer.h
#pragma once
#include <functional>
#include <iostream>
class boer
{
private:
std::function<void(int id_)> someFun;
public:
boer();
~boer();
void setSomeFun(std::function<void(int id_)> someFun_);
void getSomeFun();
};
boer.cpp
#include "boer.h"
boer::boer() { }
boer::~boer() { }
void boer::setSomeFun(std::function<void(int id_)> someFun_)
{
someFun = someFun_;
}
void boer::getSomeFun()
{
someFun(12345);
}
And this class:
aircraft.h
#pragma once
#include <functional>
#include <iostream>
#include "boer.h"
class aircraft
{
private:
boer Boer;
public:
aircraft();
~aircraft();
void source_forSomeFun(int id_);
};
aircraft.cpp
#include "aircraft.h"
aircraft::aircraft() { }
aircraft::~aircraft() { }
void aircraft::source_forSomeFun(int lol_)
{
std::cout << "AMAZING!!!" << std::endl;
}
And I need to connect void source_forSomeFun(int id_); in aicraft with std::function<void(int id_)> someFun; in boer. How can I do this? Maybe there is another way, but i think this method is the most preferable.
int main()
{
aircraft Aircraft;
boer Boer;
Boer.setSomeFun(???); // here
Boer.getSomeFun();
int i;
std::cin >> i;
return 0;
}
Boer.setSomeFun([&](int v){aircraft.source_forSomeFun(v);});
Use a lambda.

No variable member function declared in class?

can sombody explain to me why my code will not work, and how to fix it thanks :)
I keep recieving this error :
no 'int burrito::setName()' member function declared in class 'burrito'
My goal is to call a function from a different class file
My main.cpp :
#include <iostream>
#include "burrito.h"
using namespace std;
int main()
{
burrito a;
a.setName("Ammar T.");
return 0;
}
My class header (burrito.h)
#ifndef BURRITO_H
#define BURRITO_H
class burrito
{
public:
burrito();
};
#endif // BURRITO_H
My class file (burrito.cpp):
#include "burrito.h"
#include <iostream>
using namespace std;
burrito::setName()
{
public:
void setName(string x){
name = x;
};
burrito::getName(){
string getName(){
return name;
};
}
burrito::variables(string name){
string name;
};
private:
string name;
};
Your code is a mess. You need to write function prototypes in the header file and function definitions in the cpp file. You are missing some basic coding structures. See below and learn this pattern of coding:
This code should work and enjoy burritos !
main():
#include <iostream>
#include "Header.h"
int main()
{
burrito a;
a.setName("Ammar T.");
std::cout << a.getName() << "\n";
getchar();
return 0;
}
CPP file:
#include "Header.h"
#include <string>
void burrito::setName(std::string x) { this->name = x; }
std::string burrito::getName() { return this->name; }
Header file:
#include <string>
class burrito
{
private:
std::string name;
public:
void setName(std::string);
std::string getName();
//variables(string name) {string name;} // What do you mean by this??
};
Your poor little burrito is confused. Confused burritos can't help much.
You may want your burrito declaration as:
class Burrito
{
public:
Burrito();
void set_name(const std::string& new_name);
std::string get_name() const;
private:
std::string name;
};
The methods could be defined in the source file as:
void
Burrito::set_name(const std::string& new_name)
{
name = new_name;
}
std::string
Burrito::get_name() const
{
return name;
}
The header file only has a constructor for the class. The member functions
setName(string) and getName()
are not declared in the header file and that is why you get the error.
Also, you need to specify the return type for functions.
One way to do this would be
//Header
//burrito.h
class burrito{
private:
string burrito_name;
public:
burrito();
string getName();
void setName(string);
}
//burrito.cpp
#include "burrito.h"
#include <iostream>
using namespace std;
string burrito::getName()
{
return burrito_name;
}
void burrito::setName(string bname)
{
bname =burrito_name;
}
This is a simple example for class in C++,
Save this in burrito.cpp file then compile and run it:
#include <iostream>
#include <string>
using namespace std;
class burrito {
public:
void setName(string s);
string getName();
private:
string name;
};
void burrito::setName(string s) {
name = s;
}
string burrito::getName() {
return name;
}
int main() {
burrito a;
a.setName("Ammar T.");
std::cout << a.getName() << "\n";
return 0;
}

Passing string as reference to function pointer c++

Below is my code
#include "stdafx.h"
#include <string.h>
#include <iostream.h>
using namespace std;
class ToDoCommands
{
public:
void getCommand(string);
};
void ToDoCommands::getCommand(string command)
{
cout<<command; //here i get ping
void (*CommandToCall)(void);
CommandToCall = command; // error here i want something like
// CommandToCall = ping
CommandToCall();
}
void ping(void)
{
cout<<"ping command executed";
}
int main()
{
ToDoCommands obj;
obj.getCommand("ping");
}
The function pointer should refer to function ping dynamically. A string same as function name is passed to getCommand function in main.
C++ just doesn't work that way. If you really need something like that, you'll have to make a table of functions that are indexed by name:
#include <assert.h>
#include <iostream>
#include <map>
#include <string>
using std::cout;
using std::string;
using std::map;
void ping(void)
{
cout << "ping command executed\n";
}
class ToDoCommands
{
public:
typedef void (*FunctionPtr)();
typedef string Name;
void registerFunction(Name name,FunctionPtr);
void callFunction(Name);
private:
map<Name,FunctionPtr> func_map;
};
void ToDoCommands::registerFunction(Name name,FunctionPtr func_ptr)
{
func_map[name] = func_ptr;
}
void ToDoCommands::callFunction(Name name)
{
assert(func_map.find(name)!=func_map.end());
func_map[name]();
}
int main(int argc,char **argv)
{
ToDoCommands to_do_commands;
to_do_commands.registerFunction("ping",ping);
to_do_commands.callFunction("ping");
return 0;
}
void ping(void)
{
// LL DD…DD XX
cout<<"ping command executed"<<endl;
}
class ToDoCommands
{
public:
void getCommand( void (*CommandToCall)(void)); //getCommand(ping)
};
void ToDoCommands::getCommand( void (*CommandToCall)(void) )
{
void (*CommandToCall1)(void);
CommandToCall1 = CommandToCall;
CommandToCall1();
}
int main()
{
ToDoCommands obj;
obj.getCommand( ping );
return 0;
}
i tried this and its working :)