pan.h
#pragma once
#include "vector.h"
namespace math_ {
void worldToScreen(const Vec2& world, Vec2& screen, const Vec2& offset);
}
pan.cpp
#include "pan.h"
namespace math_{
void worldToScreen(const Vec2& world, Vec2& screen, const Vec2& offset)
{
screen = world + offset;
}
}
vector.h
#pragma once
namespace math_ {
struct Vec2
{
Vec2(float x_, float y_);
float x = 0.0f;
float y = 0.0f;
Vec2 operator +(const Vec2& other);
Vec2 operator -(const Vec2& other);
};
}
vector.cpp
#include "vector.h"
namespace math_ {
Vec2::Vec2(float x_, float y_) :
x(x_), y(y_) {}
Vec2 Vec2::operator+(const Vec2& other)
{
return Vec2(this->x + other.x, this->y + other.y);
}
}
vector.h
包含在pan.h
中,我試圖在函數worldToScreen
中添加兩個向量,但是我得到了一個錯誤no operator + matches these operands
,盡管它已在vector.cpp文件中重載。
我不認為這是重載本身的問題,因為當我試圖在一個文件中執行完全相同的操作時,文件中的所有內容都有效。只是當我試圖把東西分成不同的文件時,才發現有問題。一定是我做錯了什么,但我不知道是什么。
重載未標記為
const
,但您是從const Vec2&
調用它: