简体   繁体   中英

Why do I keep getting unresolved externals for my program?

I keep getting unresolved externals when I try to run this code in Visual Studio.

Structure Time Assignment for College

Create a structure called Time that has members days, hours, minutes, and seconds as ints. Create an instance of Time and initialize the members. Create a function to normalize the time when values are added to it.

For example, after adding values to the hours, call the normalize function which should see if the hours > 24. If so, add 1 to the days member and reset hours by subtracting 24 from the current value. DO the same for minutes and seconds over 59.

Your main program should add values to hours, minutes and seconds and after each, call the normalize function to properly set the values.

Output the members after each update. Assume hours use a 24-hour clock.

#include <iostream>
using namespace std;

Struct time
{
int days = 0;
int hours = 0;
int minutes = 0;
int seconds = 0;

};

void normalize();

int main()
{
int clockRepeating;

for (clockRepeating = 0; clockRepeating < 150; clockRepeating++)
{
normalize();
}

return 0;
}

void normalize(Time &timenormalize)
{

if (timenormalize.days > 31)
timenormalize.days = 1;
if (timenormalizehours > 24)
{
timenormalize.hours = 0;
timenormalize.days++;
}
if (timenormalize.minutes > 59)
{
timenormalize.minutes = 0;
timenormalize.hours++;
}
if (time normalize.seconds > 59)
{
timenormalize.seconds = 0;
timenormalize.minutes++;
cout << timenormalize.days, timenormalize.hours, timenormalize.minutes, timenormalize.seconds;
}
else
timenormalize.seconds++;
cout << timenormalize.days, timenormalize.hours, timenormalize.minutes,timenormalize.seconds;

The signature you declared for void normalize(); does not match the signature as it's defined in this file ( void normalize(Time &timenormalize) ).

Here's a fixed up version of your code. First the compilation errors:

  • changed Struct to struct : struct is a key word, must be lowercase;
  • changed Time to struct time in void normalize(..) : symbols are case sensitive: Time isn't declared, but struct time is;
  • added missing . to if (timenormalizehours) : if (timenormalize.hours) ;
  • added } to the end of the file (probably copy/paste error).

And then the linker error undefined reference to 'normalize' :

  • change function declaration void normalize() to void normalize(struct time &) : you declare the normalize function without parameters, but define it with one parameter.

And then finally the compilation error this introduces:

  • change the normalize(); call to normalize( mytime ); because it takes a paramter
  • and declare a local variable struct mytime to pass as the parameter.
#include <iostream>
using namespace std;

struct time
{
    int days = 0;
    int hours = 0;
    int minutes = 0;
    int seconds = 0;
};

void normalize(struct time &);

int main()
{
    int clockRepeating;
    struct time mytime;

    for (clockRepeating = 0; clockRepeating < 150; clockRepeating++)
    {
        normalize( mytime );
    }

    return 0;
}

void normalize(struct time &timenormalize)
{

    if (timenormalize.days > 31)
        timenormalize.days = 1;
    if (timenormalize.hours > 24)
    {
        timenormalize.hours = 0;
        timenormalize.days++;
    }
    if (timenormalize.minutes > 59)
    {
        timenormalize.minutes = 0;
        timenormalize.hours++;
    }
    if (timenormalize.seconds > 59)
    {
        timenormalize.seconds = 0;
        timenormalize.minutes++;
        cout << timenormalize.days, timenormalize.hours, timenormalize.minutes, timenormalize.seconds;
    }
    else
        timenormalize.seconds++;
    cout << timenormalize.days, timenormalize.hours, timenormalize.minutes,timenormalize.seconds;
}

It prints a series of 0 . Now it's up to you to put some values in struct time mytime . I hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM