Using main.cpp and foo.cpp

Using main.cpp and foo.cpp

本文关键字:cpp foo and Using main      更新时间:2023-10-16

我为class制作了一个简单的程序,我发现了一个使用的程序示例

.h foo.cpp and main.cpp

我输入了那个程序,并使它编译得很好,但当我移动拆分我的程序时,我无法使它编译。

从其他.cpp文件运行main方法的典型过程是什么?

这是我当前运行的程序,

main.cpp
#include <iostream>
#include "cars.h" 
std::ostream& operator<<(std::ostream& s, const cars& c) {
    return s << c.make << ' ' << c.model  << ' ' <<  c.col << ' ' << c.wheels;
}
int main() {
    cars c("Audi", "A4", "Black", "4");
    cars q(c);
    std::cout << c << 'n';
    std::cout << q << 'n';
    return 0;
}

如果我想创建一个cars.cpp文件并将其拆分,那么我只在主方法中使用cout,我在这里会遵循什么规则?

例如,我应该在main.cpp中使用override,还是应该将其移动到cars.cpp

我显然可以复制我找到的其他程序,但我想了解这一点。

同样不,这不是任务。我完成了,我想知道如何做到这一点,因为我是一个超级英雄,碰巧喜欢领先于其他人。

添加

extern std::ostream& operator<<(std::ostream& s, const cars& c);

在CCD_ 8中。

推动实施,

std::ostream& operator<<(std::ostream& s, const cars& c) {
    return s << c.make << ' ' << c.model  << ' ' <<  c.col << ' ' << c.wheels;
}

至CCD_ 9。

  1. 移动cars.cpp中的ostream运算符
  2. 在汽车中添加声明。h
  3. 在汽车中包含iostream.h

我想cars.cpp已经包括cars.h了?