-2

I'm currently trying to display the dot product by calling a dotProduct function however I am confused as to how to do this. I've tried calling the function but commented it out since this did not work. What would be the proper way to display the dot product? Any help would be much appreciated!

#include <iostream>
#include <vector>
#include <ctime>
#include <iomanip>
using namespace std;
class list
{
    public:
        list();
        void input(int s);
        void output();
        double dotProduct(vector <double> a, vector <double> b);
    private:
        vector <int> v;
};
list :: list() : v()
{
}
void list :: input(int s)
{
    int t;
    for(int i = 1; i <= s; i++)
    {
        t = rand() % 10;
        v.push_back(t);
    }
}
void list :: output()
{
    for(unsigned int i = 0; i < v.size(); i++)
        cout << v[i] << " ";
    cout << endl;
}
double list :: dotProduct(vector <double> a, vector <double> b)
{
    double product = 0;
    if(a.size() != b.size())
    {
        cout << "Vectors are not the same size\n";
    }
    for (unsigned int i = 0; i < a.size(); i++)
    {
        product = product + a[i] * b[i];
    }

    return product;

}
int main()
{
    list L1, L2,ob1;
    int s;
    cout << "Enter the size of list 1: \n";
    cin >> s;
    L1.input(s);
    cout << "\nEnter the size of list 2: \n";
    cin >> s;
    L2.input(s);
    cout << "\nVector 1: ";
    L1.output();
    cout << endl;
    cout << "Vector 2: ";
    L2.output();
    cout << endl;

    cout << "The dot product is: ";
    //ob1.dotProduct(L1.output(),L2.output());
    cout << endl;   

    return 0;

}
Dogm
  • 1
  • 1
  • 3
    The return value of `list::output()` has not type `vector`. You cannot pass it to a function that expects one. – n314159 Nov 13 '19 at 23:58
  • Unrelated: When you get that far you may find some extra efficiency and versatility in in `double dotProduct(const vector & a, const vector & b);` – user4581301 Nov 13 '19 at 23:59

1 Answers1

0

dotProduct() really does not look like a member function. It doesn't access any of the instance data. If you want, you can define it as a regular function which takes two vectors and computes the dot product.

If you want to call this function with the vectors from the two lists L1 and L2 (why do you call the class "list", when it isn't a list?), you will either need to expose the actual vector "v" by making it public, or create a "getter" member function which returns it. The dotProduct() function doesn't return a class instance or a vector, it returns a scalar value.

An alternate would be to define a class member function which operates on one class instance and takes a second class instance as an argument. The signature would be something like

list::dotProduct (list arg);

This would compute the dot product of the "this" instance and the argument arg.

It would be called

cout << L1.dotProduct (L2);
Mike Eager
  • 309
  • 1
  • 2
  • 9
  • My preferences would be to make it a free function so that it followed [the same paradigm as the non-assigning arithmetic operators](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading). – user4581301 Nov 14 '19 at 02:55