这是我的第一个带有类的代码。 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:错误:`'不是命名空间
非常感谢,
答案 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函数。如果Get
或Set
对应的人没有做任何特别的事情,你应该抛弃它们并将成员数据公开。这是因为更少的代码可以减少错误。complex::Add()
中,如果它不改变对象,它也应该是一个const函数。答案 3 :(得分:1)
您的第一个类代码应该是:
class complex{
};
int main()
{
return(0);
}
严重。让它工作,没有编译器警告。然后一次添加一点复杂性,永远不要添加到不起作用的代码。