带标头文件的C 中的错误

Errors in c++ with header files

本文关键字:错误 文件      更新时间:2023-10-16

你好,我正在尝试使用标头文件运行此代码

//#include <iostream>
//#include <cmath>
#include "formula.h"
//using namespace std;

int main()
{
//double r1;
//double r2;
//double r3;
//double combinedresistors;
//double counter;
cout << "Enter your first resistance value:";
cin >> r1;
cout << "Enter your second resistance value:";
cin >> r2;
cout << "Enter your third resistance value:";
cin >> r3;
//combinedresistors = 1/((1/r1) + (1/r2) + (1/r3));
if (r1 == 0 )
  cout << "ERROR:You can't have your resistance(s) value be zero";
//  counter = 1;
else if (r2 == 0)
  cout << "ERROR:You can't have your resistance(s) value be zero";
//  counter = 1;
else if (r3 == 0)
    cout << "ERROR:You can't have your resistance(s) value be zero";
  //  counter = 1;
else
  cout << "Your combined Resistance is:" << combinedresistors << endl;
return 0;
}

这是标题文件。它称为公式。h

  //header file
  #include <iostream>
  #include <cmath>
  #include <string>
  using namespace std;
  double combinedresistors;
  double r1;
  double r2;
  double r3;
  combinedresistors = 1/((1/r1) + (1/r2) + (1/r3));

我已经评论了main.cpp文件中的零件,因为我认为我不需要它们,因为它们在标头文件中。这是我遇到的错误...

 c:work areac++lab3formula.h(15): error C4430: missing type specifier - 
 int assumed. Note: C++ does not support default-int
 c:work areac++lab3formula.h(15): error C2371: 'combinedresistors': 
 redefinition; different basic types
 c:work areac++lab3formula.h(10): note: see declaration of 
 'combinedresistors'
 .Lab3.cpp(34): error C2088: '<<': illegal for class

我尝试了很多事情,并在互联网上浏览了很多东西,但我需要帮助。

谢谢。

您需要在某些功能中编写combinedresistors = 1/((1/r1) + (1/r2) + (1/r3));。喜欢,

double GetCombinedResistors()
{
 return 1/((1/r1) + (1/r2) + (1/r3));
} 

使用,

  cout << "Your combined Resistance is:" << GetCombinedResistors() << endl;

我不是一个好的CPP程序员,但是您可以尝试以下操作:

//#include <cmath>
#include "formula.h"
//using namespace std;

int main()
{
double r1;
double r2;
double r3;
double combinedresistors;
double counter;
cout << "Enter your first resistance value:";
cin >> r1;
cout << "Enter your second resistance value:";
cin >> r2;
cout << "Enter your third resistance value:";
cin >> r3;
if (r1 == 0 || r2 == 0 || r3 == 0){
  cout << "ERROR:You can't have your resistance(s) value be zero";
}
else{
  combinedresistors = 1/((1/r1) + (1/r2) + (1/r3));
  cout << "Your combined Resistance is:" << combinedresistors << endl;
}
return 0;
}