繁体   English   中英

编译时发生C ++错误,错误(无法解析的外部符号)

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

我使用的是Visual Basic2010。我已尽一切努力消除了这些错误。 这是程序背后的一个简单想法,但是我很好奇这个问题是否与我引用或调用变量有关。 我是C ++的新手,将获得任何帮助或建议。 提前致谢。

Box.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

};

//Main.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文件中声明要使用的类的每个Function [float functioName(int param)]。 这包括构造函数classname(int a);


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

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

应该在box.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);
}   

暂无
暂无

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

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