簡體   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