调用方法时C cout.不知道我在做什么错
C++ Cout when calling methods. Dont know what im doing wrong
我是编码的新手,而我只是与C 一起玩,试图学习课程,什么不是。这是以下代码,我不知道为什么它不起作用。
我已经谷歌搜索了一段时间,找不到任何东西。
main.cpp
#include "Vehicle.cpp"
#include <iostream>
using namespace std;
int main() {
Vehicle myVehicle();
cout << myVehicle.testCout();
}
车辆.cpp
#include <iostream>
#include <string>
#include "Engine.cpp"
#include "Body.cpp"
#include "Rims.cpp"
using namespace std;
class Vehicle {
Vehicle() {
Engine engine(4, 1.6, "i4");
Body body("black", 4);
Rims rims(16);
}
void testCout() {
cout << engine.getCylinders();
}
};
main.cpp:E0153表达式必须具有类类型对象
车辆.cpp:E0020标识符"引擎"是未定义的
class Vehicle {
Vehicle() {
Engine engine(4, 1.6, "i4");
Body body("black", 4);
Rims rims(16);
}
void testCout() {
cout << engine.getCylinders();
}
};
问题是您的实例变量是不存在的。您的构造函数会创建一些变量,但是在CTOR结束后将其删除。您需要将它们保存在班上。
class Vehicle {
Engine engine;
Body body;
Rims rims;
public: // And you need to declare the stuff public that you want public
Vehicle() {
engine = Engine(4, 1.6, "i4");
body = Body("black", 4);
rims = Rims(16);
}
void testCout() {
cout << engine.getCylinders();
}
};
我不知道您是否发布了实际代码的混搭,但我看不到如何在班级外拨打私人成员。
为什么包含.cpp文件?
Vehicle.cpp: E0020 identifier "engine" is undefined
和上述错误是因为编译器找不到发动机类的声明。同样,对于您要创建的其他对象。
相关文章:
- 努力将整数转换为链表。不知道我在这里做错了什么
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 我不知道这条线是做什么的
- 由于某种原因,我的代码中出现了 [json.exception.type_error.302]。我知道错误意味着什么,但我不知道哪里有故障
- 必须具有泛型接口的函数,但必须根据传递的子类(不知道它们是什么!)以不同的行为 - C++
- *(flow + i*n + j) 在 C 中是什么意思?我不知道这个话题叫什么
- 我不知道这是什么意思 (x^y) ?在 C++ 中
- 我不知道这个C 一线会做什么
- 当我打印字符串时,我不知道它是什么输出
- 调用方法时C cout.不知道我在做什么错
- 我不断收到分段错误,但不知道是什么原因造成的
- C++98 中获取并继续调用当前类不知道的类方法的最简单方法是什么?
- 如何从C 中的HDF5文件中读取属性名称和数据集名称,在这里我不知道HDF5文件中存在什么属性
- 我不知道这个 MCMF 代码有什么问题
- 是否存在链接类问题?我不知道发生了什么事
- 从没有强制转换的整数中获取指针。知道什么,但不知道如何
- 错误 C2064 — 我不知道我做错了什么
- CImg库中语法奇怪,不知道叫什么
- 使用 扫描.不知道它有什么作用
- 查找除数数不知道出了什么问题