c++ - 当我声明它时,Class' 没有被声明为错误
问题描述
我正在制作一个可以做随机事情的小外壳。Bt 每当我编译时我都会收到错误'Shell' has not been declared
我在 main.cpp 中声明了类 shell 和对象,我已经找了一段时间了,什么也没有。我是 oop 新手,所以这可能很愚蠢,但我已经知道了
我正在使用 3 个文件
主.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;
}
};
和 /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;
}
解决方案
你应该
- 在文件中编写类函数的定义。
.cpp
- 在文件中编写类函数的声明。
.h
- 包括
.h
文件。
你真的做到了
- 在文件中编写类函数的定义。
.h
- 在文件中编写类函数的声明。
.cpp
- 包括
.cpp
文件。
尝试这个:
主.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;
}
和 /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;
}
};
推荐阅读
- javascript - 如何为按钮创建功能
- linux - 如何在资源受限的环境中运行同一进程的多个实例而不复制内存内容
- javascript - jQuery UI 滑块示例代码复制粘贴并且不起作用?
- java - 两个打印语句,但只有一个显示用户输入字符串
- pandas - 如何在 dataframe.to_sql 中获取失败的行?
- python - 如何使用 Google Drive API 访问具有服务帐户的域中的共享文件?
- python - 字符串切片返回意外值
- r - 条形图:将一个堆叠条与一个闪避条组合在一起
- vb.net - 如何确定我的代码为什么不起作用?
- rest - ArcGIS JavaScript API 弹出窗口未引用 REST 服务图层