简体   繁体   English

为什么我的程序始终无法获取外部信息?

[英]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. 当我尝试在Visual Studio中运行此代码时,我总是遇到无法解决的外部问题。

Structure Time Assignment for College 大学的结构时间分配

Create a structure called Time that has members days, hours, minutes, and seconds as ints. 创建一个名为Time的结构,其成员天,小时,分钟和秒为整数。 Create an instance of Time and initialize the members. 创建一个Time实例并初始化成员。 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. 例如,在将值添加到小时后,调用标准化函数,该函数应查看小时是否大于24。如果是,则将1加到day成员,并通过从当前值中减去24来重置小时。 DO the same for minutes and seconds over 59. 在59分钟和几秒钟内执行相同操作。

Your main program should add values to hours, minutes and seconds and after each, call the normalize function to properly set the values. 您的主程序应将值添加到小时,分钟和秒,然后分别在其后调用normalize函数以正确设置这些值。

Output the members after each update. 每次更新后输出成员。 Assume hours use a 24-hour clock. 假设小时使用24小时制。

#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(); 您声明为void normalize();的签名void normalize(); does not match the signature as it's defined in this file ( void normalize(Time &timenormalize) ). 与该文件中定义的签名不匹配( 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; Struct更改为structstruct是一个关键字,必须为小写;
  • changed Time to struct time in void normalize(..) : symbols are case sensitive: Time isn't declared, but struct time is; 改变Time ,以struct timevoid normalize(..)符号是区分大小写的: Time未声明的,但struct time是;
  • added missing . 补充缺少. to if (timenormalizehours) : if (timenormalize.hours) ; 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' : 然后链接器错误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. 将函数声明void normalize()更改为void normalize(struct time &) :您声明不带参数的normalize函数,但使用一个参数对其进行定义

And then finally the compilation error this introduces: 最后,这引入了编译错误:

  • change the normalize(); 更改normalize(); call to normalize( mytime ); 调用normalize( mytime ); because it takes a paramter 因为它需要一个参数
  • and declare a local variable struct mytime to pass as the parameter. 并声明要传递的局部变量struct mytime作为参数。
#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 . 打印一系列0 Now it's up to you to put some values in struct time mytime . 现在,您可以在struct time mytime一些值。 I hope this helps! 我希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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