简体   繁体   English

C ++错误:没有在范围内声明; 会员功能

[英]C++ Error: Not declared in scope; Member function

I have a an error for 'Result Vector' was not declared in this scope. 我在此范围内未声明“结果向量”时出错。 I am not sure of where to or how to declare this? 我不确定该在哪里或如何声明? The intention of the Result Vector is the show the result of adding the X's of vectors and the Y's of each vector and then return result_vector 结果向量的意图是显示将向量的X与每个向量的Y相加然后返回result_vector的结果

#include <iostream>
#include <vector>
using namespace std;

class vector{
private:
    double x;
    double y;
public:
    //Constructor - default
    vector() : x(0), y(0) {}
    //Constructor - Custom
    vector(double xx, double yy) : x(xx), y(yy) {}
    //Get X & Y Coordinates
    double get_x() { return x;
    }
    double get_y() { return y;
    }
    //Set X & Y Coordinates
    void set_x( double xx) { x = xx;
    }
    void set_y( double yy) { y = yy;
    }
    //Adding Vectors
    vector add_vector( vector v1, vector v2){ 
        result_vector.x = v1.x + v2.x;
        result_vector.y = v1.y + v2.y;
        return result_vector; 
    }
    //Subtracting Vectorsed
    vector subtract_vector( vector v1, vector v2){
        result_vector.x = v1.x - v2.x;
        result_vector.y = v1.y - v2.y;
        return result_vector;
    }
};

int main() {
    //Default
    vector test;
    cout <<"Default \n" test.get_x().get_y() << "\n";
    //Customer
    vector test2(10, 12);
    cout <<"Custom \n" test2.get_x().get_y() << "\n";
    //Adding
    vector add = vector.add_vector(vector test1&, vector test2&);
    cout <<"Adding \n" add.get_x().get_y() <<"\n";
    //Subtracting
    vector sub = vector.subtract_vector(vector test1&, vector test2&);
    cout <<"Subtracting \n" sub.get_x().get_y() <<"\n";

    return 0;


}

You haven't declared result_vector it doesn't exist, hence the error. 您尚未声明result_vector不存在,因此发生了错误。

Also, you're using namespace std; 另外,您正在using namespace std; and you've named a class vector . 并且您已经命名了一个class vector
therefore the reference to vector is ambiguous. 因此,对vector的引用是不明确的。

Your code has several other errors, I suggest you use a debugger and go through the errors 您的代码还有其他几个错误,建议您使用调试器并仔细检查错误

The error is self explanatory. 该错误不言自明。

  • You need to declare result_vector before its first use. 您需要在第一次使用result_vector之前对其进行声明。
  • You can't pass the class name along with object name as arguments. 您不能将类名和对象名作为参数传递。
  • Assuming this statement cout <<"Default \\n" test.get_x().get_y() << "\\n"; 假设此语句cout <<"Default \\n" test.get_x().get_y() << "\\n"; is supposed to print the x and y values of vector, you will have to call get_x() and get_y() separately. 应该打印出向量的x和y值,则必须分别调用get_x()get_y()
  • Also as mentioned by Andreas DM you can't use using namespace std; 同样,正如Andreas DM所提到的,您不能使用using namespace std; because it conflicts with the existing class vector.vector class after making the corrections 因为它在进行更正后与现有的类vector.vector类冲突

     #include <iostream> #include <vector> class vector{ private: double x; double y; public: //Constructor - default vector() : x(0), y(0) {} //Constructor - Custom vector(double xx, double yy) : x(xx), y(yy) {} //Get X & Y Coordinates double get_x() { return x; } double get_y() { return y; } //Set X & Y Coordinates void set_x( double xx) { x = xx; } void set_y( double yy) { y = yy; } //Adding Vectors vector add_vector( vector v2){ vector result_vector; result_vector.x = x + v2.x; result_vector.y = y + v2.y; return result_vector; } //Subtracting Vectorsed vector subtract_vector( vector v2){ vector result_vector; result_vector.x = x - v2.x; result_vector.y = y - v2.y; return result_vector; } }; int main() { //Default vector test; std::cout <<"Default \\n" <<test.get_x()<<test.get_y() << "\\n"; //Customer vector test2(10, 12); std::cout <<"Custom \\n" <<test2.get_x()<<test2.get_y() << "\\n"; //Adding vector add; add = test.add_vector(test2); std::cout <<"Adding \\n" <<add.get_x()<<add.get_y() <<"\\n"; //Subtracting vector sub ; sub = test.subtract_vector(test2); std::cout <<"Subtracting \\n"<< sub.get_x()<<sub.get_y() <<"\\n"; return 0; } 

Before you use a variable, you have to declare it, so that the compiler knows it exits. 在使用变量之前,必须先声明它,以便编译器知道它已退出。 In C++ this is required (some languages, like Python, don't require it), and so declare result_vector first before you use it ( vector result_vector; ) 在C ++中,这是必需的(某些语言,例如Python,不需要它),因此在使用它之前先声明result_vectorvector result_vector;

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

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