简体   繁体   中英

Accessing class pointers across several functions

I have a problem I've been stuck on for a while trying to make my code more efficient. I've created a Vector class and need to do some basic calculation with it. Using a vector library is out of the question, I need to create my own. The problem I have currently is in the final stage of the math. I can enter values for the first and second vector, but upon adding them together I get completely random numbers. I'm posting my header file and my cpp file - any help will be appreciated!!

Vectors.h

#include <math.h>
#include <iostream>

class Vectors
{
public:
    Vectors(void);
    ~Vectors(void);
    Vectors(double a1, double b1, double c1, double d1)
    {
        a = a1;
        b = b1;
        c = c1;
        d = d1;
    }
    void VectorAdd(Vectors vector1, Vectors vector2);
    void VectorSub();
    void VectorMulti();
    void VectorDiv();
    void VectorDP();
    void VectorCP();
    void setV1(Vectors &vector1);
    void setV2(Vectors &vector2);
private:
     double a;
     double b;
     double c;
     double d;
     double cp;
};

Cpp file

void Vectors::setV1(Vectors &vector1)
{
    Vectors *Vector1 = new Vectors();
    std::cout << "Enter the values of the first vector please.\n";
    std::cout << "a1: ";
    std::cin >> Vector1 -> a;
    std::cout << "b1: ";
    std::cin >> Vector1 -> b;
    std::cout << "c1: ";
    std::cin >> Vector1 -> c;
    std::cout << "d1: ";
    std::cin >> Vector1 -> d;
    Vector1 = &vector1;
    std::cin.get();
    std::cin.get();
}

void Vectors::setV2(Vectors &vector2)
{
    Vectors *Vector2 = new Vectors();
    std::cout << "Enter the values of the first vector please.\n";
    std::cout << "a1: ";
    std::cin >> Vector2 -> a;
    std::cout << "b1: ";
    std::cin >> Vector2 -> b;
    std::cout << "c1: ";
    std::cin >> Vector2 -> c;
    std::cout << "d1: ";
    std::cin >> Vector2 -> d;
    Vector2 = &vector2;
    std::cin.get();
    std::cin.get();
}

void Vectors::VectorAdd(Vectors vector1, Vectors vector2)
{

    setV1(vector1);
    setV2(vector2);

    Vectors *Vector3 = new Vectors();
    std::cout << "Here is the combination of the two vectors.\n";
    Vector3 -> a = vector1.a + vector2.a;
    std::cout << "a3: " << Vector3 -> a;
    Vector3 -> b = vector1.b + vector2.b;
    std::cout << "\nb3: " << Vector3 -> b;
    Vector3 -> c = vector1.c + vector2.c;
    std::cout << "\nc3: " << Vector3 -> c;
    Vector3 -> d = vector1.d + vector2.d;
    std::cout << "\nd3: " << Vector3 -> d;
    std::cin.get();
    std::cin.get();
}

Thank you in advance!

Vector2 = &vector2;

You did this backwards. You've overwritten the pointer to a new object you just initialized with a pointer to a completely uninitialized object, that you passed in here. The random data is in the uninitialized object, of course.

You don't need the

Vectors *Vector2 = new Vectors();

in the first place. Just initialize the vector2 parameter, directly, from std::cin . Ditto for the other function, setV1(), as well. Same thing.

I think the problem here is, you are confusing with pointer & reference.

In void Vectors::setV1(Vectors &vector1) , you are getting vector1 as reference. Next, you are creating a brand new object Vectors *Vector1 = new Vectors(); . And then you are continuing to fill *Vector1 . Till this point, I don't see anything weird. However, this part Vector1 = &vector1; totally damages the program. You are now re-assigning the pointer Vector1 with incoming address of vector1 .

Unless you have some value on the memory as pointed by vector1 you are not going to have correct results. Infact you are lucky, as you didn't say, your program generated SIGSEGV :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM