编译时C++错误,错误(未解析的外部符号)

C++ errors when compiling, error (unresolved external symbol)

本文关键字:错误 外部 符号 C++ 编译      更新时间:2023-10-16

我正在使用Visual Basic 2010。我已经尝试了我能想到的一切来消除这些错误。这是程序背后的简单想法,但我很好奇这个问题是否与我如何引用或调用我的变量有关。我是C++新手,任何帮助或建议将不胜感激。提前谢谢。

盒子.cpp

#include "stdafx.h"
#include "Box.h"
#include <iostream>
#include <iomanip>
using namespace std;

double getVolume(int A, int B, int C){
double totalVolume;
totalVolume = A * B * C;
return totalVolume;
}
double getSurfaceArea(int A, int B, int C){
double totalSurface;
totalSurface = (A*B*2) + (B*C*2) + (A*C*2);
return totalSurface;
}
bool perfectBox(int A, int B, int C){
bool perfect;
if (A = B) 
    if (B = C)
        perfect = true;
    else
        perfect = false;
else
    perfect = false;
return perfect;
}
/

/Box.h

class Box
{
public:
int A, B, C;
Box(int A, int B, int C);
double getVolume(int A, int B, int C);
// get volume of entered sides

double getSurfaceArea(int A, int B, int C);
// calculate surgace are based on sides
bool perfectBox(int A, int B, int); 
// compare all 3 sides to determine if box is perfect
};

主.cpp

#include "stdafx.h"
#include "Box.h"
#include <iostream>
using namespace std;
Box::Box(int a, int b, int c){
}
int main()
{ 
int a, b, c;
cout << "Enter 3 side lengths of a box to determine volume, surface area and if it's perfect...
n";
cout << "length of Side 1: ";
cin >> a;
cout << endl << "Side 2: ";
cin >> b;
cout << endl << "Side 3: ";
cin >> c;
Box test(a, b, c);
cout << "Total Area: " << test.getVolume(a, b, c) << endl;
cout << "Total Surface: " << test.getSurfaceArea(a, b, c) << endl;
cout << "Is it a perfect box: " << test.perfectBox(a, b, c) << endl;
system ("Pause");
return 0;
}

您错过了命名空间声明。 当你提到getVolume时,它不仅仅是getVolume,而是Box::getVolume。

double Box::getVolume(int A, int B, int C){
double totalVolume;
totalVolume = A * B * C;
return totalVolume;
}
double Box::getSurfaceArea(int A, int B, int C){
double totalSurface;
totalSurface = (A*B*2) + (B*C*2) + (A*C*2);
return totalSurface;
}
bool Box::perfectBox(int A, int B, int C){
bool perfect;
if (A = B) 
    if (B = C)
        perfect = true;
    else
        perfect = false;
else
    perfect = false;
return perfect;
}

你能展示编译器输出吗?在您的构造函数中,您尚未分配 A、B、C,因此可能存在问题。此外,您正在使用赋值运算符,即 =,而不是比较运算符,即 ==,这是两个不同的命令。

简单的规则:要使用的类的每个函数都必须在类的 .h 文件中声明 [float functioName(int param)]。这包括构造函数类名(int a);


box.h 中缺少构造函数声明

Box(int a, int b, int c);

应该在框中移动.cpp

Box::Box(int a, int b, int c)
{
}

来自我们计算机科学系研究人员的提示:

首先包括系统标头,然后包括本地标头:

#include <iostream>
#include <iomanip>
#include "stdafx.h"
#include "Box.h"

而不是(根据<>或"的不同顺序):

#include "stdafx.h"
#include "Box.h"
#include <iostream>
#include <iomanip>

.h 文件中还缺少函数。在 box.cpp 中定义的每个函数都必须在 box.h 中声明

宣:

float getVolume();

定义:

float Box::getVolume()
{
  float localVariableForVolume = A*B*C;
  //this works too float localVariableForVolume = this->A*this->B*this->C;
  return(localVariableForVolume);
}   
相关文章: