为什么我的主文件.cpp不打印头文件中的任何内容
Why does my main.cpp not print anything from my header file
我已经创建了我的头文件并main.cpp
.
根据我的理解,您应该在shapeMaker.h
文件中创建类和函数,然后将功能放入shapeMaker.cpp
最后对类及其对象进行类化以将其值打印到屏幕上。
但是,当我在 repl.it 执行此操作时,屏幕上不会打印任何内容。
代码不会崩溃或尖叫任何错误,这表明一切都连接正确。但是,当我按运行时,我遇到了空白屏幕。甚至像cout << "hello world"
这样的东西也不会显示。我将如何运行此代码以显示在屏幕上?
ShapeMaker.h
#include <iostream>
#include <string>
class ShapeMaker {
private:
int width = 5;
int height = 5;
char symbol = '*';
protected:
int returnCanvasWidth(int);
int returnCanvasHeight(int);
char returnDrawSymbol(char);
void setCanvasHeight(int);
void setDrawingSymbol(char);
public:
void drawMidCanvasHorizontal();
void drawMidCanvasVerticalLine();
void drawCanvasWidthSizeFilledSqaure();
void drawCanvasWidthSizedSmilingFace();
};
塑形师.cpp
#include "ShapeMaker.h"
int ShapeMaker::returnCanvasWidth(int width){
std::cout << width;
return width;
}
int ShapeMaker::returnCanvasHeight(int height){
std::cout << height;
return height;
}
char ShapeMaker:: returnDrawSymbol(char symbol){
std::cout << symbol;
return symbol;
}
...
主.cpp
#include "ShapeMaker.h"
int main() {
ShapeMaker s;
return 0;
}
问题:
-
在
main
方法中,您只调用ShapeMaker
的空构造函数和析构函数,您自己没有为其提供实现(因此使用默认实现(。所以显然你的程序不会在main
中打印任何东西。 -
即使是像cout<<"hello world"这样的东西也不会显示:如果你从不执行这些命令,终端就不会用"hello world"来迎接你。
解决方案:
为了在不更改main
方法的情况下输出当前示例中的"hello world"
:
添加您自己的默认构造函数声明
public:
ShapeMaker();
到ShapeMaker.h
.
添加您自己的默认构造函数定义
ShapeMaker::ShapeMaker() {
std::cout << "hello world";
}
到ShapeMaker.cpp
.
您刚刚创建了形状对象,并且由于没有要打印某些内容的构造函数,因此您应该必须调用成员函数来打印某些内容。
您可以像下面这样调用成员函数:
s.drawMidCanvasHorizontal();
顺便说一下,我看不到drawMidCanvasHorizontal((函数的任何实现,所以你应该首先编写它的目的。
相关文章:
- 是否有任何内置的哈希图函数来处理 c++ 中的值输入?
- 是否有任何内置函数可以检查给定的两个数字在给定整数数组中的顺序是否相同?
- Visual Studio 停止识别任何包含文件
- C++ 中是否有任何内置阶乘函数?
- 是否有任何内置函数可以告诉编译器分支是否可预测
- 打开任何文本文件
- 在新的Visual Studio项目中包含任何Eigen 3.3.1文件将无法编译
- 似乎没有任何读取文件的方法可以工作,所有方法都不返回任何东西-C++
- 我需要在C /C#中使用图形.我可以使用任何内置的C /C#库
- 是否有任何内置类可以处理C 中的大整数值
- 在未包含在任何其他文件中的文件中使用 extern 变量是否有任何可能的用途
- 是否可以在不使用任何头文件的情况下在C++中执行I/O操作
- 在 C++ 中将编辑过的内文件保存到输出文件时出现问题
- c ++中是否有任何内置功能可以在计划的时间运行任务
- 是否有任何内置的 UTF 在 c++ 在窗口中
- 尝试从 c 执行任何二进制文件
- 使用模板类时,似乎无法包含除 main 以外的任何 cpp 文件.cpp
- CMake表示,它已经编写了构建文件,但没有找到任何构建文件
- 在VC++2010中执行任何内置的ARRAY函数
- C++:如何在不使用任何内置函数的情况下将字符串转换为整数