2013-09-22 107 views
-1

我是一个新手,C++和我写一个程序添加两个复数:错误:“的Imag”没有指定类型

这里是我的.h文件:

#ifndef IMAGINE_H 
#define IMAGINE_H 

#include<iostream> 
using std::ostream; 

class Imag{ 
public: 
    double real; 
    double imag; 

    Imag() = default; 
    Imag(double,double); 

    Imag add(Imag); 
}; 

#endif 

和这里是我的.cpp文件:

#include<iostream> 
#include"imagine.h" 
using namespace std; 

Imag::Imag(){ 
    this-> real; 
    this-> imag; 

} 
Imag Imag:: add(Imag i){ 
    Imag result = new Image(); 
    result -> real = this->real + i -> real; 
    result -> imag = this-> imag + i-> imag; 
    return result; 
} 

编译时,它抱怨是这样的:

imagine.cpp:5:1: error: ‘Imag’ does not name a type 
Imag::Imag(){ 
^ 
imagine.cpp:10:1: error: ‘Imag’ does not name a type 
Imag Imag:: add(Imag i){ 
^ 

有人可以帮我吗?非常感谢!

+3

您需要在类声明结尾处使用';'。 – Jamal

+1

一旦你解决了这个问题,阅读关于物体和指针之间的区别,并摆脱那个邪恶的'新''。 –

+0

它会编译吗?将新的结果分配给对象的含义是什么? :\ – ApplePie

回答

4

您没有用分号结束类声明。这是正确的语法。

class ClassName { /* */ };

+0

我在我的程序中有分号。我忘了粘贴它。 –

+1

@NobWong:我们不会另外知道。你仍然得到相同的错误? – Jamal

+3

认真?!两次?!确保代码是* EXACTLY *,因为它现在在您的IDE中。 – ApplePie

0

我无法重现的问题,我得到不同的错误:

$ cat imagine.h 
#ifndef IMAGINE_H 
#define IMAGINE_H 

#include<iostream> 
using std::ostream; 

class Imag{ 
public: 
    double real; 
    double imag; 

    Imag() = default; 
    Imag(double,double); 

    Imag add(Imag); 
}; 

#endif 
$ cat imagine.cpp 
#include<iostream> 
#include"imagine.h" 
using namespace std; 

Imag::Imag(){ 
    this-> real; 
    this-> imag; 

} 
Imag Imag:: add(Imag i){ 
    Imag result = new Image(); 
    result -> real = this->real + i -> real; 
    result -> imag = this-> imag + i-> imag; 
    return result; 
} 
$ g++ -c -W -Wall -s -O2 imagine.cpp 
In file included from imagine.cpp:2: 
imagine.h:12: warning: defaulted and deleted functions only available with -std=c++0x or -std=gnu++0x 
imagine.cpp: In constructor ‘Imag::Imag()’: 
imagine.cpp:6: warning: statement has no effect 
imagine.cpp:7: warning: statement has no effect 
imagine.cpp: In member function ‘Imag Imag::add(Imag)’: 
imagine.cpp:11: error: expected type-specifier before ‘Image’ 
imagine.cpp:11: error: conversion from ‘int*’ to non-scalar type ‘Imag’ requested 
imagine.cpp:11: error: expected ‘,’ or ‘;’ before ‘Image’ 
imagine.cpp:12: error: base operand of ‘->’ has non-pointer type ‘Imag’ 
imagine.cpp:12: error: base operand of ‘->’ has non-pointer type ‘Imag’ 
imagine.cpp:13: error: base operand of ‘->’ has non-pointer type ‘Imag’ 
imagine.cpp:13: error: base operand of ‘->’ has non-pointer type ‘Imag’ 

这里是如何解决这些问题:

$ cat imagine.h 
#ifndef IMAGINE_H 
#define IMAGINE_H 

#include<iostream> 
using std::ostream; 

class Imag{ 
public: 
    double real; 
    double imag; 

    Imag(); 
    Imag(double,double); 

    Imag add(Imag); 
}; 

#endif 
$ cat imagine.cpp 
#include<iostream> 
#include"imagine.h" 
using namespace std; 
Imag::Imag(): real(0), imag(0) {} 
Imag::Imag(double r, double i): real(r), imag(i) {} 
Imag Imag::add(Imag i){ 
    Imag result; 
    result.real = real + i.real; 
    result.imag = imag + i.imag; 
    return result; 
} 
$ g++ -c -W -Wall -s -O2 imagine.cpp 
(No errors or warnings.) 

还有许多其他的方法来改善代码,例如,add可能需要代替const Imag&,而我们不需要#include <iostream>using namespace std;。将realimag隐藏起来也是一个好主意,并介绍公开阅读器的方法。

+0

嗨,谢谢!但是我并没有真正理解这部分代码:Imag :: Imag():real(0),imag(0){} Imag :: Imag(double r,double i):real(r),imag (i){}你能解释一下吗? –

+1

这些是构造函数的定义。在':'和'{'之间是初始化列表。如果您不理解它们,请阅读您最喜爱的C++书籍或Web教程的初始化列表一章(或者将它作为一个关于Stack Overflow的单独问题)。 – pts