帮助错误说
thevoid CalculatorClass :: SumComplex();是私人的
我添加第二个文件(不仅是标题),但问题仍然存在
calculator.h
//header file
#include<iostream>
void SumComplec();
void DifComplex();
void MultComplex();
void DivComplex();
calculator.cpp
//i calculate the result of numbers
#include <iostream.h>
#include "calculator.h"
using namespace std;
class CalculatorClass{
//float r1,i1,r2,i2;
void SumComplex() { //add complex numbers
float sumRe,sumIm;
float r1,i1,r2,i2;
cout << "Enter 2 complex numbers: " << endl;
cin >> r1 >> i1 >> r2 >> i2; //read nimber
//sum of real and imanige part of number
sumRe = r1 + r2;
sumIm = i1 + i2;
if(sumIm < 0) {
cout << "Sum is " << sumRe << sumIm << "j" << endl;
}
else {
cout << "Sum is " << sumRe << "+" << sumIm << "j" << endl;
}
}
};
的main.c
#include <iostream.h> //add header and other file
#include "calculator.h"
#include "calculator.cpp"
using namespace std;
int main() {
int choise;
char op;
CalculatorClass CalculatorObject; //i call class
do { /* Elenxos gia swsti eisagwgi epilogis */
cout << "For complex numbers press 1,for normal numbers press 2!" << endl;
cin >> choise;
} while(choise != 1 && choise != 2);
do { /* Elenxos gia swsti eisagwgi telesti */
cout << "Choose operator:" << endl;
cin >> op;
} while(op != '+' && op != '-' && op != '*' && op != '/');
if(choise == 1) { //if is '+' i calculate
if(op == '+') {
CalculatorObject.SumComplex();
}
}
return 0;
}
答案 0 :(得分:1)
默认情况下,类中的成员变量和成员函数是私有的。因此,不能直接从课堂外引用它们。
在您的情况下,成员函数SumComplex()
是私有的,并在main
中引用。您需要在public:
定义之前写SumComplex
来公开。
请注意,public:
是一个开关,它与之后出现的类中的所有声明保持相关。因此,如果您在SumComplex
下添加更多内容,它也会公开,直到您明确切换回private:
。
答案 1 :(得分:0)
我猜你的班级结构应该是这样的:
<强> calculator.h 强>
#include <iostream>
using namespace std;
class CalculatorClass{
public:
//float r1,i1,r2,i2;
void SumComplex();
void DifComplex();
void MultComplex();
void DivComplex();
};
<强> calculator.cpp:强>
#include "calculator.h"
void CalculatorClass::SumComplex() { //add complex numbers
float sumRe,sumIm;
float r1,i1,r2,i2;
cout << "Enter 2 complex numbers: " << endl;
cin >> r1 >> i1 >> r2 >> i2; //read nimber
//sum of real and imanige part of number
sumRe = r1 + r2;
sumIm = i1 + i2;
if(sumIm < 0) {
cout << "Sum is " << sumRe << sumIm << "j" << endl;
}
else {
cout << "Sum is " << sumRe << "+" << sumIm << "j" << endl;
}
}
<强> main.cpp中:强>
#include "calculator.h"
using namespace std;
int main() {
int choise;
char op;
CalculatorClass CalculatorObject; //i call class
do { /* Elenxos gia swsti eisagwgi epilogis */
cout << "For complex numbers press 1,for normal numbers press 2!" << endl;
cin >> choise;
} while(choise != 1 && choise != 2);
do { /* Elenxos gia swsti eisagwgi telesti */
cout << "Choose operator:" << endl;
cin >> op;
} while(op != '+' && op != '-' && op != '*' && op != '/');
if(choise == 1)
{ //if is '+' i calculate
if(op == '+') {
CalculatorObject.SumComplex();
}
}
else{
if(op == '+')
cout<<"Sum Operation"<<endl;
else
cout<<"Something Else"<<endl;
}
return 0;
}
在Mac终端上进行测试:
g++ main.cpp calculator.cpp
./a.out
它按预期工作。
我希望,这会有所帮助。
答案 2 :(得分:0)
您的程序错过了'public:'关键字。如果您不包括私人或公共,那么您班级中的所有内容都将被视为私人。只需添加一个公共:在你的功能面前