在C ++中定义头文件

时间:2016-03-09 22:30:40

标签: c++ header

首先,我提到我主要来自Java背景。我确实接触过C并理解C ++背后的大多数概念。我试图帮助自己了解有关该语言的更多信息,而且似乎无法找出标题。我理解为什么除了cpp文件和所有这些之外还要使用它们。我的问题是尝试实际管理与他们合作。例如,使用私有浮点变量定义Vector3标头,然后重载操作。当我尝试在cpp文件中定义构造函数和方法时,我的问题就出现了。我似乎无法弄清楚如何在没有专门定义标题中的函数和构造函数的情况下访问私有变量,这或多或少让我相信我不需要头文件和cpp在这个例子中的文件。

以下是我目前如何定义头文件(哪个有效,但未定义):

#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

3 个答案:

答案 0 :(得分:2)

它需要看起来更像这样:

Vector_3.h:

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

}

Vector_3.cpp:

SpriteBatch batch = new SpriteBatch();
Sprite star = new Sprite(new Texture("star.png"));

public void render(float delta) {
     batch.begin();
     batch.draw(star, 100, 100, star.getOriginX(), star.getOriginY(),
       star.getWidth(), star.getHeight(), star.getScaleX(), star.getScaleY(),
       star.getRotation());
     batch.end;
}

答案 1 :(得分:0)

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

// File Vector3.cpp
#include "Vector3.h"

namespace VectorMath {

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

如果将其保存在同一名称空间

中,则应按如下方式实现添加
    Vector3 Vector3::operator+(const Vector3& v)
    { 
        return Vector3 (x+v.x,y+v.y,z+v.z);
    }
}

答案 2 :(得分:0)

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

// 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
// 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 {