包含头文件的c ++中的错误

时间:2017-05-29 04:00:27

标签: c++

您好我正在尝试使用头文件

运行此代码
//#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;
}

这是头文件。它被称为formula.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 area\c++\lab3\formula.h(15): error C4430: missing type specifier - 
 int assumed. Note: C++ does not support default-int
 c:\work area\c++\lab3\formula.h(15): error C2371: 'combinedresistors': 
 redefinition; different basic types
 c:\work area\c++\lab3\formula.h(10): note: see declaration of 
 'combinedresistors'
 .\Lab3.cpp(34): error C2088: '<<': illegal for class

我尝试了很多东西并浏览了互联网,无法找到有关这些内容的信息,所以我需要帮助。

感谢。

2 个答案:

答案 0 :(得分:0)

您需要在某些功能中编写double GetCombinedResistors() { return 1/((1/r1) + (1/r2) + (1/r3)); } 。像,

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

使用,

`<script>
function process()
{
var url="store/index.php?route=product/search&filter_name=" + document.getElementById("filter").value;
location.href=url;
return false;
}
</script>
<form id="store" onSubmit="return process();">
<input type="text" name="filter" id="filter" value="Store Search" onclick="this.value = '';"> 
<input type="submit" value="Search">
</form>`

答案 1 :(得分:0)

我不是一个优秀的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;
}