Print 

Author Topic: C++ Help!  (Read 6088 times)

Offline Goalie

  • Posts Too Much
  • *****
  • Posts: 2462
  • Congrats Jordan Lynch, 3rd place in Heisman!!!
    • York Set/Tech Crew
Re: C++ Help!
« Reply #30 on: April 11, 2008, 09:06:08 PM »
Still gives me the problem at the call statement in main() for the function.
You blame me?  Remember it had to get past 10 other players before I saw the ball.

Thanks to Spidey for this sig!                                                                                                                  .

Offline AR~Archon

  • Senior Poster
  • ****
  • Posts: 846
  • No surrender, no retreat
    • MacFire (Xfire for Mac)
Re: C++ Help!
« Reply #31 on: April 12, 2008, 12:11:54 AM »
This is what my prototype says:

void buildArrays(double, double, double);

It looks okay.  Not sure what the problem is.

Remember that the types in a function (or method) prototype must be consistent with the definition of the function.  Your declaration tells the compiler that you have a function that takes three floating point numbers, not three arrays of floating point numbers.  Edison is correct in that you need to include the [] in your function declaration.  That tells the compiler that the arguments are arrays of 'double's, and not individual 'double' values.  It helps to include the parameter names in the declaration (prototype), too, just like in the definition so it's more clear.

Try this:

Code: [Select]
void buildArrays(double gas[], double electricity[], double water[]);

A lot of the time I actually copy the header line from my function/method definition up to the top and turn it into the declaration.  It helps avoid consistency problems.


I prefer to create only one variable per line, as it is easier to see. I don't know if it's different in C++, but in Java you declare it this way: double[] ArrayName = new double[numElements];

I also prefer one variable declaration per line :).

The syntax and semantics for arrays in C and C++ are quite a bit different than Java.  Last time I used Java, it didn't have the same semantics for the arrays that Goalie is dealing with here.  Not sure if the language has changed since then.

In fact, I think that's the problem, because the raised x might indicate an array, and it can't convert an array of doubles to a single double.

It's not a raised x, it's actually a star (*).  It's the pointer syntax used by C and C++, and is a consequence of how C/C++ arrays were designed.  Correct, the error message is saying that it can't convert the pointer to the array-of-double 'gas' into a double.

So double[] gas instead of double gas[].

In C/C++ the [] goes after the variable name.
The minstrel boy to the war has gone...
Understanding is a three edged sword.  Your side, their side, and the truth.
Who are you?  What do you want?  Why are you here?  Where are you going?




Offline Goalie

  • Posts Too Much
  • *****
  • Posts: 2462
  • Congrats Jordan Lynch, 3rd place in Heisman!!!
    • York Set/Tech Crew
Re: C++ Help!
« Reply #32 on: April 12, 2008, 12:17:21 AM »
You're right.  I didn't declare the functions properly.  It just compiled for me.
Thanks so much.  It's always little things like this that I miss, not big things.
You blame me?  Remember it had to get past 10 other players before I saw the ball.

Thanks to Spidey for this sig!                                                                                                                  .

Offline MrMxyzptlk

  • Posts Too Much
  • *****
  • Posts: 9208
  • Never backward,           always forward!
    • My 5th Dimensional Homepage
Re: C++ Help!
« Reply #33 on: April 12, 2008, 10:19:00 AM »



heh.  When I saw your code fragments, Goalie, I popped up a Reply in a tab and almost started typing in it, but then caught myself and finished reading the rest of the thread first!

Well done, Archon.  GJ!

Mr. Mxy's current Word Corner word is catachresis    

Offline AR~Archon

  • Senior Poster
  • ****
  • Posts: 846
  • No surrender, no retreat
    • MacFire (Xfire for Mac)
Re: C++ Help!
« Reply #34 on: April 12, 2008, 08:48:45 PM »
Anytime, Goalie :).

Thanks, Mxy  :zoot:
The minstrel boy to the war has gone...
Understanding is a three edged sword.  Your side, their side, and the truth.
Who are you?  What do you want?  Why are you here?  Where are you going?




Print