【问题标题】:C++ [Linker error] undefined reference to `performComputation(char, double)'C++ [链接器错误] 未定义对“performComputation(char, double)”的引用
【发布时间】:2014-02-05 21:24:48
【问题描述】:

我有一个非常简单的项目,用于初级编程课程。我查看了所有涉及此错误的 Stack Overflow 问题,但没有一个对我的问题有帮助,所以我决定问问自己。

lab5.cpp

#include <cstdlib>
#include <iostream>
#include <math.h>
#include "shape.h"

using namespace std;
int main()
{
    char shape = '0';
    char compType = '0';
    double radius = 0.0;
    double height = 0.0;
    shape = inputShape();

    while (shape != QUIT)  
    {
        switch(shape)
        {
            case SPHERE:
                inputDimension(radius);
                compType = inputCompType(); 
                performComputation(compType, radius);
                break;
            case CYLINDER:
            case CONE:
                inputDimension(radius, height);
                compType = inputCompType(); 
                performComputation(shape, compType, radius, height);
                break;
        }
    }
    cout << "GoodBye!";
    system("PAUSE");
    return 0;           

}
char inputShape()
{
char c = '0';
cout << "Select a shape (1) Sphere, (2) Cylinder, (3) Cone (Q) Quit: ";
cin >> c;
return c;
}
void inputDimension(double& r)
{
cout << "Input a Radius: ";
cin >> r;
}
void inputDimension(double& r, double& h)
{
cout << "Input a Radius: ";
cin >> r;
cout << "Input a Height: ";
cin >> h;
}
char inputCompType()
{
char c = '0';
cout << "Select a Computation (1) Volume (2) Surface Area: ";
cin >> c;
return c;
}
void perfomComputation(char c, double d)
{
if(c == SURFACE_AREA)
{
    cout << "The surface area of the Sphere is: " << sphere_surface_area(d);
} 
else if (c == VOLUME)
{
    cout << "the volume of the Sphere is: " << sphere_volume(d);
}
}
void performComputation(char b, char c, double d, double e)
{
if (b == CYLINDER)
{
    if(c == SURFACE_AREA)
    {
        cout << "the surface area of the Cylinder is: " <<cylinder_surface_area(d,e);
    }
    else if (c == VOLUME)
    {
        cout << "the volume of the Cylinder is: " << cylinder_volume(d,e);
    }
}
else if (b == CONE)
{
    if (c == SURFACE_AREA)
    {
        cout << "the surface area of the Cone is: " << cone_surface_area(d,e);
    }
    else if (c == VOLUME)
    {
        cout << "the volume of the Cone is: " << cone_volume(d,e);
    }
}
}
double sphere_surface_area(double r)
{
return (4.0) * PI * pow(r,2);
}
double sphere_volume(double r)
{
return (4.0/3.0) * PI * pow(r,3);
}
double cylinder_surface_area(double r, double h)
{
return ((2.0) * PI * pow(r,2))+((2.0) * PI * r * h);
}
double cylinder_volume(double r, double h)
{
return (PI * pow(r,2) * h);
}
double cone_surface_area(double r, double h)
{
return (PI*pow(r,2)) +((PI*r)*sqrt((pow(r,2) + pow(h,2))));
}
double cone_volume(double r, double h)
{
return (1.0/3.0)*(PI * pow(r,2) * h);
}

形状.h

#include <iomanip>
#include <cstdlib>
#include <iostream>
//constant declarations
const double PI = 3.141593;
const char SPHERE = '1';
const char CYLINDER = '2';
const char CONE = '3';
const char QUIT = 'Q';
const char VOLUME = '1';
const char SURFACE_AREA = '2';
//function declarations
char inputShape();
void inputDimension(double&);
void inputDimension(double&, double&);
char inputCompType();
void performComputation(char, char, double, double);
void performComputation(char, double);
double sphere_volume(double);
double sphere_surface_area(double);
double cylinder_volume(double, double);
double cylinder_surface_area(double, double);
double cone_volume(double, double);
double cone_surface_area(double, double);

我的错误是:

[Linker error] undefined reference to `performComputation(char, double)'
ld returned 1 exit status
C:\Users\AdminM\Documents\Cpp\Lab5\Makefile.win [Build Error]  [Lab5.exe] Error 1 

【问题讨论】:

  • 显示准确的编译命令。 g++ 的参数顺序很重要。

标签: c++ windows compilation linker


【解决方案1】:

在lab5.cpp中

void perfomComputation(char c, double d){}

您错过了函数名称中的字母“r”。这就是为什么链接器找不到在头文件中声明的 performComputation 函数的实现。

【讨论】:

  • 现在我觉得自己非常愚蠢,因为我把那句话重写了大约 5 次。
【解决方案2】:

我在.h 文件中看到了void performComputation(char, double);,但在.cpp 中没有看到它我猜链接器也想知道它在哪里,至少因为您在代码中使用它。

编辑: 是的,我只是在浏览器中使用了文本搜索 :)

【讨论】:

    【解决方案3】:

    lab5.cpp 中的void perfomComputation(char c, double d) 有拼写 - r 字符缺失。

    【讨论】:

      猜你喜欢
      • 2011-05-01
      • 1970-01-01
      • 2013-04-01
      • 1970-01-01
      • 2016-07-21
      • 2022-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多