创建c+的多个实例+;图书馆 我试图创建一个静态C++库的多个实例,但是我不能创建它的多个实例…当我创建两个实例并向它们写入不同的数据时,我从两个实例中读取相同的数据。这是我的密码:

创建c+的多个实例+;图书馆 我试图创建一个静态C++库的多个实例,但是我不能创建它的多个实例…当我创建两个实例并向它们写入不同的数据时,我从两个实例中读取相同的数据。这是我的密码:,c++,C++,.cpp文件: // 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

.cpp文件:

    // 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;
    }
主文件:

    #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;
}
提前谢谢!我已经为此挣扎了好几个小时了。我真的很感激任何帮助


贾斯珀

我不认为
静态
能做你认为它能做的事。你可能想查阅一本关于这个关键词的书或教程。你所说的两个库实例是什么意思?你是指一个类的两个实例吗?
static
关键字与静态库的概念完全不同。
静态变量将在类的所有实例中共享其值。
静态
函数是从类本身而不是从实例调用的。另一方面,当一个库是静态的时,这意味着它有静态链接(与动态链接相反)!我应该把重点放在动态库上?…在这种情况下,你认为“库”是什么意思?这真的让人觉得你把“图书馆”和“课堂”搞混了,这会让你的困惑程度大不相同。
    #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;
}