confusing error

This is a discussion on confusing error within the C++ Programming forums, part of the General Programming Boards category; the error im getting is Code: g++ -ofinalprog planet.c input.c input2.c addplanet.c addplanet.c: In function `double add_planet(std::vector<double, std::allocator<double> >&)': addplanet.c:9: ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    32

    confusing error

    the error im getting is
    Code:
    g++ -ofinalprog planet.c input.c input2.c addplanet.c
    addplanet.c: In function `double add_planet(std::vector<double, std::allocator<double> >&)':
    addplanet.c:9: error: invalid operands of types `double' and `<unknown type>' to binary `operator<<'
    addplanet.c:10: error: invalid operands of types `double' and `<unknown type>' to binary `operator<<'
    addplanet.c:11: error: invalid operands of types `double' and `<unknown type>' to binary `operator<<'
    addplanet.c:13: error: initializer expression list treated as compound expression
    addplanet.c:14: error: initializer expression list treated as compound expression
    addplanet.c:20: error: `surface_area' cannot be used as a function
    addplanet.c:21: error: `density' cannot be used as a function
    addplanet.c:22: error: `gravity' cannot be used as a function
    addplanet.c looks like this
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include "input.h"
    using namespace std;
    
    double add_planet(vector<double>& planet_info)
    {
       double name=input_planet("The name of the planet is ")<<endl;
       double mass=readDouble("The mass of the planet is ",false)<<endl;
       double radius=readDouble("The radius of the planet is ",false)<<endl;
       double surface_area(radius);
       double density(mass,radius);
       double gravity(mass,radius);
       
       planet_info.push_back(name);
       planet_info.push_back(mass);
       planet_info.push_back(radius);
       planet_info.push_back(planet_type());
       planet_info.push_back(surface_area(radius));
       planet_info.push_back(density(mass,radius));
       planet_info.push_back(gravity(mass,radius));
    }
    can someone explain to me what is wrong, i cannot figure out what the problem is

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    My suggestion would be to ditch the "type random words and symbols that you've seen in other programs" style of programming and instead focus on first figuring out what you want to do, then writing code that does that.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    These:
    Code:
    addplanet.c:9: error: invalid operands of types `double' and `<unknown type>' to binary `operator<<'
    addplanet.c:10: error: invalid operands of types `double' and `<unknown type>' to binary `operator<<'
    addplanet.c:11: error: invalid operands of types `double' and `<unknown type>' to binary `operator<<'
    ...are in reference to these:
    Code:
    double name=input_planet("The name of the planet is ")<<endl;
    double mass=readDouble("The mass of the planet is ",false)<<endl;
    double radius=readDouble("The radius of the planet is ",false)<<endl;
    You insert endl in a stream via operator<< to put a newline (and flush the stream) into the output stream. You seem to be treating the input_planet and readDouble functions as if they returned a reference to an output stream which is the only way that statement (or at least the parts in red) would work. If that were the case then you still wouldn't be able to assign the resultant stream object to a variable of type double. So, I would therefore assume they return a value of type double in which case why is the <<endl part even there at all?




    These:
    Code:
    addplanet.c:13: error: initializer expression list treated as compound expression
    addplanet.c:14: error: initializer expression list treated as compound expression
    ...are in reference to the following:
    Code:
    double density(mass,radius);
    double gravity(mass,radius);
    You're not initializing them properly, you can only initialize with a single value. Unless these are meant to be function prototypes... in which case they are still wrong.



    This:
    Code:
    addplanet.c:20: error: `surface_area' cannot be used as a function
    addplanet.c:21: error: `density' cannot be used as a function
    addplanet.c:22: error: `gravity' cannot be used as a function
    ...is in reference to this:
    Code:
    double surface_area(radius);
    double density(mass,radius);
    double gravity(mass,radius);
    
    ...
    
    planet_info.push_back(surface_area(radius));
    planet_info.push_back(density(mass,radius));
    planet_info.push_back(gravity(mass,radius));
    In the first statement, the variable surface_area is initialized with the value of the radius variable. The other two variable initializations (density/gravity) have already been mentioned as needing work. In the parts in red, you seem to be trying to use the tokens surface_area/gravity/density as function names passing in values as arguments. Once again, are these variables or function prototypes? If they are variables, then just use the variable name. If they are prototypes, then the prototypes are wrong.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 04:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21