Redeclaration and definition problems
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Redeclaration and definition problems

  1. #1
    Join Date
    Dec 2012
    Posts
    1

    Redeclaration and definition problems

    I'm having severe problems with my code. We are asked to take in the Vehicle name, tank size, and MPG. We are give the distance between the cities and are supposed to output the # of tankfuls required to make the ship. The errors I am getting are:

    variable or field 'doOutput' declared vod
    'int doOutput' redeclared as different kind of symbol
    previous declaration of 'void doOutput
    declaration of 'int doOutput'
    conflicts with previous declaration 'void doOutput'
    Then it tells me that Vehicle, MPG, and fillups are not declared on line 90 (were the function is supposed to be opened)
    What is wrong? It seems to revolve around the redelcaration that is happening of doOutput. But why is it doing this?





    void menu();
    void getinput();
    void doCalculations(char Vehicle[3][20], double tank[3], double mpg[3]);
    void doOutput(char Vehicle[3][20],double mpg[3], double fillups[3]);
    void cls();

    #include <cstdlib>
    #include <iomanip>
    #include <iostream>
    using namespace std;



    int main()
    {
    cout << "Welcome, please choose one of the following options" << '\n';
    cout << '\n';
    menu();
    }
    void menu()
    {
    int choice;
    cout << "1. Enter the data!" << '\n';
    cout << "2. Quit the Program!" << '\n';
    cout << "Enter the choice you would like below: ";
    cin >> choice;

    switch (choice)
    {
    case 1:
    getinput();
    break;

    case 2:
    return;
    break;

    default:
    cout << "DO IT AGAIN! I SAID PICK FROM THE CHOICES!!!" << '\n';
    menu();
    break;
    }


    }

    void cls()
    {
    system("cls");
    }

    void getinput()
    {
    int counter;
    char Vehicle[3][20];
    double tank[3];
    double mpg[3];

    for (counter = 0; counter <3; counter++)
    {
    cout << "What is the Vehicle's Name: ";
    cin >> Vehicle[counter];
    cout << "What is the Vehicle's gas tank size: ";
    cin >> tank[counter];
    cout << "What is the Vehicle's Miles Per Gallon: ";
    cin >> mpg[counter];
    }
    doCalculations(Vehicle,tank, mpg);
    }
    void doCalculations(char Vehicle[3][20], double tank[3], double mpg[3])
    {
    int counter;
    double distance = 527.8;
    double milespertank[3];
    double fillups[3];

    for (counter =0; counter <3; counter ++)
    {
    milespertank[counter] = (tank[counter]*mpg[counter]);
    fillups[counter] = ((distance)/(milespertank)[counter]);
    }
    doOutput(Vehicle, fillups, mpg);
    }

    void doOutput(Vehicle[3][20],mpg[3], fillups[3])

    {
    char Vehicle[3][20];
    double tank[3];
    double mpg[3];
    double distance = 527.8;
    double milespertank[3];
    double fillups[3];
    int counter

    cout << "The program calculates that: "
    for (counter = 0; counter <3; counter++)
    {
    cout << "Vehicle Name: " << Vehicle[counter];
    cout << "Vehicle MPG: " << mpg[counter];
    cout << "# of Fillups: " << fillups[counter];
    cout << '\n';
    }
    menu();
    }

  2. #2
    Join Date
    May 2004
    Location
    45,000FT Above Nevada
    Posts
    1,538

    Re: Redeclaration and definition problems

    Do you see a difference between the function prototype and the function itself ?
    This is what the compiler is complaining about...


    Code:
    void doOutput(char Vehicle[3][20],double mpg[3], double fillups[3]);
    void doOutput(Vehicle[3][20],mpg[3], fillups[3])
    Jim
    ATP BE400 CE500 (C550B-SPW) CE560XL MU300 CFI CFII

    "The speed of non working code is irrelevant"... Of course that is just my opinion, I could be wrong.

    "Nothing in the world can take the place of persistence. Talent will not; nothing is more common than unsuccessful men with talent. Genius will not; unrewarded genius is almost a proverb. Education will not; the world is full of educated derelicts. Persistence and determination are omnipotent. The slogan 'press on' has solved and always will solve the problems of the human race."...Calvin Coolidge 30th President of the USA.

  3. #3
    Join Date
    Jan 2012
    Location
    India
    Posts
    135

    Re: Redeclaration and definition problems

    Can we name the local variables of a function with the same names of parameters
    we are passing to it ? How the program will work then ?

  4. #4
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,632

    Re: Redeclaration and definition problems

    Yes that's possible. The local names are just as the name say local.
    Code:
    #include <iostream>
    int Something(int a)
    {
      return a;
    }
    int main()
    {
      int a = 1;
      std::cout << "Something is " << Something(a) << std::endl;
      return 0;
    }
    If this is what you questioned...
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,931

    Re: Redeclaration and definition problems

    Quote Originally Posted by new_2012 View Post
    Can we name the local variables of a function with the same names of parameters
    we are passing to it ? How the program will work then ?
    Please note, the variable name has a scope where it remains effective. If variable is declared as function parameter or local function variable, the scope is going to be limited by function body. Out of this scope the variable of the same name means absolutely different region of memory.

    Back to the S_M_A's sample:
    Code:
    #include <iostream>
    int Something(int a) // (2)
    {
      return a;
    }
    int main()
    {
      int a = 1;  // (1)
      std::cout << "Something is " << Something(a) << std::endl; // (3)
      return 0;
    }
    Variable a (1) is a memory absolutely different from a (2), though the value from a (1) gets copied to a (2) when call occurs in (3).
    Best regards,
    Igor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center