Reputation: 133
#include<iostream>
#include<cmath>
#include<complex>
#include<vector>
using namespace std;
vector<complex<double> > Compute_wn(int n);
int main()
{
Compute_wn(8);
return 0;
}
vector<complex<double> > Compute_wn(int n)
{
vector<complex<double> > V[n];
for(int i = 0; i<n; i++)
{
V[i] = complex<double>(cos(2.0*M_PI*double(i)/double(n)),sin(2.0*M_PI*double(i)/double(n)));
cout<<V[i]<<"\t";
}
cout<<"\n";
}
I am new to C++. When I compile this code the error I get is that error: no viable overloaded '='.
(V[i] = complex<double>(cos(2.0*M_PI*double(i)/double(n)),sin(2.0*M_PI*double(i)/double(n)));
) I don't understand how I need to overload the operator '=' for a vector of complex numbers of type double. As I understand that vector is a vector of complex numbers I should be able to directly assign to V[I].
Upvotes: 1
Views: 134
Reputation: 7202
You're mixing array and vector syntax, and here's the culprit:
vector<complex<double>> V[n];
This defines an array of size n
of vector<complex<double>>
. Since n
is not a compile-time constant, this shouldn't be allowed, but some compilers accept it as a non-standard extension. See here for discussion.
This means that V[i]
is actually a vector<complex<double>>
, not a complex<double>
, and that's why you can't assign a complex<double>
to it.
The fix is to correctly initialize your vector:
vector<complex<double>> V;
V.resize(n);
Note: you're currently not returning anything from your function which is declared to return vector<complex<double>>
, which will cause a bad case of Undefined-Behavior-related woes. Did you mean to return V;
?
Upvotes: 5