首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在C++中定义头文件

在C++中定义头文件
EN

Stack Overflow用户
提问于 2016-03-09 22:30:39
回答 3查看 2.7K关注 0票数 1

首先,我将提到我主要来自Java背景。我确实接触过C,并且理解了C++背后的大多数概念。我试图帮助自己更多地了解这门语言,但似乎找不出标题。我理解为什么要在cpp文件和所有这些文件之外使用它们。我的问题是设法和他们一起工作。例如,使用私有浮动变量定义Vector3头,然后进行重载操作。当我试图在cpp文件中定义构造函数和方法时,我的问题就出现了。如果不明确定义头文件中的函数和构造函数,我似乎无法理解如何访问私有变量,这或多或少使我相信在这种情况下我不需要头文件和cpp文件。

下面是我当前如何定义头文件(它可以工作,但不应该是未定义的):

代码语言:javascript
复制
#pragma once

#ifndef __Vector_3_H__
#define __Vector_3_H__

namespace VectorMath {

class Vector3 {

public:

    Vector3(float x, float y, float z) {
        this->x = x;
        this->y = y;
        this->z = z;
    }

    Vector3 operator+(Vector3 vector) {
        return Vector3(x + vector.x, y + vector.y, z + vector.z);
    }

    Vector3 operator-(Vector3 vector) {
        return Vector3(x - vector.x, y - vector.y, z - vector.z);
    }

    Vector3 operator*(Vector3 vector) {
        return Vector3(x * vector.x, y * vector.y, z * vector.z);
    }

    Vector3 operator/(Vector3 vector) {
        return Vector3(x / vector.x, y / vector.y, z / vector.z);
    }

    float getX() {
        return x;
    }

    float getY() {
        return y;
    }

    float getZ() {
        return z;
    }

private:

    float x;
    float y;
    float z;

};
}

#endif
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-03-09 22:44:51

它需要看起来更像这样:

Vector_3.h:

代码语言:javascript
复制
#ifndef Vector_3_H
#define Vector_3_H

#pragma once

namespace VectorMath {

class Vector3 {

public:

    Vector3(float x, float y, float z);

    Vector3 operator+(Vector3 vector);    
    Vector3 operator-(Vector3 vector);
    Vector3 operator*(Vector3 vector);
    Vector3 operator/(Vector3 vector);

    float getX();
    float getY();
    float getZ();

private:

    float x;
    float y;
    float z;

};

}

#endif

Vector_3.cpp:

代码语言:javascript
复制
#include "Vector_3.h"

namespace VectorMath {

Vector3::Vector3(float x, float y, float z) {
    this->x = x;
    this->y = y;
    this->z = z;
}

Vector3 Vector3::operator+(Vector3 vector) {
    return Vector3(x + vector.x, y + vector.y, z + vector.z);
}

Vector3 Vector3::operator-(Vector3 vector) {
    return Vector3(x - vector.x, y - vector.y, z - vector.z);
}

Vector3 Vector3::operator*(Vector3 vector) {
    return Vector3(x * vector.x, y * vector.y, z * vector.z);
}

Vector3 Vector3::operator/(Vector3 vector) {
    return Vector3(x / vector.x, y / vector.y, z / vector.z);
}

float Vector3::getX() {
    return x;
}

float Vector3::getY() {
    return y;
}

float Vector3::getZ() {
    return z;
}

}
票数 3
EN

Stack Overflow用户

发布于 2016-03-09 22:42:20

如果要为构造函数使用cpp文件,则应编写

代码语言:javascript
复制
// File Vector3.cpp
#include "Vector3.h"

namespace VectorMath {

    Vector3::Vector3 (float x, float y, float z)         
    { 
        this->x=x;
       //...
    }

如果将加法保留在相同的命名空间中,则应按如下方式实现

代码语言:javascript
复制
    Vector3 Vector3::operator+(const Vector3& v)
    { 
        return Vector3 (x+v.x,y+v.y,z+v.z);
    }
}
票数 0
EN

Stack Overflow用户

发布于 2016-03-09 22:47:24

如果要将成员函数的实现从头文件移开,仍然需要在类的定义中声明。例如:

代码语言:javascript
复制
// Vector1.h
#pragma once
#ifndef VectorMath_Vector1_H
#define VectorMath_Vector1_H


namespace VectorMath {

class Vector1 {

public: // Methods:

    // This is a definition for a default constructor:
    Vector1() noexcept : m_x(0) {}

    // This is a declaration for another constructor:
    Vector1(float x) noexcept;

    // This is a declaration of a member function:
    Vector1 operator+(Vector1 const & rhs) const noexcept;

private: // Fields:

    float m_x;

}; // class Vector1

} // namespace VectorMath {

#endif // VectorMath_Vector1_H
代码语言:javascript
复制
// Vector1.cpp
#include "Vector1.h"


namespace VectorMath {

// Definition of the other constructor:
Vector1::Vector1(float x) noexcept
    : m_x(x)
{}

// Definition of the binary + operator:
Vector1 Vector1::operator+(Vector1 const & rhs) const noexcept
{ return m_x + rhs.m_x; }

} // namespace VectorMath {
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35904001

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档