简体   繁体   English

跨多个函数访问类指针

[英]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. 我已经创建了Vector类,并需要对其进行一些基本计算。 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!! 我正在发布我的头文件和cpp文件-任何帮助将不胜感激!

Vectors.h 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 Cpp文件

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 . 只需直接从std::cin初始化vector2参数即可。 Ditto for the other function, setV1(), as well. 同样,其他函数setV1()也是如此。 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. void Vectors::setV1(Vectors &vector1) ,您将获得vector1作为参考。 Next, you are creating a brand new object Vectors *Vector1 = new Vectors(); 接下来,您将创建一个全新的对象Vectors *Vector1 = new Vectors(); . And then you are continuing to fill *Vector1 . 然后,您将继续填充*Vector1 Till this point, I don't see anything weird. 到此为止,我什么都看不到。 However, this part Vector1 = &vector1; 但是,这部分Vector1 = &vector1; totally damages the program. 完全损坏程序。 You are now re-assigning the pointer Vector1 with incoming address of vector1 . 现在,您使用传入地址vector1 重新分配指针Vector1

Unless you have some value on the memory as pointed by vector1 you are not going to have correct results. 除非您在vector1指出的内存上具有某些值,否则您将不会获得正确的结果。 Infact you are lucky, as you didn't say, your program generated SIGSEGV :) 实际上,您很幸运,正如您未说的那样,您的程序生成了SIGSEGV :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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