简体   繁体   English

3个未解析的外部符号(C ++初学者)

[英]3 Unresolved external symbols (beginner C++)

my intro computer science class just covered functions this week and after checking the major thread for "Unresolved external symbols" I think it is either 我的计算机科学入门课程本周刚刚介绍了函数,并且在检查了“未解决的外部符号”的主线程后,我认为是

a) You declared the functions but never called them after main a)您声明了函数,但从未在main之后调用它们

b) You are missing the correct library b)您缺少正确的库

I'm just not sure which one it is or how to correctly go about it. 我只是不确定它是哪一个,或者如何正确地解决它。 Also, I think my logic is slightly flawed in the calcSideC block although Im not sure 此外,我认为我的逻辑在calcSideC块中略有瑕疵,尽管我不确定

#include <iostream>
#include <cmath>

using namespace std;

float getSide();
float calcSideC(float sideA, float sideB, float total);
void displaySideC(float sideC);

int main()
{
    {
        float sideA = 0.0;
        float sideB = 0.0;
        float total = sideA + sideB;
        float sideC = sqrt(total);
        sideA = getSide();
        sideB = getSide();
        sideC = calcSideC(sideA, sideB, total);
        displaySideC(sideC);

        return 0;
    }

    float getSide();
    {
        float sideA;
        cout << "Enter two sides of a right triangle.\n\n" << "Side A: \n" << "Please enter the dimension:  ";

        cin >> sideA;

        return sideA;
    }
    float getSide();
    {
        float sideB;
        cout << "\n\n" << "Side B: \n" << "Please enter the dimension:  ";
        cin >> sideB;

        return sideB;
    }
    float calcSideC(float sideA, float sideB, float total);
    {
        float sideA;
        float sideB;
        float total;
        float sideC;
        pow(sideA, 2);
        pow(sideB, 2);
        float sqrt(total);
        return sideC;
    }
    void displaySideC(float sideC);
    {
        float sideC;
        cout << "The dimension of Side C is:  " << sideC;
    }
    system("pause");
    return 0;
}

You can't define functions inside of other functions (not that you are doing it correctly anyway, as you have extra ; that are causing the code to behave differently than you are expecting). 您不能在其他函数中定义函数(不是因为您有多余的东西,无论如何您还是在正确地进行操作;这导致代码的行为与预期的不同)。

You need to move the function definitions out of main() . 您需要将函数定义移出main()

Now, with that syntax error fixed, you still have quite a few logic errors in your code, including: 现在,修复了该语法错误,您的代码中仍然有很多逻辑错误,包括:

  • declaring function local variables that are the same names as function parameters 声明与函数参数同名的函数局部变量

  • ignoring the return values of std::pow() an std::sqrt() 忽略std::pow()std::sqrt()的返回值

  • calculating total before sideA and sideB have been assigned values by the user. 在用户为sideAsideB分配值之前计算total

Try something more like this instead: 尝试类似这样的方法:

#include <iostream>
#include <cmath>

using namespace std;

float getSideA()
{
    float sideA;
    cout << "\n\n" << "Side A: \n" << "Please enter the dimension:  ";    
    cin >> sideA;
    return sideA;
}

float getSideB()
{
    float sideB;
    cout << "\n\n" << "Side B: \n" << "Please enter the dimension:  ";
    cin >> sideB;
    return sideB;
}

float calcSideC(float sideA, float sideB)
{
    return sqrt(pow(sideA, 2) + pow(sideB, 2));
}

void displaySideC(float sideC)
{
    cout << "The dimension of Side C is:  " << sideC;
}

int main()
{
    cout << "Enter two sides of a right triangle.";
    float sideA = getSideA();
    float sideB = getSideB();

    float sideC = calcSideC(sideA, sideB);
    displaySideC(sideC);

    system("pause");
    return 0;
}

Or, if you want to forward-declare the functions: 或者,如果您要向前声明功能:

#include <iostream>
#include <cmath>

using namespace std;

float getSideA();
float getSideB();
float calcSideC(float sideA, float sideB);
void displaySideC(float sideC);

int main()
{
    cout << "Enter two sides of a right triangle.";
    float sideA = getSideA();
    float sideB = getSideB();

    float sideC = calcSideC(sideA, sideB);
    displaySideC(sideC);

    system("pause");
    return 0;
}

float getSideA()
{
    float sideA;
    cout << "\n\n" << "Side A: \n" << "Please enter the dimension:  ";    
    cin >> sideA;
    return sideA;
}

float getSideB()
{
    float sideB;
    cout << "\n\n" << "Side B: \n" << "Please enter the dimension:  ";
    cin >> sideB;
    return sideB;
}

float calcSideC(float sideA, float sideB)
{
    return sqrt(pow(sideA, 2) + pow(sideB, 2));
}

void displaySideC(float sideC)
{
    cout << "The dimension of Side C is:  " << sideC;
}

You declared your functions inside main! 您在main中声明了函数! This is wrong. 错了 Move them outside. 将它们移到外面。 Next, functions do NOT have semi-colons on the declarative line.. 接下来,函数在声明行上没有分号。

IE: void func(); {} IE: void func(); {} void func(); {} is wrong because it should be void func() {} with no semi-colon. void func(); {}是错误的,因为它应该是void func() {} ,没有分号。

  • You have a function getSide() TWICE.. 您具有函数getSide() TWICE ..
  • You return from main before system("pause"); 您从main之前返回system("pause");
  • calcSideC has local variables which are same as the parameters (redeclaration error). calcSideC具有与参数相同的局部变量(重新声明错误)。
  • displaySideC has local variable which is the same as the parameters (redeclaration error). displaySideC局部变量与参数相同(重新声明错误)。
  • calcSideC doesn't use the result of sqrt(total); calcSideC不使用sqrt(total);的结果sqrt(total); .
  • calcSizeC doesn't actually calculate sideC using pythagorean theorem because you don't even use pow result.. calcSizeC实际上不会使用勾股定理来计算sideC,因为您甚至都不使用pow结果。

Correct code would be: 正确的代码是:

#include <iostream>
#include <cmath>

using namespace std;

float getSideA();
float getSideB();
float calcSideC(float sideA, float sideB);
void displaySideC(float sideC);

int main()
{
    float sideA = 0.0;
    float sideB = 0.0;
    float sideC = 0.0;

    sideA = getSideA();
    sideB = getSideB();
    sideC = calcSideC(sideA, sideB);
    displaySideC(sideC);

    system("pause");
    return 0;
}


float getSideA()
{
    float sideA;
    cout << "Enter two sides of a right triangle.\n\n" << "Side A: \n" << "Please enter the dimension:  ";

    cin >> sideA;

    return sideA;
}

float getSideB()
{
    float sideB;
    cout << "\n\n" << "Side B: \n" << "Please enter the dimension:  ";
    cin >> sideB;

    return sideB;
}

float calcSideC(float sideA, float sideB)
{
    float total = pow(sideA, 2);
    total = total + pow(sideB, 2);
    return sqrt(total);
}

void displaySideC(float sideC)
{
    cout << "The dimension of Side C is:  " << sideC;
}

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

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