C++ identifier is niet gedefinieerd

Ik ben nieuw in C++. Ik begrijp niet waarom ik deze foutmelding krijg. Van de 5 uitspraken die vergelijkbaar zijn, markeren 3 fouten, maar de andere twee zijn oké. De fout zit in de hoofdfunctie.

   #include <iostream>
using namespace std;
// Function declaration
void getGallons(int wall);
void getHours(int gallons);
void getCostpaint(int gallons, int pricePaint);
void getLaborcharges(int hours);
void getTotalcost(int costPaint, int laborCharges);
// Function definition
void getGallons(int wall)
{
    int gallons;
    gallons = wall / 112;
    cout << "Number of gallons of paint required: " << gallons << endl;
}
// Function definition
void getHours(int gallons)
{
    int hours;
    hours = gallons * 8;
    cout << "Hours of labor required: " << hours << endl;
}
// Function definition
void getCostpaint(int gallons, int pricePaint)
{
    int costPaint;
    costPaint = gallons * pricePaint;
    cout << "The cost of paint: " << costPaint << endl;
}
// Function definition
void getLaborcharges(int hours)
{
    int laborCharges;
    laborCharges = hours * 35;
    cout << "The labor charge: " << laborCharges << endl;
}
// Funtion definition
void getTotalcost(int costPaint, int laborCharges)
{
    int totalCost;
    totalCost = costPaint + laborCharges;
    cout << "The total cost of the job: " << totalCost << endl;
}
// The main method
int main()
{
    int wall;
    int pricePaint;
    cout << "Enter square feet of wall: ";
    cin >> wall;
    cout << "Enter price of paint per gallon: ";
    cin >> pricePaint;
    getGallons(wall);
    getHours(gallons); // error here
    getCostpaint(gallons, pricePaint);
    getLaborcharges(hours); // error here
    getTotalcost(costPaint, laborCharges); //error here
    return 0;
}

Deze les was gericht op het gebruik van functies en het doorgeven van parameters in de code. Ik mag geen globale variabelen gebruiken. Als je een betere manier hebt om dit te doen, deel het dan.


Antwoord 1, autoriteit 100%

Terugbrengen tot drie regels (de andere fouten zijn analoog):

int wall;    
getGallons(wall);
getHours(gallons); // error here

Terwijl wallis gedefinieerd, is gallonsdat niet. En waar wil je eigenlijk gallonsvandaan halen? Het resultaat zit diep verborgen in een andere functie. Hoe wil je het daar vandaan krijgen?

Nou, je hebt een retourwaarde nodig:

 int getGallons(int wall)
//^^^ !
{
     int gallons = wall / 112;
     // ...
     return gallons; // !
}

Op deze manier kunt u uw functie als volgt gebruiken:

int gallons = getGallons(wall);
// now gallons is defined and you can use it:
getHours(gallons);

Analoog voor de andere functies en variabelen.

Meestal is het geeneen goed idee om logica (berekeningen) en uitvoer in dezelfde functie te combineren. Dus ik zou liever het schrijven naar de console verplaatsen naar de functie main:

int getGallons(int wall) { return wall / 112; }
int getHours(int gallons) { return gallons * 8; }
int wall;
std::cin >> wall;
int gallons = getGallons(int wall);
std::cout << ...;
int hours = getHours(gallons);
std::cout << ...;

Kennisgeving? Alle input/output is nu op hetzelfde niveau…

Kanttekening: het is niet nodig om functies te declareren voordat u ze definieert als u ze niet gebruikt vóór de definitie:

//void f(); // CAN be ommitted
void f() { };
void g() { f(); }

Tegenvoorbeeld:

void f();
void g() { f(); } // now using f before it is defined, thus you NEED do declare it
void f() { };

Als je de declaraties nog steeds wilt behouden, is dat nogal een kwestie van stijl (maar wordt belangrijk bij het beheren van code in verschillende compilatie-eenheden, omdat je dan de declaraties in header-bestanden zou hebben – je zult snel in de volgende lessen tegenkomen ).


Antwoord 2, autoriteit 25%

Reden is dat variabelen niet zijn gedefinieerd voordat ze worden gebruikt.
Volgende wijzigingen toegevoegd aan de code.

  • aangezien je functies als “getSomeValue()” hebt genoemd, is het beter om een ​​retourtype te gebruiken in plaats van void.
  • het is beter om dubbel te gebruiken in plaats van int, omdat er delen in de berekening zijn
  • gebruikte ook geneste functieaanroepen om het aantal regels code te verminderen.

Vaste code:

#include <iostream>
using namespace std;
// Function declaration
int getGallons(int wall);
int getHours(int gallons);
int getCostpaint(int gallons, int pricePaint);
int getLaborcharges(int hours);
int getTotalcost(int costPaint, int laborCharges);
// Function definition
int getGallons(int wall)
{
    int gallons;
    gallons = wall / 112;
    cout << "Number of gallons of paint required: " << gallons << endl;
    return gallons;
}
// Function definition
int getHours(int gallons)
{
    int hours;
    hours = gallons * 8;
    cout << "Hours of labor required: " << hours << endl;
    return hours;
}
// Function definition
int getCostpaint(int gallons, int pricePaint)
{
    int costPaint;
    costPaint = gallons * pricePaint;
    cout << "The cost of paint: " << costPaint << endl;
    return costPaint;
}
// Function definition
int getLaborcharges(int hours)
{
    int laborCharges;
    laborCharges = hours * 35;
    cout << "The labor charge: " << laborCharges << endl;
    return laborCharges;
}
// Funtion definition
int getTotalcost(int costPaint, int laborCharges)
{
    int totalCost;
    totalCost = costPaint + laborCharges;
    cout << "The total cost of the job: " << totalCost << endl;
    return totalCost;
}
// The main method
int main()
{
    int wall;
    int pricePaint;
    cout << "Enter square feet of wall: ";
    cin >> wall;
    cout << "Enter price of paint per gallon: ";
    cin >> pricePaint;
    int costPaint = getCostpaint(getGallons(wall), pricePaint);
    int laborCharges = getLaborcharges(getHours(getGallons(wall)));
    getTotalcost(costPaint, laborCharges);
    return 0;
}

Antwoord 3

Hier zijn een paar fouten/problemen

  1. Je hebt functiedeclaraties die overbodig zijn. Je hebt ze alleen nodig als je van plan bent de functie vóór de definitie aan te roepen.

  2. In uw hoofdmethode declareert u geen gallons

  3. In je hoofdmethode geef je geen waarden voor muur en prijsverf.

  4. In uw functies werkt u via neveneffecten, wat inhoudt dat u naar de console print in plaats van iets terug te sturen.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

seventeen − five =

Other episodes