#Whats wrong with making this Utility function namespace ??

15 messages · Page 1 of 1 (latest)

young pond
#

I've got a Camera class that needs to use a random number generator.

For this I've defined a function in a different file (named "utils.hpp") and this file contains a namespace "Utility" which contains these helpful functions.

But I get the following error

src/camera.hpp: In member function ‘Ray Camera::ShootSampleRay(int, int)’:
src/camera.hpp:62:25: error: ‘Utility’ has not been declared
   62 |         float xOffset = Utility::get_random_number();
      |                         ^~~~~~~
src/camera.hpp:63:25: error: ‘Utility’ has not been declared
   63 |         float yOffset = Utility::get_random_number();
      |                         ^~~~~~~
make: *** [makefile:2: build] Error 1

why is this ? Code will be attached in a message down below

upbeat spearBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

young pond
#

This is utils.hpp

#ifndef UTILS_HPP
#define UTILS_HPP

// Important standard libs
#include <iostream>
#include <vector>
#include <memory>
#include <random>

// Main class headers
#include "vec3d.hpp"
#include "camera.hpp"
#include "ray.hpp"
#include "color.hpp"
// #include "triangle.hpp"
namespace Utility
{
    int clamp(int value , int maxValue , int minValue)
    {
        if (value > maxValue)
            return maxValue;
            
        else if (value < minValue)
            return minValue;
            
        else
            return value;
    }

    float get_random_number()
    {
        std::random_device rd; // obtain a random seed
        std::mt19937 gen(rd()); // seed the generator
        std::uniform_real_distribution<> dis(0.0, 1.0); // distribution between 0.0 and 1.0
        return dis(gen);
    }
}

#endif
#

This is camera.hpp

#ifndef CAMERA_HPP
#define CAMERA_HPP
#include "vec3d.hpp"
#include "ray.hpp"
#include "utils.hpp"

class Camera
{
public:
    Vec3D camPos;
    Vec3D frameOrigin;
    Vec3D xVec;
    Vec3D yVec;
    int xRes;
    int yRes;

    Camera()
        : camPos{0 , 6 , 0},
          frameOrigin{1 , 4 , 1},
          xVec{-2 , 0 , 0},
          yVec{0 , 0 , -2},
          xRes(256),
          yRes(256)
    {}

    Camera(Vec3D CamPos , Vec3D FrameOrigin , Vec3D XVec , Vec3D YVec , int XRes , int YRes)
        : camPos(CamPos),
          frameOrigin(FrameOrigin),
          xVec(XVec),
          yVec(YVec),
          xRes(XRes),
          yRes(YRes)
    {}

    Camera(int XRes , int YRes)
        : camPos{0 , 6 , 0},
          frameOrigin{1 , 4 , 1},
          xVec{-2 , 0 , 0},
          yVec{0 , 0 , -2},
          xRes(XRes),
          yRes(YRes)
    {}

    Ray ShootRay(int xCoord , int yCoord)
    {
        Vec3D xVecDx = xVec / xRes;
        Vec3D yVecDy = yVec / yRes;

        Vec3D rayDirection = (frameOrigin + (xVecDx * xCoord) + (yVecDy * yCoord)) - camPos;
        // (camPos + rayDirection).PrintVec();
        rayDirection = rayDirection / rayDirection.Magnitude();
        Ray shotRay = Ray(camPos , rayDirection);
        return shotRay;
    }


    Ray ShootSampleRay(int xCoord , int yCoord)
    {
        Vec3D xVecDx = xVec / xRes;
        Vec3D yVecDy = yVec / yRes;

        float xOffset = Utility::get_random_number();
        float yOffset = Utility::get_random_number();

        Vec3D rayDirection = (frameOrigin + (xVecDx * (xCoord + xOffset)) + (yVecDy * (yCoord + yOffset))) - camPos;
        // (camPos + rayDirection).PrintVec();
        rayDirection = rayDirection / rayDirection.Magnitude();
        Ray shotRay = Ray(camPos , rayDirection);
        return shotRay;
    }
};

#endif
opal trail
#

You have a circular import, so when you include utils.hpp, it will in turn include camera.hpp, which will in turn include utils.hpp (which will be an empty file essentially due to the header guard); now you start processing class Camera, but since there's no namespace Utility (as the header guard prevented us from declaring it the second time around) it errors out

#

The way to solve this is to not include camera.hpp in utils.hpp, or to forward declare your classes before the header guard or before the includes

young pond
#

Ohh okay got you

#

thanks so much !

#

i dont think im ever figuring out the include command

upbeat spearBOT
#

@young pond Has your question been resolved? If so, type !solved :)

opal trail
upbeat spearBOT
young pond
#

okk ill read through this

#

thanks again !

#

!solved