首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从已有的头文件中写入cpp文件以运行测试程序。存在错误

从已有的头文件中写入cpp文件以运行测试程序。存在错误
EN

Stack Overflow用户
提问于 2013-05-31 08:28:07
回答 1查看 109关注 0票数 1

我正在尝试从现有的.h文件写入.cpp文件。我收到一些错误。任何关于如何让它运行的反馈都是非常感谢的。我在这方面有点新手,我一直在看视频并注销现有的文件,但有些地方不太对劲。

下面是我的代码:

代码语言:javascript
复制
#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
};

以下是我的错误:

代码语言:javascript
复制
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 : '}'
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-31 08:40:03

从您的错误中可以明显看出,代码中存在基本的语法错误。当你的编译器明确地说:

代码语言:javascript
复制
warning C4390: ';' : empty controlled statement found; is this the intent?

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

代码语言:javascript
复制
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); }
}

编译器错误可以是非常有用的-它给出了行号,对于诸如"missing ;“或"expected {,found (”)这样的基本问题,它表示某个地方有一个小的打字错误。仔细检查您的代码块-它们是否正确对齐?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16848020

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档