I'm working on a C++ project set up in VS Code and it was working fine at first, but when trying to include a class file into another, it gives me the following error:
fatal error: source/math/vector2.cpp: No such file or directory 5 | #include "source/math/vector2.cpp"
This would suggest that my include path is incorrect, but VS Code does not give me an error squiggle, meaning VS Code sees the path as valid, but the compiler does not. I am aware that VS Code and the compiler are entirely separate but in the Makefile I'm using the compiler pulls the include directories from the project settings
What I thought might be the case was the fact that main.cpp is able to include vector.cpp because its in the folder "source", which both the renderer folder and the math folder are subfolders of, while referencing vector from the renderer class requires it to go up a level and then back down to a different subfolder but the problem persists when putting all files in the same folder, so that can't be it
Code is below, I don't think the contents of the renderer class would have an effect anything but I included it just in case
main.cpp
#include <time.h>
#include <nds.h>
#include <nf_lib.h>
#include "renderer/renderer.cpp"
#include "math/vector2.cpp"
#include "vlf_input.h"
#include "vlf_rand.h"
int main(int argc, char **argv) {
// Initialize the random function
vlf_rand::setSeed((long int)time(NULL));
// Initialize the console
consoleDemoInit();
consoleClear();
// Initialize Nitro File System
NF_SetRootFolder("NITROFS");
// Initialize the renderer
Renderer renderer = Renderer();
renderer.setBackground("bg/screen1", 256, 256, BOTTOM_SCREEN, 1);
while(1) {
// Input
scanKeys();
touchRead(&vlf_input::touch);
// Update
// Render
NF_SpriteOamSet(TOP_SCREEN);
NF_SpriteOamSet(BOTTOM_SCREEN);
swiWaitForVBlank();
oamUpdate(&oamMain);
oamUpdate(&oamSub);
}
return 0;
}
vector2.h
#ifndef VECTOR2_H
#define VECTOR2_H
struct Vector2 {
float x;
float y;
Vector2();
Vector2(const float& x, const float& y);
bool operator==(const Vector2& other);
bool operator!=(const Vector2& other);
Vector2 operator+(const Vector2& addend);
Vector2 operator-(const Vector2& subtrahend);
Vector2 operator*(const float& multiplier);
Vector2 operator/(const float& divisor);
float magnitude();
Vector2 normalized();
Vector2 clampMagnitude(const float& maxMagnitude);
};
#endif
vector2.cpp
#include <math.h>
#include "vector2.h"
Vector2::Vector2()
{
x = 0;
y = 0;
}
Vector2::Vector2(const float& x, const float& y)
{
this->x = x;
this->y = y;
}
bool Vector2::operator==(const Vector2& other)
{
return (x == other.x && y == other.y);
}
bool Vector2::operator!=(const Vector2& other)
{
return (x != other.x || y != other.y);
}
Vector2 Vector2::operator+(const Vector2& addend)
{
return Vector2(x + addend.x, y + addend.y);
}
Vector2 Vector2::operator-(const Vector2& subtrahend)
{
return Vector2(x - subtrahend.x, y - subtrahend.y);
}
Vector2 Vector2::operator*(const float& multiplier)
{
return Vector2(x * multiplier, y * multiplier);
}
Vector2 Vector2::operator/(const float& divisor)
{
return Vector2(x / divisor, y / divisor);
}
float Vector2::magnitude()
{
return sqrt(pow(x, 2) + pow(y, 2));
}
Vector2 Vector2::normalized()
{
float m = magnitude();
if (m == 0) return Vector2();
return Vector2(x / m, y / m);
}
Vector2 Vector2::clampMagnitude(const float& maxMagnitude)
{
float m = magnitude();
float multiplier = 1;
if (m > maxMagnitude) multiplier = maxMagnitude / m;
return Vector2(x * multiplier, y * multiplier);
}
renderer.h
#define RENDERER_H
#include <nf_lib.h>
#include "source/math/vector2.cpp"
// Screen
#define TOP_SCREEN 0
#define BOTTOM_SCREEN 1
class Renderer {
Vector2 viewportPosition;
static const char* getBackgroundName(u8 screen, u8 layer);
public:
Renderer();
~Renderer();
void setSpritePosition(u8 screen, u8 id);
void changeSpriteLayer(u8 screen, u8 id, u8 layer);
void setBackground(const char* file, u16 width, u16 height, u8 screen, u8 layer);
void deleteBackground(u8 screen, u8 layer);
void deleteAllBackgrounds();
void render();
};
#endif
renderer.cpp
#include "renderer.h"
#include "source/math/vector2.cpp"
#include <sstream>
#include <string.h>
Renderer::Renderer()
{
// Set both screens to 2D render mode
NF_Set2D(TOP_SCREEN, 0);
NF_Set2D(BOTTOM_SCREEN, 0);
// Initialize the background buffers
NF_InitTiledBgBuffers();
// Initialize the background system
NF_InitTiledBgSys(TOP_SCREEN);
NF_InitTiledBgSys(BOTTOM_SCREEN);
}
Renderer::~Renderer()
{
deleteAllBackgrounds();
}
const char* Renderer::getBackgroundName(u8 screen, u8 layer) {
std::string s = "";
s += std::to_string(screen);
s += std::to_string(layer);
return s.c_str();
}
void Renderer::setBackground(const char* file, u16 width, u16 height, u8 screen, u8 layer) {
// Get the background name from the screen and layer
const char* name = getBackgroundName(screen, layer);
// Load the background from FAT
NF_LoadTiledBg(file, name, width, height);
// Create the background
NF_CreateTiledBg(screen, layer, name);
// Delete from RAM if the background is less than 512x512
if (width <= 512 && height <= 512)
NF_UnloadTiledBg(name);
}
void Renderer::deleteBackground(u8 screen, u8 layer) {
// Get the background name from the screen and layer
const char* name = getBackgroundName(screen, layer);
// Delete and unload the background
NF_DeleteTiledBg(screen, layer);
NF_UnloadTiledBg(name);
}
void Renderer::deleteAllBackgrounds() {
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 4; j++)
{
deleteBackground(i, j);
}
}
}
void Renderer::render()
{
}
There is a chance this is not related to anything in my code and is rather related to the compiler, but if that was the case I would likely need to seek help elsewhere as the compiler I'm using is old and pretty niche for my purposes (devkitPro and libNDS for Nintendo DS development) so I won't provide details on that here. If you know anything about that, let me know and I'll make a separate question for it and tag you.
Any help would be appreciated. Thanks!