编译时C++错误,错误(未解析的外部符号)
C++ errors when compiling, error (unresolved external symbol)
我正在使用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);
}
相关文章:
- 使用外部SDK工具链文件在VisualStudio上生成项目编译错误
- spdlog标头仅与外部fmt一起使用.spdlog错误:'内部':不是'fmt'
- 我收到错误LNK2001:未解析的外部符号(C++代码)
- Visual Studio 2017 中的 QtMetaObject 未解决的外部错误
- 外部"C"拆卸导致奇怪的链接器错误
- C++指针无法在函数外部传递值和分段错误(核心转储)错误
- 在Embarcadero C++ Builder中生成的DLL未解决的外部链接错误
- 可视抽象类 c++(错误 LNK 2001:未解析的外部符号)
- 如何摆脱C++中未解析的外部符号"private: static char"错误?
- 当 lib 已添加到其他依赖项时,如何在 VS 中调试未解析的外部符号错误
- 错误LNK2019未解析的外部符号"public: __thiscall SLinkList<char>::SLinkList<char>(void)"
- 外部函数在 main() 上调用时返回分段错误 11.(C++)
- 引入参数化构造函数后显示 LNK 2019 未解析外部符号的代码错误
- 每次我要使用外部资源时,我都会得到:collect2。exe:错误 ld 返回 1 退出状态
- 链接器错误:切换到unicode生成给出:未解析的外部符号WinMain
- C#WPF和C++/CLI以及C++在C++/CLI中添加外部库错误
- 函数 LNK2019 "int __cdecl __scrt_common_main_seh(void)" 中引用的未解析外部符号主错误 (?__scrt_common_main_seh@@YAHXZ
- C++:在"try"外部创建的类型会导致错误,但在内部不会
- 编译错误外部接口Simulink
- 为什么这会给出多个定义错误:外部 int i;国际 i;.