0

I had started to learn 'vectors in c++' but my first program stops like this...

//Program to print dot product of two vector

#include<iostream>
using namespace std;

class vector{
    public:
        int *arr;
        int size;
        vector (int m){
            size=m;
            arr= new int[size];

        }
    int dotProduct(vector &v){
        int d = 0;
        for(int i=0; i <size; i++){
            d += this->arr[i]*v.arr[i];
        }
        return 0;
    }
};

int main()
{
    vector v1(3);
    v1.arr[0]=4;
    v1.arr[1]=3;
    v1.arr[2]=1;
    vector v2(3);
    v2.arr[0]=1;
    v2.arr[1]=0;
    v2.arr[2]=1;
    int a=v1.dotProduct(v2);
    cout<<a<<endl;
    return 0;

}

It shows this as an error. 'v2' was not declared in this scope.

Another one is: class vector reference to 'vector' is ambiguous

  • Always solve compiler errors from the top one. Often, one error is followed by others. Here, the first error is that vector is ambiguous. This is because you decided to use `using namespace std;` and `std::vector` exists, so your compiler is confused what do you mean. Remove `using namespace std;` and use `std::cout` and `std::endl`, that should fix your error. – Yksisarvinen May 05 '22 at 17:17
  • As a note, your code leaks memory whenever a `vector` is destroyed – Lala5th May 05 '22 at 17:19
  • And once the leak is fixed correctly, `vector` does not observe [The Rule of Three (and friends)](https://en.cppreference.com/w/cpp/language/rule_of_three) and will fail if copied. – user4581301 May 05 '22 at 17:21
  • 'vector' is not a member of 'std'?? – DUMRE ANUP May 05 '22 at 17:24
  • 1
    What do you mean? You have your own class called `vector` and you want to use that, right? So once you remove `using namespace std;`, your own objects would be created like `vector v1(3);`. You need to fix `cout` and `endl` to `std::cout` and `std::endl` tho. – Yksisarvinen May 05 '22 at 17:30
  • solved, sir Lots of thanks... – DUMRE ANUP May 05 '22 at 17:38

0 Answers0