Skip to main content
Senior
July 31, 2024
Solved

3D vectors in C++

Hi,

recently I detected "complex.h" and the float complex data type for C++ project files. Since then dealing with complex numbers got very easy.

 

I would appreciate the same ease of use for 3D float vectors, i.e. I need a float vector data type and the vector operators.

Is there a comparable include for 3D vectors?

THX

Cheers

Detlef

 

 

    This topic has been closed for replies.
    TDK
    TDKBest answer
    July 31, 2024

    Fixed length arrays are implemented in <array> in C++.

    #include <array>
    
    std::array<float, 3> vector;

     

    std::array - cppreference.com

    "If you feel a post has answered your question, please click ""Accept as Solution""."

    5 replies

    Andrew Neil
    Super User
    July 31, 2024

    I would think this would be generic C++ - not specific to STM32 or CubeIDE ?

    What does your favourite internet search engine say?

    Have you tried  a search on GitHub ?

     

    eg, https://github.com/cdelv/Vector3D 

    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
    DetlefSAuthor
    Senior
    July 31, 2024

    Hi,

    the very first lines of the code in github 

    #include <iostream>
    #include <cmath>
    using namespace std; 

    show errors on CubeIDE.

    I'm good in C but a C++ newbie. Fixed length vectors are not integrated in C++, as far as I know. Reportedly there are some 3D vector implementations out in the wild. I have the hope this is already included in the compiler for CubeIDE !?

    Cheers

    Detlef

     

    TDK
    TDKBest answer
    July 31, 2024

    Fixed length arrays are implemented in <array> in C++.

    #include <array>
    
    std::array<float, 3> vector;

     

    std::array - cppreference.com

    "If you feel a post has answered your question, please click ""Accept as Solution""."
    DetlefSAuthor
    Senior
    July 31, 2024

    Yes,

    it works.

     

    The pitfall: 'target language C++' is chosen, but you have to rename main.c to main.cpp manually. For main.c gcc throws an error, g++ for main.cpp ist working fine.

     

    THX

    Cheers

    Detlef

    Andrew Neil
    Super User
    July 31, 2024

    @DetlefS wrote:

    The pitfall: 'target language C++' is chosen, but you have to rename main.c to main.cpp manually.


    Now that is a bit dumb. :face_with_rolling_eyes:

     


    @DetlefS wrote:

    For main.c gcc throws an error,lef


    Yes: GCC will, by default, compile a file with .c extension as C - not as C++

    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.