简体   繁体   English

2个未解析的外部符号(C ++函数基础)

[英]2 unresolved external symbols (C++ function basics)

We learned about functions in class this week and I have narrowed my code down to 1 error which is "2 unresolved external symbols", last time I had this error was because I declared my functions within main but I made sure not to make the same mistake this time. 这周我们了解了课堂上的函数,我将代码范围缩小到1个错误,即“ 2个未解析的外部符号”,上一次出现此错误是因为我在main中声明了函数,但确保不要做相同的事情这次错了。

Also, when trying to run the program it says that 另外,当尝试运行程序时,它说

'File Name' is not recognized as an internal or external command, operable program or batch file. 无法将“文件名”识别为内部或外部命令,可操作程序或批处理文件。 I have turned on Not Using Precompiled Headers to no avail. 我没有启用“不使用预编译头”。

I have read numerous threads none of which helped, been working on this project for 7 hours now 我读了许多线程,但都无济于事,已经在这个项目上工作了7个小时

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

int getDivisor();
int calcSquare(int number);
void findNumbers(int divisor, int number, int square);


int main()
{
    int divisor = 0;
    int square = 0;
    int number = 0;

        cout << "Enter a divisor: ";
        cin >> divisor;
        cout << "Here are the numbers from 0 to 100 that are evenly 
divisible by " << divisor << ",\nand their squares:\n\n";

        square = pow(number, 2);
        for (int count = 0; ++count;)
        {
            if (count >= 0 && count <= 100, count % divisor == 0)
            {
                int calcSquare(number);
                cout << number << setw(6) << square << endl;

                return square;
            }
        }
}

There were a lot of problems with your code, and your edits made them worse. 您的代码有很多问题,您的编辑使它们变得更糟。

This is what I think you were trying to do: 这是我想您正在尝试做的事情:

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

int getDivisor();
int calcSquare(int divisor);
void findNumbers(int divisor);

int main()
{
    int divisor = getDivisor();
    findNumbers(divisor);
    return 0;
}

int getDivisor()
{
    int divisor = 0;
    cout << "Enter a divisor: ";
    cin >> divisor;
    return divisor;
}

int calcSquare(int divisor)
{
    return divisor * divisor;
}

void findNumbers(int divisor)
{
    cout << "Here are the numbers from 1 to 100 that are evenly divisible by " << divisor << ", and their squares : \n\n";
    for (int count = 1; count <= 100; ++count)
    {
        if (count % divisor == 0)
        {
            cout << count << setw(6) << calcSquare(count) << "\n";
        }
    }
}

Putting the function prototypes at the beginning of your program is fine, but you need to understand where and how you implement the functions OUTSIDE of your main function. 将函数原型放在程序的开头很好,但是您需要了解在主要函数之外的地方以及如何实现函数。 As you said, this is the second time you've had this problem. 正如您所说,这是您第二次遇到此问题。 I hope this helps you to avoid this issue in the future. 我希望这可以帮助您将来避免此问题。

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

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