通过参考返回C 中的对象



我设定的目标是超载 operator+(添加类对象(。事实证明,这一和可以将其解释为两个向量的总和。但是,当涉及方法operator+时,我发现很难返回对象。我读过类似的话题,甚至尝试应用一些Subenions,但没有成功,不幸。我包装了一些代码。

template<class Y>
class myVect {
public:
    myVect(int n = 1);                          
    ~myVect();
    myVect(const myVect& a);                    
    myVect& operator= (const myVect&);
    myVect& operator+ (const myVect&);
    void display(const myVect& a);      
private:
    int size;
    Y* data;
    template<class U> friend class myClass;     
}; 
template<class Y>                               // constructor      
myVect<Y>::myVect(int n) {
    size = n;
    data = new Y[size];
    cout << endl << "Pass the elements" << " " << size << "n";
    for (int i = 0; i < size; i++) {
        cin >> *(data + i);
    }
}
template <class Y>                             // deconstructor                 
myVect<Y> :: ~myVect() {
    delete[] data;
}

template<class Y>                               // copy constructor
myVect<Y> ::myVect(const myVect & a) {
    size = a.size;
    data = new Y[size];                         
    for (int i = 0; i < size; i++) {
        *(data + i) = *(a.data + i);            
    }
}
template<class Y>                              //ASSIGMENT OPERATOR                                                 
myVect<Y> & myVect<Y> :: operator= (const myVect<Y> & a) {
    if (this != &a) {                                                   
        delete[] data;                                                  
        size = a.size;                                                  
        data = new Y[size];
        for (int i = 0; i < size; i++) {
            *(data + i) = *(a.data + i);
        }
    }
    return *this;
}

方法运算符 是一个:

template<class Y>
myVect<Y>& myVect<Y> ::operator+ (const myVect<Y>& a) {
    if (this->size != a.size) {
    cout << endl << "not able to perform that operation - wrong dimensions" << endl;
    }
    else {
        myVect<Y> newObj(this->size);
        for (int i = 0; i < this->size; i++) {
            *(newObj.data + i) = *(this->data + i) + *(a.data + i);
        }
    }
    return newObj;                                                      
}       

我得到的错误是'newobj':找不到标识符。我相信这是由于解构者的原因。我试图将myVect类放入新类(封装(并构成返回方法,但没有改变愤怒 - 错误的类型仍然相同。你知道如何解决这个问题吗?

无论如何,如果这是破坏者的故障,这是否意味着newObj在返回之前已删除?

可以将问题简化为:

int foo()
{
    if (true)  // In reality, some meaningful condition
    {
       int x = 4;
    }
    return x;
}

变量范围 if块。它不存在于此。

您必须将其声明从条件中移出,并采取其他任何需要做出的工作和地狱。或内部return,然后做其他事情(抛出异常?(。

例如,给定上述演示:

int foo()
{
    int x = 0; // Or some other value
    if (true)  // In reality, some meaningful condition
    {
       x = 4;
    }
    return x;
}

或:

int foo()
{
    if (true)  // In reality, some meaningful condition
    {
       int x = 4;
       return x;
    }
    throw std::runtime_error("For some reason I have no value to give you!");
}

您的下一个问题将是您试图通过参考返回本地变量。你不能这样做。取而代之的是,以价值返回,无论如何,这对于您正在做的事情都是惯用的。

您已将对象声明在一个块内,因此在外部范围中不存在。通常,这通常会让您释放不同分支机构的变量名称;尝试在语句的if内部制作newObj,并观看不是,例如。

最新更新