课堂创作[编程初学者]

时间:2011-04-17 18:52:35

标签: c++ class

这是我的第一个带有类的代码。 dev c ++编译器找到4个错误,所以我需要一个帮助。我认为我的概念可能有些不对劲 这是头文件“complex.h”

class complex{
  public:
         bool ReadComplex();
  private:
          double real;
          double imag;
  };

这是.cpp文件

#include "complex.h"
#include <iostream.h>
#include <math.h>

using namespace std;

bool complex::ReadComplex()
 { cout<<"Enter the real part";
   cin>>real;
   cout<<"Enter the imaginary part";
   cin>>imag;
   return true;
                       }

我得到4个错误

C:/ Dev-Cpp / include / c ++ / 3.4.2 / mingw32 / bits / c ++ config.h:57:错误:在“命名空间”之前预期的unqualified-id

C:/ Dev-Cpp / include / c ++ / 3.4.2 / mingw32 / bits / c ++ config.h:57:error:expected ,' or;'在“命名空间”之前

C:/ Dev-Cpp / include / c ++ / 3.4.2 / mingw32 / bits / c ++ config.h:61:错误:在';'之前的预期命名空间名称令牌

C:/ Dev-Cpp / include / c ++ / 3.4.2 / mingw32 / bits / c ++ config.h:61:错误:`'不是命名空间


非常感谢,

4 个答案:

答案 0 :(得分:6)

类定义应以;

结尾
class complex
{
    // ....
} ;
//^ missing semi-colon

答案 1 :(得分:2)

您忘记了类定义末尾的分号:

class complex{

}; //<------- here put a semicolon

答案 2 :(得分:2)

  • 确保将该类放在您自己的命名空间中,否则不要说using namespace std。有std::complex类型,虽然您不包含其标题,但允许实现者将其自身包含在任何标准标题中。
  • 使用分号结束您的班级定义:class complex { /* ... */ };
  • 请勿使用<iostream.h>。使用<iostream>。顺便说一下,std::名称空间中存在的东西。
  • 什么是<Math.h>?是否在项目树外部安装了一些第三方库?如果它是您自己的代码或在项目树中,则使用双引号,而不是尖括号。双引号要​​求编译器在树中搜索代码,而尖括号则要求编译器查看系统目录。
  • 您确定标准数学标题不会吗?请查看<cmath>标题。

你也应该

  • 制作GetReal()GeatImag() const函数。如果GetSet对应的人没有做任何特别的事情,你应该抛弃它们并将成员数据公开。这是因为更少的代码可以减少错误。
  • 只要有意义,你应该将参数作为const引用。例如,在complex::Add()中,如果它不改变对象,它也应该是一个const函数。

答案 3 :(得分:1)

您的第一个类代码应该是:

class complex{
  };

int main()
  {
    return(0);
  }

严重。让它工作,没有编译器警告。然后一次添加一点复杂性,永远不要添加到不起作用的代码。

相关问题