【问题标题】:Using header file in C++ program在 C++ 程序中使用头文件
【发布时间】:2013-05-22 07:38:31
【问题描述】:

我有一些关于使用头文件编码 C++ 程序的问题。

这是我的 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;
double factor;
};

这是我存储所有方法的 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;
};

最后,这是我的主要方法.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;
}

但是,当我尝试运行该程序时,系统告诉我存在构建错误和未解决的外部问题,我不知道为什么会这样。有人可以帮我解决吗?

提前致谢。

【问题讨论】:

  • 你需要在你的questestion cpp文件中包含header.h,你还必须使用this指针:this->width = width;
  • 不要在标题中使用using namespace ...
  • 有什么错误?哪些行导致了错误?你检查过sscce.org 吗?
  • question1.cpp line 5中的矩形类重定义
  • 你可以使用this-&gt;width = width

标签: c++ class header


【解决方案1】:

你的实现不应该是这样的

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

但喜欢

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

您需要在实现.cpp 文件和任何需要定义Rectangle 类的文件中包含header.h。您的标题中还需要include guards。并且不要将 using namespace std 放在标题中。事实上,不要把它放在任何地方。

【讨论】:

  • 所以我只是将#include 保护写入头文件?但是有一个错误
  • @Yvonne 阅读包含警卫的链接。它会告诉你该怎么做。
【解决方案2】:

将 .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;
};

然后.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;
}

这应该可以工作。 问候, 卢卡

【讨论】:

    【解决方案3】:

    这里有几件事要取消。

    1) 使用this-&gt;width,它相当于java 的this.width(在C++ 中,这是一个指针)。实际上,一些 C++ 程序员(包括我)在成员变量前面加上 m_。然后你可以写m_width = width

    2) 在 Question1.cpp 的顶部包含“header.h”

    3) 避免将“using namespace 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
    

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

    【讨论】:

      【解决方案4】:

      在 Question1.cpp 中你必须包含 header.h

      别忘了在 header.h 中包含守卫

      同样在 Question1.cpp 你必须改变

      Rectangle(double width, double length)
      

      Rectangle::Rectangle(double width, double length)
      

      【讨论】:

        【解决方案5】:

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

        通常,您的类和构造函数会使用与输入参数不同的名称。很多人在开头加前缀(mLength,iLength)或在结尾加后缀(length_很常见)。

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

        【讨论】:

          【解决方案6】:

          在 .cpp 文件中你应该只实现方法而不重写完整类实现的大错误尝试在 .cpp 文件中执行以下操作

          矩形::矩形(双倍宽,双倍长) { 宽度=宽度; //我不知道如何在 Java 中使用 this.something 长度=长度; //问题可能发生在这里 } 并且不要在 class{} 中包含方法;阻止并且不要重新定义您的成员变量 也不要忘记在 .cpp 文件中包含头文件

          谢谢

          【讨论】:

            【解决方案7】:

            我想知道您是否收到链接器错误,因为您的 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
            }
            

            【讨论】:

              【解决方案8】:

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

              矩形.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
              

              现在 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;
              }
              

              所以当作为解释: 头文件告诉您哪些字段和方法可用,*.cpp 文件实现这些方法。

              在你的 main.cpp 中你只需要包含 rectangle.h

              【讨论】:

                【解决方案9】:

                下面的代码有效

                #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;
                }
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2012-11-02
                  • 2011-03-20
                  • 1970-01-01
                  • 2011-08-10
                  • 2017-08-23
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多