0

I am trying to create multiple instances of a static c++ library I wrote, but I can't create multiple instances of it... When I create two instances and write different data to them, I read the same data from both of the instances. Here is my code:

.cpp file:

    // MathFuncsLib.cpp
// compile with: cl /c /EHsc MathFuncsLib.cpp
// post-build command: lib MathFuncsLib.obj
/*
DECLARING VECTORS
|3,6,4|
|9,1,5|
|2,0,2|
|5,3,6|
Should be inputted as:
int a[] = {3,6,4,9,1,5,2,0,2,5,3,6} with x = 3 and y = 4

Inputting training vals:
|0.1 (inp1),0.1 (inp2) ,0.1 (targeted output)| depends on the number of inputs and outputs
|9,1,5|
|2,0,2|
|5,3,6|
*/
//#include "stdafx.h"
#include "vector.h"
#include "iostream"
#define DEBUG
#include <stdexcept>
//using namespace std;

    double* vectorLib::arrayPtr;
    int vectorLib::x;
    int vectorLib::y;

    vectorLib::vectorLib(int xInp, int yInp) {
        vectorLib::arrayPtr = new double[xInp*yInp];
        vectorLib::x = xInp;
        vectorLib::y = yInp;
        //return 0;
    }

    double vectorLib::sigmoid(double inp) {
        return 1 / (1 + exp(-inp));
    }

    double* vectorLib::getArrayPtr() {
        return vectorLib::arrayPtr;
    }

    double vectorLib::read(int xInp, int yInp) {
#ifdef DEBUG
        if (xInp >= vectorLib::x) {
            std::cout << "X_OUT_OF_BOUNDS_VECTOR_READ\n";
            while (1);
        }
        if (yInp >= vectorLib::y) {
            std::cout << "X_OUT_OF_BOUNDS_VECTOR_READ\n";
            while (1);
        }
#endif // DEBUG
        return *(arrayPtr + xInp + vectorLib::x*yInp);
    }

    void vectorLib::write(int xInp, int yInp, double data) {
#ifdef DEBUG
        if (xInp >= vectorLib::x) {
            std::cout << "X_OUT_OF_BOUNDS_VECTOR_WRITE\n";
            while (1);
        }
        if (yInp >= vectorLib::y) {
            std::cout << "X_OUT_OF_BOUNDS_VECTOR_WRITE\n";
            while (1);
        }
#endif // DEBUG
        vectorLib::arrayPtr[xInp + vectorLib::x*yInp] = data;
    }

    void vectorLib::writeArr(double* inpArr) {
        int i;
        for (i = 0; i < vectorLib::x*vectorLib::y; i++) {
            vectorLib::arrayPtr[i] = *(inpArr + i);
        }
    }

    void vectorLib::sigmoidVect() {
        int yy;
        int xx;
        for (yy = 0; yy < vectorLib::y; yy++) {
            for (xx = 0; xx < vectorLib::x; xx++) {
                write(xx, yy, sigmoid(read(xx, yy)));
            }
        }
        write(0, vectorLib::y - 1, 1);
    }

    int vectorLib::getX() {
        return vectorLib::x;
    }

    int vectorLib::getY() {
        return vectorLib::y;
    }

    int vectorLib::totalVectSize() {
        return vectorLib::x * vectorLib::y;
    }

    void vectorLib::printVector() {
        int yy;
        int xx;
        for (yy = 0; yy < y; yy++) {
            for (xx = 0; xx < x; xx++) {
                std::cout << vectorLib::read(xx, yy);
                if (xx + 1 != x) {
                    std::cout << ",";
                }
            }
            std::cout << "\n";
        }
    }

    vectorLib* vectorLib::vectorMult(vectorLib* vect1, vectorLib* vect2) {
#ifdef DEBUG
        if (vect1->getX() != vect2->getY()) {
            std::cout << "INPUTS_DONT_MATCH_VECTORMULT\n";
            while (1);
        }
#endif // DEBUG
        vectorLib toRet(vect1->getX(), vect2->getY());
        int i;
        for (i = 0; i < vect2->getX(); i++) {
            int p;
            for (p = 0; p < vect1->getY(); p++) {
                double tempOut = 0;
                int q;
                for (q = 0; q < vect1->getX(); q++)
                {
                    tempOut += vect1->read(q, p) * vect2->read(i, q);
                }
                toRet.write(i, p, tempOut);
            }
        }

        return &toRet;
    }

.h file:

    //#include "stdafx.h"

using namespace std;
    class vectorLib
    {
        //int x, y;
    public:
        static double* arrayPtr;
        static int x;
        static int y;
        //Constructor takes x and y of the vector
        vectorLib(int xInp, int yInp);
        //The pointer to the array that holds all the doubles in the vector
        static double* getArrayPtr();
        //Read the vector at a specified x and y
        static double read(int xInp, int yInp);
        //Write one double to a specific location
        static void write(int xInp, int yInp, double data);
        //Write the array inside the vector class
        static void writeArr(double* inpArr);
        //Takes sigmoid of whole vector
        static void sigmoidVect();
        //Returns x of vector
        static int getX();
        //Returns y of vector
        static int getY();
        //Returns total size of vector
        static int totalVectSize();
        //Returns a vector pointer to the multiplication result
        static vectorLib* vectorMult(vectorLib* vect1, vectorLib* vect2);
        //Prints vector
        static void printVector();
    private:
        static double sigmoid(double inp);
    };

Main file:

    #define DEBUG
#include "stdafx.h"
#include "vector.h"
#include "iostream"

using namespace std;

int main()
{
    vectorLib testVectLol(1, 3);
    vectorLib testVect(3, 4);
    double vectInp[] = { 1,1,1,
                        1,1,1,
                        1,1,1,
                        1,1,1};
    double vectInp2[] = { 0.5,0.5,0.5 };
    testVect.writeArr(vectInp);
    testVectLol.writeArr(vectInp2);

    testVect.printVector();// Both print 0.5, 0.5, 0,5
    testVectLol.printVector();// Both print 0.5, 0.5, 0,5
    while (1);
    return 0;
}

Thanks in advance! I've been struggling with this for hours. I would really appreciate any help!

Jasper

  • 3
    I don't think `static` does what you think it does. You may want to consult a book or tutorial about that keyword. – Borgleader Jan 19 '17 at 16:41
  • What do you mean by two instances of a library? Do you mean two instances of a class? – Barmar Jan 19 '17 at 16:41
  • The `static` keyword is completely different from the idea of a static library. A `static` variable will share its value across all instances of a class. A `static` function is meant to be called from the class itself rather than from an instance. On the other hand, when a library is static, that refers to the fact that it has static linkage (as opposed to dynamic). – 0x5453 Jan 19 '17 at 16:42
  • Okay thanks a lot! I should be focussing on a dynamic library instead? – Jasper Insinger Jan 19 '17 at 16:48
  • 2
    ...what do you think "library" means, in this context? It really feels like you're confusing "library" and "class," which is going to make a world of difference in how confused you get. – jaggedSpire Jan 19 '17 at 16:51
  • Got it figured out!! Thanks a lot for your help! – Jasper Insinger Jan 19 '17 at 17:07
  • Write an answer then. – n. m. could be an AI Jan 19 '17 at 17:13
  • In this context I'm using a library as a collection of functions that inherits several specific properties – Jasper Insinger Jan 19 '17 at 18:03
  • @JasperInsinger A library is a specific thing in C++-land. Specifically, it's [a set of functionality accessible through function calls, implemented in a compiled binary.](http://stackoverflow.com/a/924503/4892076) There are dynamic and statically linked libraries, but neither are produced by making a class and giving it static functions--indeed, Dynamically linked libraries (dlls) are commonly made with all functionality hidden behind a C-compatible interface. – jaggedSpire Jan 19 '17 at 18:22

0 Answers0