dimanche 3 janvier 2021

C++ class has some members in scope, and others out of scope

I'm trying to learn Vulkan programming in C++. I was following the Intel API Without Secrets and the Vulkan Cookbook and ran into a roadblock when the macro-based approach they take to dynamically load functions at runtime didn't work. So instead, I'm trying to dynamically load those vulkan functions by making them public static members of a class, and let the class constructor load them for me.

The problem I'm having is that some of the public member variables in my class are declared out of scope, for reasons I can't understand. My code is very simple, so I'll post it in it's entirety. I've created a simple case where I do the exact same thing, and it works. What am I missing here?

Here's the error.

-------------- Build: Debug in vulkan3 (compiler: GNU GCC Compiler)---------------

g++ -Wall -std=gnu++11 -std=c++11 -g -std=gnu++11 -Iinclude -c /data/home/kenneth/Desktop/Programming/c++/vulkan3/src/main.cpp -o obj/Debug/src/main.o 
g++ -Wall -std=gnu++11 -std=c++11 -g -std=gnu++11 -Iinclude -c /data/home/kenneth/Desktop/Programming/c++/vulkan3/src/VulkanFunctions.cpp -o obj/Debug/src/VulkanFunctions.o 
g++  -o bin/Debug/vulkan3 obj/Debug/src/main.o obj/Debug/src/VulkanFunctions.o  -ldl  
/data/home/kenneth/Desktop/Programming/c++/vulkan3/src/VulkanFunctions.cpp: In constructor ‘k_vulkan::VulkanFunctions::VulkanFunctions()’:
/data/home/kenneth/Desktop/Programming/c++/vulkan3/src/VulkanFunctions.cpp:18:9: error: ‘num’ was not declared in this scope; did you mean ‘enum’?
   18 |         num = 5; // <-not in scope
      |         ^~~
      |         enum
/data/home/kenneth/Desktop/Programming/c++/vulkan3/src/VulkanFunctions.cpp:20:9: error: ‘vkEnumerateInstanceExtensionProperties’ was not declared in this scope; did you mean ‘PFN_vkEnumerateInstanceExtensionProperties’?
   20 |         vkEnumerateInstanceExtensionProperties =
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |         PFN_vkEnumerateInstanceExtensionProperties

Here's the header file.

#ifndef VULKANFUNCTIONS_H
#define VULKANFUNCTIONS_H

#define VK_NO_PROTOTYPES
#include <vulkan/vulkan.h>

#if defined _WIN32
#define LoadFunction GetProcAddress
#elif defined __linux
#define LoadFunction dlsym
#endif // defined

namespace k_vulkan
{
    class VulkanFunctions
    {
        public:
            VulkanFunctions();
            virtual ~VulkanFunctions();
            bool foo();

            int num;    // <- not in scope
            static PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr; // <- in scope
            static PFN_vkEnumerateInstanceExtensionProperties vkEnumerateInstanceExtensionProperties;  //<- not in scope

        protected:

        private:
            static void *vulkan_library;  // <- in scope

    };
}
#endif // VULKANFUNCTIONS_H

And here's the implementation.

#include "VulkanFunctions.h"
#include <vulkan/vulkan.h>
#include "dlfcn.h"
#include <string>
#include <iostream>


namespace k_vulkan
{
    VulkanFunctions::VulkanFunctions()
    {
#if defined _WIN32
this->vulkan_library = LoadLibrary("vulkan-1.dll");
#elif defined __linux
this->vulkan_library = dlopen("libvulkan.so.1", RTLD_NOW);
#endif // defined

        num = 5; // <-not in scope
        vkGetInstanceProcAddr = (PFN_vkGetInstanceProcAddr)LoadFunction(vulkan_library, "vkGetInstanceProcAddress"); // <- in scope
        vkEnumerateInstanceExtensionProperties =
            (PFN_vkEnumerateInstanceExtensionProperties)vkGetInstanceProcAddr(nullptr, "vkEnumerateInstanceExtensionProperties");
        // ^^^^ not in scope ^^^

    }

    bool VulkanFunctions::foo()
    {
        std::cout << "hello?" << std::endl;
        return true;
    }

}

Aucun commentaire:

Enregistrer un commentaire