-1

I'm trying to understand how the hpp, cpp, and main all work together. for this example I'm working on a code that coverts ARGB to RGBA and I'm confused on what to put in each file.

This is my code:

color.hpp

using namespace std;
#include <stdio.h>
#include <stdio.h>
#include <iostream>
#ifndef colors_hpp
#define colors_hpp
 /* colors_hpp */

string getHex();
uint32_t fromArgb();
#endif

color.cpp

#include "colors.hpp"
#include <iostream>
#include <stdio.h>
#include <stdarg.h>
#include <stdint.h>

template <typename T>
struct Color
{
        public:
        /* Works fine!!! */
        Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255)
        {
            buffer((r << 0) | (g << 8) | (b << 16) | (a << 24));
        }

        Color(const uint32_t argb)
        {
          buffer = fromArgb(argb);
        }


        inline uint32_t fromArgb(uint32_t argb)
        {
            return
                // Source is in format: 0xAARRGGBB
                ((argb & 0x00FF0000) >> 16)  | //____RR
                ((argb & 0x0000FF00))        | //_GG_
                ((argb & 0x000000FF) << 16)  | //__BB_
                ((argb & 0xFF000000));         //AA____
                // Return value is in format:  0xAABBGGRR
        }

        inline uint8_t getRed(void) const
        {
            return (buffer >> 0) & 0xFF;
        }

        inline uint8_t getGreen(void) const
        {
            return (buffer >> 8) & 0xFF;
        }

        inline uint8_t getBlue(void) const
        {
            return (buffer >> 16) & 0xFF;
        }

        inline uint8_t getAlpha(void) const
        {
            return (buffer >> 24) & 0xFF;
        }

        /* Works fine!!!*/
        std::string getHex(void) const
        {
            std::string result    = "#";
            char colorBuffer[255] = {}; 

            // Order is intentionally end to beginning
            sprintf_s(colorBuffer, 255, "%.2X", getAlpha());
            result.append(colorBuffer);

            sprintf_s(colorBuffer, 255, "%.2X", getBlue());
            result.append(colorBuffer);

            sprintf_s(colorBuffer, 255, "%.2X", getGreen());
            result.append(colorBuffer);

            sprintf_s(colorBuffer, 255, "%.2X", getRed());
            result.append(colorBuffer);

            return result;
        }

        private:
         uint32_t buffer;
};

main.cpp

int main(int argc, char**argv) {
    fromArgb(255,255,0,0);
    getHex();
    
}

I'm not able to understand where to use or call the struct or functions, and i'm really confused on what to put in hpp, cpp, and main files.

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • TLDR put definitions in a cpp file and declarations in an hpp file. Of course if you don't know the difference that's awkward. – john Jun 27 '22 at 13:20
  • 1
    We can see that you have chosen what to put in each file. Do you have a question about this code? – Drew Dormann Jun 27 '22 at 13:25
  • This code does not compile. There is more wrong than what is in header files and cpp files. I think the first thing you should do is put everything in main.cpp, when it is working there you can start to move stuff to different files. Programming is hard enough as it is, get the code working first before you worry about the organisation. – john Jun 27 '22 at 13:25
  • 1
    [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Retired Ninja Jun 27 '22 at 13:34
  • Usual order for header files is 1. include guards, 2. includes and only then, *if at all*, using directives. Don't ever import entire namespaces in *headers* (see previous comment), you *will* break somebody's code that way... – Aconcagua Jun 27 '22 at 13:41
  • As in your representations both a and g do not change position you can get them in one single go: `| (argb & 0xff00ff00) // AA__GG__`. – Aconcagua Jun 27 '22 at 13:49

1 Answers1

0

Some advice

  1. Remove this template <typename T>

  2. Move struct Color { ... }; to color.hpp (all of it, you can delete color.cpp, it is not needed).

  3. Remove using namespace std; from color.hpp

  4. Remove string getHex(); uint32_t fromArgb(); from color.hpp

  5. change main to this

    int main(int argc, char**argv) {
        Color c;
        c.fromArgb(255,255,0,0);
        std::cout << c.getHex() << std::endl;
    }
    

The main problem seems to be that you don't know how objects work. In order to use the fromArgb and getHex methods you need a Color object. So in my code I declared a Color object like this Color c; and then I used that colour object like this c.fromArgb(255,255,0,0); and this c.getHex(). How to use classes and objects is a more important topic than how to organise your code into headers and cpp files.

I haven't tested these changes. If there are any further problems you can't figure out then ask again.

john
  • 85,011
  • 4
  • 57
  • 81
  • And the initialiser list, constructor should look like `Color(const uint32_t argb) : buffer(fromArgb(argb)) { }` – admitted, a bit more advanced, but it's easier to learn correctly right from the start than fighting bad habits later on ;) – Aconcagua Jun 27 '22 at 13:52
  • for 1. , i tried removing template but it gave me the error "Called object type 'uint32_t' (aka 'unsigned int') is not a function or function pointer" on the code "buffer((r << 0) | (g << 8) | (b << 16) | (a << 24));" – majd merhebi Jun 27 '22 at 14:11
  • for 2. then why do we need a cpp file? – majd merhebi Jun 27 '22 at 14:12
  • @majdmerhebi That line should read `buffer = ((r << 0) | (g << 8) | (b << 16) | (a << 24));` – john Jun 27 '22 at 14:40
  • @majdmerhebi In this case you do not need a cpp file – john Jun 27 '22 at 14:41
  • @majdmerhebi I think you need to spend some time studying, do you have a book on C++? At the moment you are making changes to code (like `template ` without having any idea what they mean, except that they make one particular error message go away. That is no way to learn how to program C++. – john Jun 27 '22 at 14:43
  • got it. thank a lot, i will do more research since i'm still a beginner who's learning – majd merhebi Jun 27 '22 at 14:48