435,346 Members | 2,318 Online
Need help? Post your question and get tips & solutions from a community of 435,346 IT Pros & Developers. It's quick & easy.

# Multiplying an array using pointers

 P: 4 Hey guys, I have put together a class called Mu Expand|Select|Wrap|Line Numbers class Mu {       private:               double **s;               int velocity,numbers;               double sPeak,muPeak,mu1;               double a1,b1,c1,u_s[100];           public:              Mu(int);              ~Mu();              void muCurve();              double get_data(int);                           int randomVelocity(int);              //double sValues(); }; My constructor looks like this: Expand|Select|Wrap|Line Numbers Mu::Mu(int n) {     //allocate 100 doubles and assign the pointer of the first element to s.      *s = new double [100]; } This class takes an array s with 100 elements in it. It also pulls in data from a file. In my member function muCurve, I want to multiply my data from a file with each element of my s array. Unfortunately I keep getting the error "invalid operands of type 'double' and 'double*' to binary 'operator*'" Can someone tell me how I can fix this?Any tips to point me in the right direction would be greatly appreciated, thanks. Here is my muCurve function: Expand|Select|Wrap|Line Numbers void Mu::muCurve() {        double v;        int n=100;        cout<
5 Replies

 Expert 10K+ P: 11,448 [quote=EJSpin]Hey guys, I have put together a class called Mu Expand|Select|Wrap|Line Numbers class Mu {       private:               double **s; ... Mu::Mu(int n) {     //allocate 100 doubles and assign the pointer of the first element to s.      *s = new double [100]; } I didn't read real closely (I didn't have my espresso yet ;-) but that code fragment stinks. Variable s is suposed to point to a double pointer (i.e. a thing that points to a double). s doesn't point to a 'thing' yet but you want that 'thing' to point to your new double array. kind regards, Jos Aug 11 '07 #2

 P: 4 I don't understand, you're saying I should create a pointer when I allocate memory? Aug 11 '07 #3

 Expert 10K+ P: 11,448 I don't understand, you're saying I should create a pointer when I allocate memory? After reading the rest of your code you most like want 's' to be a double*, not a double**. Try that first. You want 's' to point to the first double of the 100 doubles you just created using the 'new' operator. kind regards, Jos Aug 11 '07 #4

 P: 4 After reading the rest of your code you most like want 's' to be a double*, not a double**. Try that first. You want 's' to point to the first double of the 100 doubles you just created using the 'new' operator. kind regards, Jos Thanks that fixed of my problem. Unfortunately I don't seem to be reading my private data correctly in my muCurve member function. I'm reading in a text file from my get_data member function and setting those values in my class. But I can't seem to access them in muCurve. Expand|Select|Wrap|Line Numbers //Snippet of get_data double Mu::get_data(int range) {     int x;     double sPeak,muPeak,mu1;     srand((unsigned)time(NULL));       x = 1;//(rand() % range)+1;        if(x==1) {cout << "It's sunny and dry" << endl<>sPeak >> muPeak >> mu1;      cout <

 P: 4 Nevermind I can see from the snippet I posted what I did wrong. I accidentally defined a seperate case of my data "sPeak, muPeak, and mu1" in my get_data function, and the data I was trying to call in muCurve was never set in my class. Anyways, I thank you for your previous tips. Aug 12 '07 #6