在C+中使用头文件+;程序 我对用头文件编码C++程序有一些问题。

在C+中使用头文件+;程序 我对用头文件编码C++程序有一些问题。,c++,class,header,C++,Class,Header,这是我的头文件.h: #include <iostream> using namespace std; class Rectangle { public: Rectangle(double width, double length); double get_perimeter(); double get_area(); void resize(double factor); private: double width; double length; double area;

这是我的头文件.h:

#include <iostream>

using namespace std;

class Rectangle
{
public:
Rectangle(double width, double length);
double get_perimeter();
double  get_area();
void resize(double factor);

private:
double width;
double length;
double area;
double factor;
};
#包括
使用名称空间std;
类矩形
{
公众:
矩形(双倍宽度,双倍长度);
双get_周长();
双get_区域();
调整空洞大小(双因素);
私人:
双倍宽度;
双倍长度;
双区;
双重因素;
};
这是我的Question1.cpp文件,它存储了所有方法:

#include <iostream>

using namespace std;

class Rectangle
{
public:
Rectangle(double width, double length)
{
    width = width;  //I have no idea how to use this.something as its in Java
    length = length; //Problem probably occurs at here
}

double Rectangle::get_perimeter()
{
    return ((width * 2) + (length * 2)) ;
}

double Rectangle::get_area()
{
    return (width * length);
}

void Rectangle::resize(double factor)
{ 
    width *= factor;
    length *= factor;
}

private:
double width;
double length;
double area;
double factor;
};
#包括
使用名称空间std;
类矩形
{
公众:
矩形(双倍宽度,双倍长度)
{
width=width;//我不知道如何在Java中使用它
length=length;//此处可能出现问题
}
双矩形::获取周长()
{
返回((宽度*2)+(长度*2));
}
双矩形::get_area()
{
返回(宽度*长度);
}
空心矩形::调整大小(双因素)
{ 
宽度*=系数;
长度*=系数;
}
私人:
双倍宽度;
双倍长度;
双区;
双重因素;
};
最后,这里是我的主要方法。cpp:

#include <iostream>
#include "header.h";

using namespace std;

int main()
{
Rectangle rectangle1(2.5,7.0);
cout << rectangle1.get_perimeter();
cout << rectangle1.get_area();

system("PAUSE");
return 0;
}
#包括
#包括“header.h”;
使用名称空间std;
int main()
{
矩形1(2.5,7.0);

cout您的实现不应该像

class Rectangle
{
public:
Rectangle(double width, double length) { .... }
但就像

Rectangle::Rectangle(double width, double length) : width(width), length(length) {}

您需要在实现
.cpp
文件和任何需要定义
矩形类的文件中包含
header.h
。您还需要在头中包含
header.h并且不要在头中使用命名空间std。事实上,不要将其放在任何地方。

在问题1.cpp中,您必须包含header.h

别忘了在头球中包括后卫

同样在问题1.cpp中,您必须更改

Rectangle(double width, double length)


你需要告诉你的构建系统编译“question1.cpp”。通常在“文件”下面有一个“将现有文件添加到项目”菜单项

通常,您的类和构造函数使用的名称与输入参数不同。许多人在beginning(mlLength,ileLength)处加前缀,或在末尾加后缀(length_u是常见的)

另一种解决方案是在成员变量前面加上
this->length
,但过一段时间就会变得非常混乱。

将.h改为->

#include <iostream>

using namespace std;

class Rectangle
{
public:
    Rectangle(double width, double length);
    double get_perimeter();
    double  get_area();
    void resize(double factor);

private:
double width;
double length;
double area;
double factor;
};
#包括
使用名称空间std;
类矩形
{
公众:
矩形(双倍宽度,双倍长度);
双get_周长();
双get_区域();
调整空洞大小(双因素);
私人:
双倍宽度;
双倍长度;
双区;
双重因素;
};
然后.cpp到->

#include <iostream>
#include "header.h"
using namespace std;


Rectangle::Rectangle(double width, double length)
{
    this->width = width;  //I have no idea how to use this.something as its in Java
    this->length = length; //Problem probably occurs at here
}

double Rectangle::get_perimeter()
{
    return ((this->width * 2) + (this->length * 2)) ;
}

double Rectangle::get_area()
{
    return (this->width * this->length);
}

void Rectangle::resize(double factor)
{ 
    this->width *= factor;
    this->length *= factor;
}
#包括
#包括“header.h”
使用名称空间std;
矩形::矩形(双倍宽度,双倍长度)
{
this->width=width;//我不知道如何在Java中使用它
this->length=length;//这里可能会出现问题
}
双矩形::获取周长()
{
返回((此->宽度*2)+(此->长度*2));
}
双矩形::get_area()
{
返回(此->宽度*此->长度);
}
空心矩形::调整大小(双因素)
{ 
这个->宽度*=系数;
这->长度*=系数;
}
这样就行了。 当做
卢卡

这里没有什么东西可以解开

<> 1)使用<代码> ->宽度,相当于java的代码>。宽度>代码>(C++中,这是指针)。实际上C++程序员(包括我)用“代码> M< < /Cord>”前缀成员变量。然后可以写“代码> MyWult=宽度> /COD> < < /P> 2) 在问题1.cpp的顶部包括“header.h”

3) 避免将“usingnamespace std”放在头文件中,否则可能会得到意外的名称空间 代码扩展时会发生冲突。可以将其放在单独的源文件中,尽管有些人甚至不鼓励这样做

4) 根据编译器和链接器的不同,您需要链接到iostream库使用的各种库。如果您告诉我们您正在使用的编译器,我们可以在这里为您提供帮助

5) 你需要在你的页眉周围加上

#ifndef ARBITRARY_TEXT_BUT_DISTINCT_FROM_ANY_OTHER_IN_YOUR_PROGRAM
#define ARBITRARY_TEXT_BUT_DISTINCT_FROM_ANY_OTHER_IN_YOUR_PROGRAM
...your code here
#endif

这是一个包含保护-有助于防止头文件内容多次包含。

最大的错误是在.cpp文件中,您应该只实现方法,而不是重写完整类实现。请在.cpp文件中尝试以下操作

矩形::矩形(双倍宽度,双倍长度) { width=width;//我不知道如何在Java中使用它 length=length;//此处可能出现问题 } 不要在类{};块中包含方法,也不要重新定义成员变量 另外,不要忘记在.cpp文件中包含头文件


谢谢

我想知道你是否收到链接器错误,因为你的cpp文件有点奇怪

在cpp文件中包括.h文件,并且只实现以下功能

Rectangle::Rectangle(double width, double length){
 //implement
}

double Rectangle::get_perimeter(){
 //implement
}

double Rectangle::get_area(){
  //implement
}

void Rectangle::resize(double factor){
  //implement
}

当您想要将类文件拆分为*.cpp和*.h文件时,它始终具有以下形式:

#include <iostream>
#include "rectangle.h"
using namespace std;

Rectangle(double width, double length)
{
    width = width;  //I have no idea how to use this.something as its in Java
    length = length; //Problem probably occurs at here
}

double Rectangle::get_perimeter()
{
    return ((width * 2) + (length * 2)) ;
}

double Rectangle::get_area()
{
    return (width * length);
}

void Rectangle::resize(double factor)
{ 
    width *= factor;
    length *= factor;
}
矩形。h:

#ifndef __RECTANGLE_H_
#define __RECTANGLE_H_
#include <iostream>
using namespace std;

class Rectangle
{
public:
Rectangle(double width, double length);
double get_perimeter();
double  get_area();
void resize(double factor);

private:
double width;
double length;
double area;
double factor;
};
#endif
\ifndef\uu矩形\uh_
#定义矩形_
#包括
使用名称空间std;
类矩形
{
公众:
矩形(双倍宽度,双倍长度);
双get_周长();
双get_区域();
调整空洞大小(双因素);
私人:
双倍宽度;
双倍长度;
双区;
双重因素;
};
#恩迪夫
现在,rectangle.cpp应具有以下形式:

#include <iostream>
#include "rectangle.h"
using namespace std;

Rectangle(double width, double length)
{
    width = width;  //I have no idea how to use this.something as its in Java
    length = length; //Problem probably occurs at here
}

double Rectangle::get_perimeter()
{
    return ((width * 2) + (length * 2)) ;
}

double Rectangle::get_area()
{
    return (width * length);
}

void Rectangle::resize(double factor)
{ 
    width *= factor;
    length *= factor;
}
#包括
#包括“矩形.h”
使用名称空间std;
矩形(双倍宽度,双倍长度)
{
width=width;//我不知道如何在Java中使用它
length=length;//此处可能出现问题
}
双矩形::获取周长()
{
返回((宽度*2)+(长度*2));
}
双矩形::get_area()
{
返回(宽度*长度);
}
空心矩形::调整大小(双因素)
{ 
宽度*=系数;
长度*=系数;
}
因此,作为解释: 头文件告诉您哪些字段和方法可用,*.cpp文件实现这些方法

在main.cpp中,您只需要包含矩形。h

下面的代码可以工作

#include<iostream>
#include<iomanip>
using namespace std;
class student
{
private:
int area;//hours;
float perimeter;//gpa;
public:
void addcourse(int len, float wid)
{
float sum;
sum = area * perimeter;
area += len;
sum += wid * len;
perimeter = sum / area;
}
student()   // constructor
{
area = 0;
perimeter= 0.0;
}
};


int main()
{
student s;
int length;//classhours;//l
float width;//classgpa;//w
cout << "Enter length ( 1 to end):  ";
cin >> length;
while(length !=  1)
{
cout << "Enter width:  ";
cin >> width;

s.addcourse(length, width);

cout << "Enter length ( 1 to end):   ";
cin >> length;
}

// cout << "Rectangle's length = " << r.getlength() << endl;
//    cout << "Rectangle's width = " << r.getwidth() << endl;
//    cout << "Rectangle's area = " << r.getarea() << endl;
//   cout << "Rectangle's perimeter = " << r.getperimeter() << endl;
}
#包括
#包括
使用名称空间std;
班级学生
{
私人:
int区域;//小时;
浮动周长;//gpa;
公众:
无效addc