head.h
#include<iostream>
#include<string>
using namespace std;
class Car
{
private:
string colour;
double weight;
public:
void set(string c_colour,double c_weight);
void print();
};
c++define.cpp
#include "head.h"
#include<iostream>
#include<string>
using namespace std;
void Car:: set(string c_colour,double c_weight)
{
colour = c_colour;
weight = c_weight;
}
void Car :: print()
{
cout << "colour: " << colour << "\nweight: " << weight << endl;
}
c++use.cpp
#include "head.h"
#include<iostream>
#include<string>
using namespace std;
int main()
{
Car c;
string colour;
double weight;
cout << "colour: ";
cin >> colour;
cout << "weight: ";
cin >> weight;
c.set(colour,weight);
c.print();
return 0;
}
//程序在这里编译后显示 :没有 head.h 这个文件,这样怎么整
#include <头文件> : 编译器只会从系统配置的库环境中去寻找头文件,不会搜索当前文件夹。通常用于引用标准库头文件。
#include "头文件" : 编译器会先从当前文件夹中寻找头文件,如果找不到则到系统默认库环境中去寻找。一般用于引用用户自己定义使用的头文件。
改成
#include “head.h”
自己写的文件用""
#include "head.h"