visual-studio-2010 - 从现有头文件编写 cpp 文件以运行测试程序。存在错误

标签 visual-studio-2010 visual-c++ testing header

我正在尝试从现有的 .h 文件编写一个 .cpp 文件。我收到一些错误。任何关于如何让它运行的反馈都值得赞赏。我对此有点陌生,我一直在观看视频并注销现有文件,但有些地方不对。

这是我的代码:

#include<iostream>
#include <cmath>
#include<string>
#include"quad.h"

using namespace std;


//--constructor (no default constructor for quadraticEquation)
  quadraticEquation::quadraticEquation(double initA, double initB, double initC)
// post: initialize coefficients of quadratic equation initA*x*x + initB + c
  {
  my_a=initA;
  my_b=initB;
  my_c=initC;

  }

//--accessors  
  double quadraticEquation::root1() const
  // pre: there is at least one real root: b*b-4*a*c >= 0.0 
  // post: returns one real root as  (-b+sqrt(b*b-4*a*c)) / (2*a)
  {
      (my_b+sqrt(my_b*my_b-4*my_a*my_c))/(2*my_a);
  }

  double quadraticEquation::root2() const
  // pre: there is at least one real root: b*b-4*a*c >= 0.0 
  // post: returns one real root as  (-b-sqrt(b*b-4*a*c)) / (2*a)
  { (my_b-sqrt(my_b*my_b-4*my_a*my_c))/(2*my_a);
  }

  bool quadraticEquation::hasRealRoots() const
  // post: returns true if an only if b*b-4*a*c >= 0.0, otherwise return false

  { if (my_b*my_b-4*my_a*my_c >= 0.0);
  }

  void quadraticEquation::display() const
  // post: shows the quadratic equation like -1x^2 + 3x - 9.7
  //       when my_a == -1, my_b = 3, and my_c == -9.7

  {
      cout << my_a << "x^2";


  if(my_b >= 0)
  cout << " + " << my_b << "x";
      else
  cout << " - " << abs(my_b) << "x";
 if(my_c >= 0)
  cout << " + " << my_c;
      else
  cout << " - " << abs(my_c);

  }


private:
  double my_a, my_b, my_c; // the three coefficients of the quadratic equation
};

这是我的错误:

cpp(24): warning C4552: '/' : operator has no effect; expected operator with side-effect
cpp(30): warning C4552: '/' : operator has no effect; expected operator with side-effect
warning C4390: ';' : empty controlled statement found; is this the intent?
error C2059: syntax error : 'private'
error C2059: syntax error : '}'
error C2143: syntax error : missing ';' before '}'
 error C2059: syntax error : '}'

最佳答案

从你的错误来看,很明显代码中存在基本的语法错误。当你的编译器明确说:

warning C4390: ';' : empty controlled statement found; is this the intent?

还鼓励在您的函数中放置大括号,这样您的显示函数就可以清理成如下所示:

void quadraticEquation::display() const
{
   cout << my_a << "x^2";
   if(my_b >= 0)
     { cout << " + " << my_b << "x"; }
   else
     { cout << " - " << abs(my_b) << "x"; }

   if(my_c >= 0)
     { cout << " + " << my_c; }
   else
     { cout << " - " << abs(my_c); }
}

编译器错误可以提供非常有用的信息 - 它为您提供行号,以及诸如“缺失;”之类的基本问题或“expected {, found (” 表示某处有一个小错字。仔细检查您的代码块 - 它们是否正确对齐?

关于visual-studio-2010 - 从现有头文件编写 cpp 文件以运行测试程序。存在错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16848020/

相关文章:

c - 如何使用 gettimeofday() 或与 Visual Studio C++ 2008 等效的东西?

vb.net - vb.net 中的 Code 128 条形码生成

c++ - TSP 的聚类算法

eclipse - Windows 8 与开发工具的兼容性。

c++ - 非常基本的模板功能出现问题

testing - Grails - 测试 GORM 关系

Eclipse 和测试包现在为白色且不可运行

sql-server - Silverlight 应用程序中的压力测试,它使用来自 WCF DataService 的数据,然后公开来自 SQLServer 数据库的数据

visual-studio-2010 - CodedUI 测试 : recognize html entities character

c++ - 如何在 VS2010 C++ 宏中使用 DTE.ExecuteCommand ("Edit.GoToDefinition")?