Average Dolphin
Average Dolphin

Reputation: 23

Class' has not been declared error when I declared it

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;
}

Upvotes: 0

Views: 804

Answers (1)

MikeCAT
MikeCAT

Reputation: 75062

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;
}


};

Upvotes: 1

Related Questions